2014-06-11 54 views
-1

我是JUNIT測試的新手,但嘗試了很多來測試UserProfileService類。請幫助或建議可能的錯誤我做:基本JUNIT測試。我得到java.lang.AssertionError

@Path("/update") 
@Consumes(MediaType.APPLICATION_JSON) 
@Produces(MediaType.APPLICATION_JSON) 
public Response updateProfile(final PartyProfile partyProfile) { 
    final ProxyResponse response = new ProxyResponse(); 
    try { 
     final boolean isUpdated = partyProfileDao.updateProfile(partyProfile); 
     if (isUpdated) { 
      LOGGER.info("update Request completed successfully"); 
      setSuccessResponse(response, "Request completed successfully"); 
      try{ 
      final String trailId = partyProfileDao.fetchPartyTrial(partyProfile.getPartyId()); 
      activityPush(trailId, partyProfile.getPartyId()); 
      }catch(Exception ex){ 
       ex.printStackTrace(); 
       LOGGER.error("Exception while triggering segmentation++"); 
      } 
     } else { 
      LOGGER.info("Unable to update profile"); 
      setErrorResponse(response, "PRFUPDT500", "Unable to update profile"); 
      return Response.ok(response).header("valid", FLAG_FALSE).build(); 
     } 




    } catch (Exception exc) { 
     LOGGER.error("Unable to update profile:" + exc.getMessage()); 
     setInternalErrorResponse(exc, response, "Unable to update profile"); 
     return Response.ok(response).header("valid", FLAG_FALSE).build(); 
    } 
    return Response.ok(response).build(); 
} 

我已經寫了下面的JUnit測試:

public class PartyProfileServiceTest { 

private PartyProfileDAO partyDAO; 
private PartyProfileService partyService; 

@Before 
public void setUp() throws Exception { 

    partyDAO = EasyMock.createMock(PartyProfileDAO.class); 
    partyService = EasyMock.createMock(PartyProfileService.class); 
    partyService.setUserProfileDAO(partyDAO); 
} 

@Test 
public void testUpdateValidProfile() throws Exception { 
    System.out.println("inside junit"); 

    PartyProfile partyProfile = new PartyProfile(); 

    partyProfile.setFirstName("adsfsdfg"); 
    partyProfile.setAddress("adressabc"); 
    partyProfile.setPartyId("dfdf"); 
    partyProfile.getSalutation(); 
    partyProfile.setMiddleName("asfsaf"); 
    partyProfile.setLastName("easdsddff"); 
    partyProfile.setNickName("srb"); 
    partyProfile.setGender("m"); 
    partyProfile.setUpdateDate("sdd"); 
    partyProfile.setEducation("srg"); 
    partyProfile.setInsurance("sg"); 
    partyProfile.setState("sdfg"); 
    partyProfile.setSiteId("fdg"); 
    partyProfile.setRandomNo("feddg"); 
    partyProfile.setPartyId("56666"); 


    EasyMock.expect(partyDAO.updateProfile(partyProfile)).andReturn(true); 
    EasyMock.expect(partyDAO.fetchPartyTrial("validPartyId")).andReturn("pass"); 
    EasyMock.replay(partyDAO); 
    EasyMock.expect(partyService.activityPush("pass", "validPartyId")).andReturn(true); 
    EasyMock.replay(partyService); 

    Response response = partyService.updateProfile(partyProfile); 

    ProxyResponse proxyResponse = (ProxyResponse) response.getEntity(); 
    Assert.assertSame("Unable to update profile", proxyResponse.getData().get("message")); 
} 

}

錯誤:

java.lang.AssertionError: 
Unexpected method call  PartyProfileService.updateProfile([email protected]): 
PartyProfileService.setUserProfileDAO(EasyMock for class com.cts.vcoach.proxy.dao.PartyProfileDAO): expected: 1, actual: 0 
PartyProfileService.activityPush("pass", "validPartyId"): expected: 1, actual: 0 
at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:44) 
at org.easymock.internal.ObjectMethodsFilter.invoke(ObjectMethodsFilter.java:94) 
at org.easymock.internal.ClassProxyFactory$MockMethodInterceptor.intercept(ClassProxyFactory.java:97) 
at com.cts.vcoach.proxy.service.rest.PartyProfileService$$EnhancerByCGLIB$$46a1112f.updateProfile(<generated>) 
at com.cts.vcoach.proxy.service.rest.PartyProfileServiceTest.testUpdateValidProfile(PartyProfileServiceTest.java:59) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:606) 
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) 
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) 
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) 
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) 
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:44) 
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180) 
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41) 
at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173) 
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) 
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:220) 
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 
+0

告訴我們你的嘲笑。 –

+0

看看這個答案http://stackoverflow.com/questions/24150017/how-can-i-test-if-this-object-returns-the-correct-string/24150270#24150270 –

+0

可能重複[什麼是一個堆棧跟蹤,我如何使用它來調試我的應用程序錯誤?](http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use -IT調試的,我的應用程序,錯誤) – Raedwald

回答

2

你試圖測試PartyProfileService類。所以你應該創建這個類的一個實例,並在你的測試中調用它的方法。但那不是你在做什麼。你創建這個類的一個實例模擬:

partyService = EasyMock.createMock(PartyProfileService.class); 

通過

partyService = new PartyProfileService(); 

如果需要部分地嘲笑服務,即嘲笑的方法activityPush(),而不是其他編制方法替換該行,然後看到the documentation如何做到這一點:

ToMock mock = createMockBuilder(ToMock.class) 
       .addMockedMethod("mockedMethod") 
       .createMock();