i'm wonder that i can see this error(Could not autowire. No beans of 'MockMvc' type found. )
this is 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.test.web.servlet.MockMvc;
import static org.hamcrest.Matchers.containsString;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@WebMvcTest(HomeController.class)
public class HomeControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
public void testHomePage() throws Exception {
mockMvc.perform(get("/"))
.andExpect(status().isOk())
.andExpect(view().name("home"))
.andExpect(content().string(
containsString("Welcome to...")));
}
}
this test code run successfully. but mockMVC shows error about autowring.
how to delete this error?
please anyone help.
i'm using IntelliJ IDEA 2022.1.1 (Ultimate Edition), java, spring, junit5.
thanks
I had the same editor error with 2021.3.1 (Ultimate Edition) version. You can ignore that specific point of error adding @SuppressWarnings tag:
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@Autowired
private MockMvc mockMvc;
This can be generated too with editor assistance: Right click over the variable > Show context actions > Inspection 'Incorrect injection point autowiring in Spring bean components' options > Suppress for field
Another way is to update the editor. Currently i'm using 2022.2.2 and the error is not detected.