[英]Spring Boot MVC Test - MockMvc is always null
I'm trying to write my first Spring MVC test but I just cannot get Spring Boot to inject the MockMvc dependency into my test class. Here is my class:我正在尝试编写我的第一个 Spring MVC 测试,但我无法获得 Spring Boot 以将 MockMvc 依赖项注入我的测试 class。这是我的 class:
@WebMvcTest
public class WhyWontThisWorkTest {
private static final String myUri = "uri";
private static final String jsonFileName = "myRequestBody.json";
@Autowired
private MockMvc mockMvc;
@Test
public void iMustBeMissingSomething() throws Exception {
byte[] jsonFile = Files.readAllBytes(Paths.get("src/test/resources/" + jsonFileName));
mockMvc.perform(
MockMvcRequestBuilders.post(myUri)
.content(jsonFile)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().is2xxSuccessful());
}
}
I've checked with IntelliJ's debugger and can confirm that mockMvc itself is null. Thus, all the Exception message tells me is "java.lang.NullPointerException".我已经检查过 IntelliJ 的调试器,可以确认 mockMvc 本身是 null。因此,所有异常消息告诉我的是“java.lang.NullPointerException”。
I've already tried adding more general Spring Boot annotations for test classes like "@SpringBootTest" or "@RunWith(SpringRunner.class)" in case it has something to do with initializing Spring but no luck.我已经尝试为“@SpringBootTest”或“@RunWith(SpringRunner.class)”等测试类添加更通用的 Spring 引导注释,以防它与初始化 Spring 有关但没有成功。
Strange, provided that you have also tried with @RunWith(SpringRunner.class)
and @SpringBootTest
.奇怪,前提是您也尝试过
@RunWith(SpringRunner.class)
和@SpringBootTest
。 Have you also tried with the @AutoConfigureMockMvc
annotation?您是否也尝试过使用
@AutoConfigureMockMvc
注释? The sample below is working fine.下面的示例工作正常。
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class HelloControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
public void getHello() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string(equalTo("Hello World of Spring Boot")));
}
}
It may also be worthwhile to consider the following comments regarding the usage of the @WebMvcTest and @AutoConfigureMockMvc annotations as detailed in Spring's documentation考虑以下有关 @WebMvcTest 和 @AutoConfigureMockMvc 注释的使用的评论也是值得的,如Spring 文档中所述
By default, tests annotated with @WebMvcTest will also auto-configure Spring Security and MockMvc (include support for HtmlUnit WebClient and Selenium WebDriver).
默认情况下,使用@WebMvcTest 注释的测试还将自动配置 Spring Security 和 MockMvc(包括对 HtmlUnit WebClient 和 Selenium WebDriver 的支持)。 For more fine-grained control of MockMVC the @AutoConfigureMockMvc annotation can be used.
为了对 MockMVC 进行更细粒度的控制,可以使用 @AutoConfigureMockMvc 注释。
Typically @WebMvcTest is used in combination with @MockBean or @Import to create any collaborators required by your @Controller beans.
通常@WebMvcTest 与@MockBean 或@Import 结合使用来创建@Controller bean 所需的任何协作者。
If you are looking to load your full application configuration and use MockMVC, you should consider @SpringBootTest combined with @AutoConfigureMockMvc rather than this annotation.
如果您希望加载完整的应用程序配置并使用 MockMVC,则应考虑将 @SpringBootTest 与 @AutoConfigureMockMvc 结合使用,而不是此注释。
When using JUnit 4, this annotation should be used in combination with @RunWith(SpringRunner.class).
使用 JUnit 4 时,此注解应与 @RunWith(SpringRunner.class) 结合使用。
I faced the same issue.我遇到了同样的问题。 It turned out that
MockMvc
was not injected due to incompatible @Test
annotation .事实证明,由于
@Test
注释不兼容, MockMvc
没有被注入。 The import was org.junit.Test
, but changing it to org.junit.jupiter.api.Test
solved the issue.导入是
org.junit.Test
,但将其更改为org.junit.jupiter.api.Test
解决了该问题。
Accepted answer works, however I solved the issue also without importing @RunWith(SpringRunner.class)
.接受的答案有效,但是我在没有导入
@RunWith(SpringRunner.class)
情况下也解决了这个问题。
In my case I had imported org.junit.Test
instead of the newer org.junit.jupiter.api.Test
.就我而言,我导入了
org.junit.Test
而不是较新的org.junit.jupiter.api.Test
。
If you are using Maven you can avoid to make this mistake excluding junit-vintage-engine
from spring-boot-starter-test
dependency:如果您使用 Maven,则可以避免犯此错误,从
spring-boot-starter-test
依赖项中排除junit-vintage-engine
:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
check if you have the latest or at least updated maven-surefire-plugin
.检查您是否有最新的或至少更新
maven-surefire-plugin
。 It helped me to solve the issue它帮助我解决了这个问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.