spring - 接口(interface)上的 @SpringBootTest 注释不起作用

标签 spring spring-test junit5

目前无法将注释 SpringBootTest 放置在接口(interface)上、继承该接口(interface)并运行测试。

@ExtendWith(SpringExtension::class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
interface SpringBootTestBase

class GreetingControllerITest : SpringBootTestBase {

    @Autowired
    private lateinit var restTemplate: TestRestTemplate

    @Test
    fun `spring boot endpoint gets correct greeting`() {
        val body = restTemplate.getForObject("/greet/World", String::class.java)
        assertThat(body).isEqualTo("Hello, World!")
    }
}

由于 NPE 而失败

Caused by: java.lang.NullPointerException: null
    at org.springframework.boot.test.context.SpringBootTestContextCustomizer.customizeContext(SpringBootTestContextCustomizer.java:50) ~[spring-boot-test-2.0.0.M6.jar:2.0.0.M6]
    at org.springframework.boot.test.context.SpringBootContextLoader$ContextCustomizerAdapter.initialize(SpringBootContextLoader.java:326) ~[spring-boot-test-2.0.0.M6.jar:2.0.0.M6]
    at org.springframework.boot.SpringApplication.applyInitializers(SpringApplication.java:625) ~[spring-boot-2.0.0.M6.jar:2.0.0.M6]
    at org.springframework.boot.SpringApplication.prepareContext(SpringApplication.java:365) ~[spring-boot-2.0.0.M6.jar:2.0.0.M6]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:325) ~[spring-boot-2.0.0.M6.jar:2.0.0.M6]
    at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:138) ~[spring-boot-test-2.0.0.M6.jar:2.0.0.M6]
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99) ~[spring-test-5.0.1.RELEASE.jar:5.0.1.RELEASE]
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:117) ~[spring-test-5.0.1.RELEASE.jar:5.0.1.RELEASE]
    ... 61 common frames omitted

这个接口(interface)的原因是,将所有注释放在接口(interface)上,并让我们所有的测试继承该接口(interface) -> 避免注释代码重复

使用基类而不是接口(interface)对我来说并不是真正的选择,因为我还有其他与测试无关的基类要继承。

这是我应该为 Spring 社区带来的有用功能吗?

最佳答案

这是正确的:Spring Boot 目前不会在接口(interface)上搜索与测试相关的注释。

如果您觉得有用,请打开 GitHub issue对于 Spring Boot。

另一方面,如果您的主要目标是避免注释重复,那么这已经受到支持,因为核心 Spring 和 JUnit Jupiter 都支持组合注释

有关结合 Spring、Spring Boot 和 JUnit Jupiter 注释的具体示例,请查看 @SpringEventsWebTest我的spring-events中的注释示例项目。

问候,

Sam(Spring TestContext 框架的作者和核心 JUnit 5 提交者)

关于spring - 接口(interface)上的 @SpringBootTest 注释不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47556830/

相关文章:

java - 类必须在内部创建新对象,无法从单元测试中访问该对象

java - Junit5 断言导入

java - 从 POM 中的 Maven 配置文件进行 Spring Boot

java - 在tomcat上部署一个html文件,该文件在部署war后创建

java.lang.ClassNotFoundException : org. springframework.core.ResolvableTypeProvider 问题

java - Spring MVC 测试、JNDI

java - Spring 启动: Inject beans from tests into web environment

java - 尝试在Spring MVC Controller 中使用entityManager时出现NullPointerException

spring - @MockBeans 示例使用

java - 属性文件中的 @TestPropertySource 和 UTF-8 支持