重用测试对象中的数据

时间:2019-07-16 13:06:13

标签: java mockito

我有测试

 private DataJpa myObject = new DataJpa();
 private DaoService daoService = mock(DaoService.class);
 private RestServiceImpl service = spy(RestServiceImpl.class);

 @Before // I've tried @BeforeClass too
 public void init(){

   service.setDaoService(daoService);
   when(daoService.selectObject(anyString())).thenReturn(myObject);
}

@Test
public void test1(){

   Response registerFirstResponse = service.registerDevice(serialNumber, token, registrationDeviceRequest);

   createMyObjectJpa(field1,field2,field3);

   Response registerSecondResponse = service.registerDevice(serialNumber, token, registrationDeviceRequest);

   assertEquals(Response.Status.CREATED.getStatusCode(), registerFirstResponse.getStatus());
   assertEquals(Response.Status.OK.getStatusCode(), registerSecondResponse.getStatus());
}

 private void createJpa(field1,field2,field3){
        myObject.setDeviceId(field1);
        myObject.setPushToken(field2); 
}

还有我的registerDevice中的service方法

 public Response registerDevice(String serialNumber, String token, String registrationDeviceRequest){
    DataJpa jpa=daoService.selectObject(serialNumber);
        if(jpa==null){
           //here I must fulfill my jpa 
            dataJpa = StaticUtils.createJpa(field1,field2,field3);
            return Response.status(Response.Status.CREATED).build();
        }else{
             return Response.status(Response.Status.OK).build();
        }
}

因此,在第一次调用doMyMethod之后,我的myObject完成了testField之类的一些数据,但是在第二次调用中,我希望myObject已经有该数据,但是我得到了null中的myObject,它又实现了。

我需要测试,如果其中包含一些数据,它会返回关于myObject的响应。

如何在第二次调用之前将不同的字段保存到myObject中?

编辑

如何测试我的问题。当您尝试在responseSecondClass1语句的Class2.testClass()内调用if时,您将获得null,但我需要将其作为fulfiled对象;

public class Class1 {

    String field1;
    String field2;
    String field3;

    public Class1() {
    }

    public Class1(String field1, String field2, String field3) {
        this.field1 = field1;
        this.field2 = field2;
        this.field3 = field3;
    }

}

public class Class2 {

    public Response testClass() {

        Class1 class1 = getMyObjectFromMethod();

        if (class1 == null) {
            return Response.status(Response.Status.OK).build();
        } else {
            return Response.status(Response.Status.NOT_MODIFIED).build();
        }
    }


    public Class1 getMyObjectFromMethod() {

        Class1 class1 = new Class1("field1", "field2", "field3");
        return class1;
    }
}

@Test
    public void test1() {
        Class1 class1 = new Class1();
        Class2 class2 = spy(Class2.class);
        when(class2.getMyObjectFromMethod()).thenReturn(class1);

        Response responseFirstClass1 = class2.testClass();

        class1 = fulfilClass();
        Response responseSecondClass1 = class2.testClass();

        assertEquals(Response.Status.OK.getStatusCode(), responseFirstClass1.getStatus());
        assertEquals(Response.Status.NOT_MODIFIED.getStatusCode(), responseSecondClass1.getStatus());

    }

    private Class1 fulfilClass() {
    Class1 class1 = new Class1("field1", "field2", "field3");
    return class1;
}

2 个答案:

答案 0 :(得分:0)

由于您在同一测试中两次致电doMyMethod,因此我认为您应该明确告诉Mockito在第二次致电中返回什么。

类似的东西:

when(myClass.doMyMethod(any()))
  .thenReturn(myObject)
  .thenReturn(myObject);  

如果您需要不同的答复,则可以执行以下操作:

 when(myClass.doMyMethod(any()))
  .thenReturn(myObject1)
  .thenReturn(myObject2);  

甚至也许

when(myClass.doMyMethod(any()))
  .thenReturn(myObject)
  .thenThrow(new MyException(...));  

答案 1 :(得分:0)

它需要改用doAnswer

when(daoService.selectObject(anyString())).thenAnswer(new Answer<DataJpa>() { 
    @Override 
    public DataJpa answer(InvocationOnMock iom) throws Throwable { 
        return myObject; 
    } 
});

或者可以使用类似的东西

when(daoService.selectObject(anyString())).thenReturn(myObject);
Response registerFirstResponse = service.registerDevice(serialNumber, token, registrationDeviceRequest);

   createMyObjectJpa(field1,field2,field3);
   when(daoService.selectObject(anyString())).thenReturn(myObject); // here `myObject` was changed and now I say to Mockito, I want my new `myObject`

   Response registerSecondResponse = service.registerDevice(serialNumber, token, registrationDeviceRequest);