Spring Boot - MockMVC test not working
10,336
please try like this:
@RunWith(SpringRunner.class)
@WebMvcTest(WarehouseController.class)
public class WarehosueControllerTest {
@Autowired
private MockMvc mvc;
@MockBean
WarehouseService warehouseService;//the business service
@Test
public void testGetWarehouses() throws Exception{
List<Long> list = new ArrayList<>();
list.add(1L);
WarehouseDTO warehouseDTO = new WarehouseDTO();
List<WarehouseDTO> warehouseDTOs = new ArrayList<>();
warehouseDTO.setId(1L);
warehouseDTO.setCode("test_code");
warehouseDTO.setName("test_name");
warehouseDTO.setAddress("adress value");
warehouseDTOs.add(warehouseDTO);
given(this.warehouseService.getWarehouses(0, list))
.willReturn(warehouseDTOs);
this.mvc.perform(get("/api/warehouses?filterType=0&ids=1")
.header("Accept-Language", "en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4")
.accept(MediaType.APPLICATION_JSON_UTF8))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
//check if the jason node exsits
.andExpect(jsonPath("$.errorCode").exists())
.andExpect(jsonPath("$.payload[0].name").exists())
.andExpect(jsonPath("$.payload[0].code").exists())
.andExpect(jsonPath("$.payload[0].id").exists())
//check the type of json node
.andExpect(jsonPath("$.errorCode").isString())
.andExpect(jsonPath("$.payload[0].name").isString())
.andExpect(jsonPath("$.payload[0].code").isString())
.andExpect(jsonPath("$.payload[0].id").isNumber())
//check the return value
.andExpect(jsonPath("$.errorCode").value(""))
.andExpect(jsonPath("$.payload[0].name").value("test_name"))
.andExpect(jsonPath("$.payload[0].code").value("test_code"))
.andExpect(jsonPath("$.payload[0].address").value("adress value"))
.andExpect(jsonPath("$.payload[0].id").value(1));
}
}
Related videos on Youtube
Author by
Nunyet de Can Calçada
no puedor, pecador de la pradera ! Ese peasso de Nullpointer que sale de la pantalla y me dise: Quietoooooor, que esto peta mas que una escopeta de feria, por la Gloria de mi Madre !!!! no puedo, no puedo, no puedo Ese peasso de Scrum ceremonias !!!! y que has hechor ? y que haras ??? y que hacees ?? y yo que sé, for my Mother's glory !!!!!!!! 7 Nullpointers que vienen de Bonaaansaaaa
Updated on June 04, 2022Comments
-
Nunyet de Can Calçada almost 2 years
I have a basic SpringBoot app. using Spring Initializer, embedded Tomcat, Thymeleaf template engine, and package as an executable JAR file.
I have this controller:
@Controller @RequestMapping("/deviceevent") public class DeviceEventController { @RequestMapping(value={ "/list"}, method = { RequestMethod.GET}) public String deviceeventList() { return "tdk/deviceEvent/DeviceEventList"; } }
and this other test class. Tests using Spring's MockMVC framework. This drives an MVC application in a test, as if it was running in a container,
@RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration public class MockMvcTests { // Pull in the application context created by @ContextConfiguration @Autowired private WebApplicationContext wac; private MockMvc mockMvc; @Before public void setup() { // Setup MockMVC to use our Spring Configuration this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); } @Test public void getDeviceEventsTest() throws Exception { this.mockMvc .perform(get("/deviceevent/list") // .accept(MediaType.parseMediaType("text/html;charset=UTF-8"))) .andExpect(status().isOk()) // .andExpect(model().size(1)) // }
But I got this error:
java.lang.AssertionError: Status expected:<200> but was:<404> at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:54) at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:81) at org.springframework.test.web.servlet.result.StatusResultMatchers$10.match(StatusResultMatchers.java:664) at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:171) at com.tdk.iot.web.controllers.MockMvcTests.getDeviceEventsTest(MockMvcTests.java:66) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75) at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86) at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
-
Andy Wilkinson almost 7 yearsYour test doesn't look like a typical Spring Boot-based test. I'd expect to see
@SpringBootTest
or@WebMvcTest
. What version of Boot are you using? -
Nunyet de Can Calçada almost 7 years@AndyWilkinson WebMvcTest was the part missing !. , please convert to answer
-