0

I have a client class that looks like the following:

class MyClient{
    Service service;

    public MyClient(Service service){ ... }

    public boolean CoreFunction(Object arg1, Object arg2){
        Service.AnotherClass instance = new Service.AnotherClass(arg1, arg2);
        service.call(instance);
        if(instance.isSuccessful()) { ...; return true; }
        else { ...; return false; }
    }
}

The instance's isSuccessful() returns a flag that will be upaded by the service.call() internally. I want to test the logic if instance.isSuccessful() is true and false. How can I use EasyMock to achieve this? Thank you.

Steve
  • 4,935
  • 11
  • 56
  • 83

1 Answers1

0

You can use IAnswer to intercept service call and set successful property of your instance:

Service service = EasyMock.createMock(Service.class);

// andAnswer style
service.call(anyObject(Service.AnotherClass.class));
expectLastCall().andAnswer(new IAnswer<Object>() {
    public Object answer() throws Throwable {
        Service.AnotherClass arg = (Service.AnotherClass) EasyMock.getCurrentArguments()[0];
        arg.setSuccessful(true); // or false in another test
        return null;
    }
});
hoaz
  • 9,883
  • 4
  • 42
  • 53