错误:为Spring Controller执行@WebMvcTest时找不到@SpringBootConfiguration [英] Error: Unable to find @SpringBootConfiguration when doing @WebMvcTest for Spring Controller

查看:188
本文介绍了错误:为Spring Controller执行@WebMvcTest时找不到@SpringBootConfiguration的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试下面给出的控制器

I am testing my controller given below

@Controller
public class MasterController {

@GetMapping("/")
public String goLoginPage(){
    return "index";
}
}

我正在使用 Spring文档来测试我的控制器.现在,我想通过仅实例化Web层而不是文档中给出的整个Spring上下文来测试我的控制器.下面是我的相同代码.

I am following this Spring documentation to test my controller. Now, I want to test my controller by just instantiating the web layer and not the whole Spring context as given in the documentation. Below is my code for the same.

@RunWith(SpringRunner.class)
@WebMvcTest
public class MasterControllerTestWithWebLayer {

@Autowired
MockMvc mockMvc;

@Autowired
MasterController masterController;


@Before
public void setUp() throws Exception {
}

@After
public void tearDown() throws Exception {
}

@Test
public void testLoginHome() throws Exception{
    mockMvc.perform(get("/"))
    .andExpect(status().isOk())
    .andExpect(view().name("index"));
}

}

运行此测试时,出现错误无法找到@SpringBootConfiguration,...等.但是我很困惑为什么当我们不想实例化它而只想使用Web层时却要求Spring配置.请指出正确的方向.以及如何解决此问题.谢谢

When I run this test I get the error Unable to find @SpringBootConfiguration,...etc. But I am confused why it is asking for Spring configuration when we do not want it to instantiate it but want to use only the web layer. Kindly point me to the right direction what is happening here. And also how to fix this. Thanks

推荐答案

这是解决方案:

搜索算法从包含测试的程序包开始工作,直到找到@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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆