搜索算法从包含测试的程序包开始工作,直到找到@SpringBootApplication或@SpringBootConfiguration注释的类.只要您以合理的方式对代码进行结构化,通常就可以找到您的主要配置.
The search algorithm works up from the package that contains the test until it finds a @SpringBootApplication or @SpringBootConfiguration annotated class. As long as you’ve structure your code in a sensible way your main configuration is usually found.
因此,在包层次结构中, @SpringBootApplication
类应高于测试类,例如,如果测试类位于包 com.zerosolutions.controller
中,则 @SpringBootApplication
类的包装应高于 com.zerosolutions.controller
软件包,即 com.zerosolutions
或 com
.
So the @SpringBootApplication
class should be higher in the package hierarchy than the test class e.g if test class is in package com.zerosolutions.controller
then @SpringBootApplication
class should be in a package higher than com.zerosolutions.controller
package i.e com.zerosolutions
or com
.
问题
但是如果 @SpringBootApplication
类与测试类处于同一级别,它将无法找到它,即 com.zerosolutions.general
.在这种情况下,您会收到以下错误:
But in case the @SpringBootApplication
class is at the same level as test class it won't be able to find it i.e com.zerosolutions.general
. In this case you'll get the following error:
java.lang.IllegalStateException:无法找到@SpringBootConfiguration,您需要在测试中使用@ContextConfiguration或@SpringBootTest(classes = ...)
java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
解决方案
如果您正在运行集成测试,则可以像这样明确地提及 @SpringBootApplication
类
If you are running an integrated test, you can explicitly mention the @SpringBootApplication
class like this
@RunWith(SpringRunner.class)
@SpringBootTest(classes={SpringBootApp.class})
但是,如果要对控制器进行单元测试,则无需启动整个Spring上下文.您可以将 @SpringBootTest
替换为 @WebMvcTest(MasterController.class)
.这将仅使用 MasterController
实例化Web层,而不是整个Spring上下文.
But if you want to do unit testing of a controller you don't need to fire up the whole Spring context. You can rather replace @SpringBootTest
with @WebMvcTest(MasterController.class)
. This will instantiate only the web layer with MasterController
and not the whole Spring context.
问题
但是问题是您将再次遇到我们之前遇到的错误:
But the problem is you will again run into the error we faced earlier:
java.lang.IllegalStateException:无法找到@SpringBootConfiguration,您需要在测试中使用@ContextConfiguration或@SpringBootTest(classes = ...)
java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
并且 @WebMvtTest
没有像 @SpringBootTest
这样的 classes
属性来明确提及 @SpringBootApplication
类.因此,有两种解决方案.
And @WebMvtTest
does not have a classes
attribute like @SpringBootTest
to explicitly mention the @SpringBootApplication
class.
So there are two solutions to this.
解决方案
第一:将您的应用程序类移至比测试类更高的包,即 com.zerosolutions
或 com
包.
First: Move your application class to a package higher than the test class i.e com.zerosolutions
or com
package.
第二:像下面这样明确地提及您的 @SpringBootApplication
类
Second: Mention your @SpringBootApplication
class explicitly like below
@RunWith(SpringRunner.class)
@WebMvcTest(MasterController.class)
@ContextConfiguration(classes={SpringBootApp.class})
希望可以消除Spring Test Configuration的混乱.谢谢
Hope that clears the Spring Test Configuration confusion. Thanks
这篇关于错误:为Spring Controller执行@WebMvcTest时找不到@SpringBootConfiguration的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!