在Spring Boot 1.4 中排除@WebMvcTest 时的配置
Exclude a configuration when @WebMvcTest in Spring Boot 1.4
我在 java/
目录(同一个包)中有一个 @SpringBootApplication class,在 test/
中有另一个 @SpringBootApplication class 用于模拟一些自动连接的 bean。有几个测试,使用的配置因测试而异。
并在测试中class
@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]]
我只是想测试控制器的路由。
如何设置特定的应用程序配置?
同一包中不能有两个 @SpringBootConfiguration
(@SpringBootApplication
)。 @WebMvcTest
自动为您搜索配置源(参见 the doc)。如果你想调整东西但你不能在同一个包中有两个,你可以在测试的嵌套包中有一个特殊的 @SpringBootConfiguration
(或应用程序)。
我不确定文档对此是否非常明确,因此我们可能应该对其进行澄清。
无论如何,自定义 @SpringBootApplication
和切片有点奇怪。 @SpringMvcTest
已经负责只启用必要的功能。如果你想模拟一些 beans,你应该 而不是 在 @SpringBootApplication
中定义它。您导入的常规 @Configuration
没问题。我们还有 @MockBean
可以自动为您模拟。
我在 java/
目录(同一个包)中有一个 @SpringBootApplication class,在 test/
中有另一个 @SpringBootApplication class 用于模拟一些自动连接的 bean。有几个测试,使用的配置因测试而异。
并在测试中class
@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]]
我只是想测试控制器的路由。
如何设置特定的应用程序配置?
同一包中不能有两个 @SpringBootConfiguration
(@SpringBootApplication
)。 @WebMvcTest
自动为您搜索配置源(参见 the doc)。如果你想调整东西但你不能在同一个包中有两个,你可以在测试的嵌套包中有一个特殊的 @SpringBootConfiguration
(或应用程序)。
我不确定文档对此是否非常明确,因此我们可能应该对其进行澄清。
无论如何,自定义 @SpringBootApplication
和切片有点奇怪。 @SpringMvcTest
已经负责只启用必要的功能。如果你想模拟一些 beans,你应该 而不是 在 @SpringBootApplication
中定义它。您导入的常规 @Configuration
没问题。我们还有 @MockBean
可以自动为您模拟。