2016-12-20 85 views
0

我在java/目錄(同一個包)中有一個@SpringBootApplication類,test/中有另一個@SpringBootApplication類用於模擬一些自動連接的bean。有幾種測試,使用哪種配置因測試而異。在Spring Boot 1.4中的@WebMvcTest時排除配置

而且在測試類

@RunWith(SpringRunner.class) 
@WebMvcTest(RecApiServerController.class) 

拋出

java.lang.IllegalStateException: Found multiple @SpringBootConfiguration annotated classes [Generic bean: class [com.xxx.MockedTestConfig]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [/..direction.../target/test-classes/com/xxx/MockedTestConfig.class], Generic bean: class [com.xxx.MyApplication]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [/...direction.../target/classes/com/xxx/MyApplication.class]] 

我只是想測試控制器的路由。

我該如何設置特定的應用程序配置?

回答

2

在同一個包中不能有兩個@SpringBootConfiguration@SpringBootApplication)。 @WebMvcTest會自動搜索配置源(請參閱the doc)。如果您想調整某些東西,但是不能在同一個包中包含兩個,則可以在測試的嵌套包中包含特殊的@SpringBootConfiguration(或應用程序)。

我不確定該文件是非常明確的,所以我們應該澄清它。

無論如何,自定義@SpringBootApplication和切片有點奇怪。 @SpringMvcTest已經只處理了必要的功能。如果你想嘲笑一些豆,你應該而不是定義在@SpringBootApplication。您導入的常規@Configuration很好。我們也有@MockBean自動爲你嘲笑事情。

+0

我在@SpringBootTest中將'MockedTestConfig.class'更改爲常規@Configuration並添加'classes = {MyApplication.class,MockedTestConfig.class}',並且所有測試都正常工作。他們之前沒有,所以我將它改爲@SpringBootApplication。我認爲那個時候'class'中的順序是錯誤的。謝謝。 :) – margincall

相關問題