2017-11-29 17 views
0

アノテーションSpringBootTestをインタフェースに配置し、インタフェースから継承してテストを実行することは今は不可能です。@SpringBootTestアノテーションがインターフェイス上で動作しない

@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 

で失敗 - >注釈コードの重複を避けます

代わりにベースクラスを使用すると、テスト関連ではない他のベースクラスが継承されているため、インターフェイスは実際の選択肢ではありません。

これは春のコミュニティにもたらすべき便利な機能ですか?

答えて

2

これは間違いありません。現在のところ、Springブートはインターフェイス上のテスト関連の注釈を検索していません。

もしあなたが役に立つと思えば、スプリングブートのためにGitHub issueを開いてください。

一方、注釈の重複を避けることを主目的とする場合、SpringとJUnit Jupiterコアの両方がの注釈を含むをサポートしているため、すでにサポートされています。

Spring、Spring Boot、およびJUnit Jupiterの注釈を組み合わせる具体例については、spring-eventsサンプルプロジェクトの@SpringEventsWebTest注釈をご覧ください。

よろしく、

サム(春TestContextフレームワーク及びコアのJUnit 5コミッターの作者)

関連する問題