基本的JUNIT测试。我收到了java.lang.AssertionError

时间:2014-06-11 16:04:36

标签: java spring spring-mvc junit

我是JUNIT测试的新手,但尝试了很多测试类UserProfileService。请帮助或建议我正在做的可能的错误:

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

1 个答案:

答案 0 :(得分:2)

您正在尝试测试PartyProfileService类。因此,您应该创建此类的实例,并在测试中调用其方法。但那不是你正在做的事情。您正在创建此类的模拟实例:

partyService = EasyMock.createMock(PartyProfileService.class);

替换此行
partyService = new PartyProfileService();

如果您需要部分模拟服务,即模拟方法activityPush(),而不是其他方法,请参阅the documentation了解如何执行此操作:

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