[英]Autowired MockMvc is null with JUnit 5 and Spring boot
我有一個 class 測試,當我運行測試時會向我發送一個錯誤。 我關注了幾個線程,並且我有正確的導入“import org.junit.jupiter.api.Test”所以我不明白為什么它會給我這個錯誤:
無法調用“org.springframework.test.web.servlet.MockMvc.perform(org.springframework.test.web.servlet.RequestBuilder)”因為“this.mockMvc”是Z37AZABD78664
我的代碼:
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 配置:
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"
編輯:我找到了解決方案。 我的 gradle 文件沒有這個依賴:
testImplementation "org.springframework.boot:spring-boot-starter-test:2.7.2"
嘗試
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"));
}
}
使用這些注釋:
@RunWith(SpringRunner.class)
@AutoConfigureMockMvc
@ExtendWith({RestDocumentationExtension.class, SpringExtension.class})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.