файл контроллера [файл LogController.java]:
@Api(value = "/")
@Path("/")
public class LogController
{
private Logger logger = LoggerFactory.getLogger(LogController.class);
private LoggerService loggerService = new LoggerService();
@GET
@Path("/getLogServerInfo")
public String getLogServerInfo(@Context HttpHeaders httpheaders) throws Exception {
//code
}
код модульного теста:
public class LogControllerTest
{
private MockMvc mockMvc;
@Mock
private LoggerServiceLua loggerServiceLua;
@InjectMocks
private LogController logController;
@Before
public void init(){
System.out.println("in here 1");
MockitoAnnotations.initMocks(this);
System.out.println("in here 1.1");
mockMvc = MockMvcBuilders
.standaloneSetup(logController)
.build();
System.out.println("in here 1 end");
}
@Test
public void testgetLogServerInfo() throws Exception
{
/*List<serviceinfo2> serviceinfo = Arrays.asList(
new serviceinfo2(1, "Daenerys Targaryen"),
new serviceinfo2(2, "John Snow"));*/
String serviceinfo = new String("{\"_id\":\"log_server\",\"_rev\":\"24-86181b008b62e31b2403852bf70fb8ce\",\"ip_address\":\"192.23.24.12\",\"port\":\"000\"}");
System.out.println(serviceinfo);
//serviceinfo2 serviceinfo2 = mock(serviceinfo2.class);
when(loggerServiceLua.getLogServerInfo()).thenReturn(serviceinfo);
System.out.println("in here 2");
mockMvc.perform(get("/getLogServerInfo"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON));
verify(loggerServiceLua, times(1)).getLogServerInfo();
verifyNoMoreInteractions(loggerServiceLua);
}
}
Мы используем фреймворк Maven + jersy для спокойной разработки сервиса, но maven: spring, mockito для модульного тестирования.
После выполнения выше я получаю
след ошибки:
java.lang.AssertionError: Ожидаемый статус: ‹200>, но был: ‹404> в org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:54) в org.springframework.test.util.AssertionErrors.assertEquals( AssertionErrors.java:81) в org.springframework.test.web.servlet.result.StatusResultMatchers$10.match(StatusResultMatchers.java:664) в org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java: 171) в Tests.LogControllerLuaTest.testgetLogServerInfo(LogControllerLuaTest.java:106) в sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) в sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) в sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43) в java.lang.reflect.Method.invoke(Method.java:498) в org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) в org.junit.internal .runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) в org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) в org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) в org.junit.internal.runners.statements .RunBefores.evaluate(RunBefores.java:26) в org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) в org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) в org.junit. runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) в org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) в org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) в org .junit.runners.ParentRunner.runChildren(ParentRunner.java:288) в org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) в org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) ) в org.junit.runners.ParentRunner.run(ParentRunner.java:363) в org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run( JUnit4TestReference.java:86) в org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) в org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java: 459) в org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678) в org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) в org. .eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Пожалуйста, прокомментируйте, если требуется какая-либо другая информация
@Path("/getLogServerInfo")
на@Path("getLogServerInfo")
в вашем контроллере. - person Eugene Kirin   schedule 11.05.2018LogControllerLuaTest
такое же, какLogControllerTest
? Я спрашиваю, потому что есть разница между вашим кодом и трассировкой стека. - person Planck Constant   schedule 11.05.2018