Error: Unable to find @SpringBootConfiguration when doing @WebMvcTest for Spring Controller

Error: Unable to find @SpringBootConfiguration when doing @WebMvcTest for Spring Controller

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

@Controller
public class MasterController {

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

我正在按照 this Spring 文档来测试我的控制器。 现在,我想通过实例化 Web 层而不是文档中给出的整个 Spring 上下文来测试我的控制器。下面是我的代码。

@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"));
}

}

当我 运行 这个测试时,我得到错误 Unable to find @SpringBootConfiguration,...etc。但是我很困惑为什么当我们不希望它实例化它而只想使用 web 层时它要求 Spring 配置。请指出这里发生的事情的正确方向。以及如何解决这个问题。谢谢

所以这里是解决方案:

documentation on detecting test configuration 说:

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 class 在包层次结构中应该比测试 class 更高,例如,如果测试 class 在包 com.zerosolutions.controller 中,那么 @SpringBootApplication class 应该在高于 com.zerosolutions.controller 的包中,即 com.zerosolutionscom.

问题

但是如果 @SpringBootApplication class 与测试 class 处于同一级别,它将无法找到它,即 com.zerosolutions.general。在这种情况下,您将收到以下错误:

java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test

解决方案

如果您正在 运行进行综合测试,您可以像这样明确提及 @SpringBootApplication class

@RunWith(SpringRunner.class)
@SpringBootTest(classes={SpringBootApp.class})

但是如果您想对控制器进行单元测试,则不需要启动整个 Spring 上下文。您可以将 @SpringBootTest 替换为 @WebMvcTest(MasterController.class)。这将仅实例化具有 MasterController 的 Web 层,而不实例化整个 Spring 上下文。

问题

但问题是您将再次 运行 进入我们之前遇到的错误:

java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test

并且 @WebMvtTest 没有像 @SpringBootTest 这样的 classes 属性来明确提及 @SpringBootApplication class。 所以有两种解决方法。

解决方案

首先:将您的应用class移动到高于测试class的包,即com.zerosolutionscom包.

第二个:明确提及您的@SpringBootApplication class,如下所示

@RunWith(SpringRunner.class)
@WebMvcTest(MasterController.class)
@ContextConfiguration(classes={SpringBootApp.class})

希望消除 Spring 测试配置的混乱。谢谢

我遇到了同样的错误,发现当我生成项目时,我的 pom.xml 将我的组 ID 显示为 com.example 而不是我的实际域:
<groupId>com.example</groupId>
我将 pom.xml 更正为:
<groupId>com.mydomain</groupId>
接下来,我将文件结构更改为:
src/test/java/com/examplesrc/test/java/com/mydomain
最后,我不得不更新 my
中的包声明 SampleProjectApplicationTest.java
文件以匹配正确的文件结构。一切就绪后,测试工作正常。

我不确定我是如何以 com.example 结束我的项目的其余部分是正确的,但在我的情况下修复就这么简单。

希望这对某人有所帮助。

如果您的 Application.java class(在 src/main/java 中)位于

com.A.B

你的测试class ApplicationTest.java (in src/test/java) 需要低于

com.A.Bcom.A.B.Ccom.A.B.C.D

如果测试 class 位于以下包下,您将收到此错误消息

com.Acom.A.Ccom.A.D

在Spring开机 一般规则是 TEST CLASS 软件包名称需要以 JAVA CLASS 将要测试的软件包的软件包名称开头

检查 src/test/java 是否与主 class 包名称相同。 src/test/java/com/example/abc 与 src/test/java/com/example/abc

相同

只需将测试包名称替换为主要应用程序 class 包名称即可。

示例:- package com.kotlin.dealerMainApplication 这是我的主要应用程序包名称,所以我将在我的测试包中使用相同的名称

这对我有用!