controller file[LogController.java file]:
@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
}
unit test 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);
}
}
We are using Maven+ jersy framework for restful service Developement but maven:spring,mockito for unit test .
After executing above ,I am getting
error trace:
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 Tests.LogControllerLuaTest.testgetLogServerInfo(LogControllerLuaTest.java:106) 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.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 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.junit.runners.ParentRunner.run(ParentRunner.java:363) 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)
Please comment if any other inf is required