简体   繁体   English

Autowired MockMvc 是 null 与 JUnit 5 和 Spring 启动

[英]Autowired MockMvc is null with JUnit 5 and Spring boot

I have a class test which send me an error when I run the test.我有一个 class 测试,当我运行测试时会向我发送一个错误。 I followed several threads and I have the right import "import org.junit.jupiter.api.Test" So I don't understand why it sends me this error:我关注了几个线程,并且我有正确的导入“import org.junit.jupiter.api.Test”所以我不明白为什么它会给我这个错误:

Cannot invoke "org.springframework.test.web.servlet.MockMvc.perform(org.springframework.test.web.servlet.RequestBuilder)" because "this.mockMvc" is null无法调用“org.springframework.test.web.servlet.MockMvc.perform(org.springframework.test.web.servlet.RequestBuilder)”因为“this.mockMvc”是Z37AZABD78664

My code:我的代码:

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@WebMvcTest(TestController.class)
public class ControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private CreateMessageProvider createMessageProvider;

    @Test
    public void test() throws Exception {
        this.mockMvc.perform(get("/test"))
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(content().string("OK"));
    }
}

Gradle config: Gradle 配置:

        mockitoCoreVersion = '4.6.1'
        mockitoJunitJupiterVersion = '4.6.1'
        springBootTestVersion = '2.7.2'
        springTestVersion = '5.3.22'

    testImplementation "org.springframework.boot:spring-boot-test:$springBootTestVersion"
    testImplementation "org.springframework:spring-test:$springTestVersion"
    testImplementation "org.mockito:mockito-junit-jupiter:$mockitoJunitJupiterVersion"
    testImplementation "org.mockito:mockito-core:$mockitoCoreVersion"

EDIT: I found the solution.编辑:我找到了解决方案。 My gradle file didn't have this dependency:我的 gradle 文件没有这个依赖:

testImplementation "org.springframework.boot:spring-boot-starter-test:2.7.2"

Try尝试

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@AutoConfigureMockMvc
@SpringBootTest
public class ControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private CreateMessageProvider createMessageProvider;

    @Test
    public void test() throws Exception {
        this.mockMvc.perform(get("/test"))
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(content().string("OK"));
    }
}

Use these annotations:使用这些注释:

@RunWith(SpringRunner.class)
@AutoConfigureMockMvc
@ExtendWith({RestDocumentationExtension.class, SpringExtension.class})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM