我是春季启动单元测试的新手。我正在尝试测试一项服务,并为此使用了 Mockito。当我执行 mvc.perform() 时,我得到一个空值。我使用 when() 来指定要返回的内容。但不知道为什么会失败。请提供一些有价值的意见。以下是 fe 片段:
@Test
public void testAll() throws Exception {
/*Mockito.when(sensorDao.findAll()).thenReturn((Iterable<Sensor>) s2);
assertEquals(200,expect().statusCode(200));*/
Collection<Sensor>list = getEntityListStubData();
when(sensorserviceimpl.findAll()).thenReturn(list);
when(sensorService.findAll()).thenReturn(list);
when(sensorDao.findAll()).thenReturn(list);
String uri="/sensor";
//System.out.print("gfnjhkjjhhg");
System.out.println("Helllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllloooooooooooooooooooooooooooooooooooooooooooo");
System.out.println(MockMvcRequestBuilders.get(uri).accept(MediaType.APPLICATION_JSON));
MvcResult result = mvc.perform(MockMvcRequestBuilders.get(uri).accept(MediaType.APPLICATION_JSON)).andReturn();
String content = result.getResponse().getContentAsString();
int status = result.getResponse().getStatus();
}
Run Code Online (Sandbox Code Playgroud)
这是输出:
2017-08-18 19:11:45.030 INFO 21424 --- [ main] o.s.t.c.transaction.TransactionContext : Rolled back transaction for test context [DefaultTestContext@35083305 testClass = SensorServiceStandaloneApplicationTests, testInstance = com.bny.sr.sensorservicestandalone.SensorServiceStandaloneApplicationTests@482cd91f, testMethod = testAll@SensorServiceStandaloneApplicationTests, testException = java.lang.NullPointerException, mergedContextConfiguration = [WebMergedContextConfiguration@8e0379d testClass = SensorServiceStandaloneApplicationTests, locations = '{}', …Run Code Online (Sandbox Code Playgroud) unit-testing nullpointerexception mockito spring-boot mockmvc