@interfrace上的SpringBootTest批注不工作

@interfrace上的SpringBootTest批注不工作,spring,spring-test,junit5,Spring,Spring Test,Junit5,现在不可能将注释springbootest放在接口上,从接口继承并运行测试 @ExtendWith(SpringExtension::class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) interface SpringBootTestBase class GreetingControllerITest : SpringBootTestBase { @Autowired

现在不可能将注释
springbootest
放在接口上,从接口继承并运行测试

@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
使用此接口的原因是,将所有注释放在接口上,并让我们的所有测试继承自该接口->避免注释代码重复

使用基类代替接口对我来说不是真正的选择,因为我有其他基类可以继承,这些基类与测试无关


这是我应该给spring社区带来的一个有用的特性吗?

没错:spring Boot目前没有在接口上搜索与测试相关的注释

如果您觉得这将是有用的,那么请打开一个弹簧启动

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

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

问候,

Sam(SpringTestContext框架和核心JUnit5提交者的作者)