@WebServlet(urlPatterns = "/test")
public class TestWebServlet extends HttpServlet{
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setStatus(200);
resp.getWriter().println("test");
}
}
Hauptklasse:Warum kann MockMvc nicht testen @WebServlet im Frühjahr-boot
@SpringBootApplication
@ServletComponentScan
public class Demo20Application {
public static void main(String[] args) {
SpringApplication.run(Demo20Application.class, args);
}
}
Testcode:
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class Demo20ApplicationTests {
@Autowired
private MockMvc mockMvc;
@Test
public void testMockMvc() throws Exception {
this.mockMvc.perform(post("/test"))
.andExpect(status().isOk());
}
}
java.lang.AssertionError: Status
Expected :200
Actual :404
das Servlet einen 200-Statuscode aber Prüfergebnis zeigt mir den Status ist 404
Es scheint, dass der MockMvc nicht verwendet werden kann, um Servlets im Springboot
zu testen