-1

I am new to JUNIT testing but tried a lot to test the class UserProfileService. Please help or suggest possible errors I am doing:

@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();
}

I have written following JUNIT test:

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"));
}

}

Error:

java.lang.AssertionError: 
Unexpected method call      PartyProfileService.updateProfile(com.cts.vcoach.proxy.model.PartyProfile@11e170c):
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)
takendarkk
  • 3,347
  • 8
  • 25
  • 37
SRB
  • 123
  • 2
  • 9
  • Take a look at this answer http://stackoverflow.com/questions/24150017/how-can-i-test-if-this-object-returns-the-correct-string/24150270#24150270 – Federico Piazza Jun 11 '14 at 16:09
  • Possible duplicate of [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors) – Raedwald Jan 04 '16 at 13:38

1 Answers1

2

You're trying to test the class PartyProfileService. So you should create an instance of this class, and call its methods in your test. But that's not what you're doing. You're creating a mock instance of this class:

partyService = EasyMock.createMock(PartyProfileService.class);

Replace this line by

partyService = new PartyProfileService();

If you need to partially mock the service, i.e. mock the method activityPush(), but not the other ethods, then see the documentation for how to do that:

ToMock mock = createMockBuilder(ToMock.class)
                 .addMockedMethod("mockedMethod")
                 .createMock();
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255