[英]How to Load, in Spring Boot Test, Basic Spring Application-Context Adding Only Named Components?
我正在嘗試加載,在 Spring Boot (v2.3.8) 應用程序的測試中,僅在基本Spring 應用程序上下文中命名組件。 更具體地說,我想在測試中加載 Spring 應用程序上下文,該應用程序上下文至少配置日志記錄、外部屬性的加載、驗證(通過 Java 驗證 API)和@Value
-plugging 但沒有 MVC、數據 Z9A87CZ44BD889359516本質上,我想通過一個帶約束注釋的接口(例如@NotBlank
)來測試一個只使用本地文件系統(沒有 MVC、數據 JPA 或任何其他 Spring 引導測試“切片”)的@Service
一個通過@Value
填充的數據成員。
我在不同的組合子集中嘗試了許多注釋(以及它們的一些固有屬性),但無濟於事:例如@SpringBootTest
、 @Configuration
、 @ContextConfiguration
、 @TestConfiguration
、 @TypeExcludeFilters
等。例如:
@Validated
public interface SomeService {
String someMethod (@NotBlank String someParam);
}
...
@Service
public class SomeServiceImpl implements SomeService {
@Value("${some.value}")
private String someValue;
@Override
public String someMethod(String someParam) {
// do something with local file-system,
// with someValue in hand, and
// knowing someParam not blank
}
}
...
@SpringBootTest(classes={SomeServiceImpl.class},webEnvironment=WebEnvironment.NONE)
class SomeServiceTests {
@Autowired
private SomeService someService;
@Test
void throwConstraintViolationException_whenSomeParamNil() {
assertThrows(
ConstraintViolationException.class,
()->someService.someMethod(new String()));
}
...
}
在此示例中,@SpringBootTest 的屬性classes
加載了引用的@SpringBootTest
,但掩蓋了我也在尋找的所有基礎知識(日志記錄、驗證等)。
以下摘錄來自 Craig Walls 的“Spring Boot in Action”,似乎正是我所尋求的,但它的主要工具( @SpringApplicationConfiguration
)現在已被棄用(並且它的官方推薦替代品 - @SpringBootTest
- 不起作用同樣,至少關於屬性classes
):
盡管@ContextConfiguration 在加載 Spring 應用程序上下文方面做得很好,但它不會使用完整的 Spring 引導處理來加載它...... SpringApplication 不僅加載應用程序上下文,還啟用日志記錄,加載外部屬性(application. properties 或 application.yml),以及 Spring Boot 的其他功能。 如果您使用@ContextConfiguration,您將無法獲得這些功能。 要在集成測試中恢復這些功能,您可以將 @ContextConfiguration 替換為 Spring Boot 的 @SpringApplicationConfiguration:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes=AddressBookConfiguration.class)
public class AddressServiceTests {
...
}
在那里,他得到了“完整的 Spring 引導處理”,但只添加了他引用的 class( @Service
)(不是他域的每個@Component
、 @Controller
和@Repository
)。
今天如何在不棄用@SpringApplicationConfiguration
的情況下做到這一點?
我最初聲明我的(不是 Craig Walls 的)示例(即@SpringBootTest(classes={SomeServiceImpl.class},webEnvironment=WebEnvironment.NONE)
,盡管它加載了引用的 class,但它掩蓋了應用程序上下文的所有基礎知識Spring Boot 會正常加載。通過進一步的實驗,我發現該語句是不准確的,因為並非所有但僅是一些基礎知識被模糊了,即驗證(Java 驗證 API 的實現)。換句話說,日志記錄,加載外部屬性和@Value-plugging 確實是加載/配置的。我認為在我所有的許多排列/嘗試中我一定感到困惑。
經過更多的實驗,我找到了答案。 我不能說這是否是最好的答案,但它很簡潔,而且肯定能解決問題......
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
// automatically configures validation (implementation of Java Validation API)
@ImportAutoConfiguration(ValidationAutoConfiguration.class)
// obscures/disables any otherwise-automatic web-environment;
// automatically configures logging, loading of external properties, and @Value-plugging;
// and, beyond that, adds to application-context only _cited_ classes/configuration-classes
@SpringBootTest(classes={SomeServiceImpl.class},webEnvironment=WebEnvironment.NONE)// full context loaded, if unqualified
public class SomeServiceTests {
...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.