1

I have the following code on my Android application:

public Observable<Person> execute(MyObject myObject) {
    return service.execute(new MyApiRequest(myObject));
}

What I want to test, using Mockito is that the same instance of MyObject is passed to MyApiRequest constructor. Let's say we don't know how MyApiRequest looks. I only want to test that there "myObject" param from execute() method is the same as MyApiRequest recieves.

mbob
  • 590
  • 1
  • 4
  • 22
  • 2
    Since you're passing `myObject` as a parameter to MyApiRequest, obviously "myObject" param from execute() method will be the same as MyApiRequest recieves – Jay Ghosh Sep 21 '16 at 12:36
  • yeah, but what if I change the code to `return service.execute(new MyApiRequest(new MyObject()));` ? – mbob Sep 21 '16 at 12:39
  • I don't know what you mean. http://stackoverflow.com/questions/13387742/compare-two-objects-with-equals-and-operator. You might find this helpful – Jay Ghosh Sep 21 '16 at 12:41
  • I wonder if I can use `ArgumentCaptor` somehow – mbob Sep 21 '16 at 12:43

1 Answers1

0

Yes, you can use an ArgumentCaptor to inspect the MyApiRequest that comes back.

// Create the object and the captor.
ArgumentCaptor<MyApiRequest> myApiRequestCaptor =
    ArgumentCaptor.forClass(MyApiRequest.class);
MyObject myObject = new MyObject();

// Run the test.
systemUnderTest.execute(myObject);

// Capture the MyApiRequest.
verify(service).execute(myApiRequestCaptor.capture());
MyApiRequest myApiRequest = myApiRequestCaptor.getValue();

// Now check the object identity.
assertSame("MyApiRequest should have the exact same passed-in object.",
    myObject, myApiRequest.getParameter());
Jeff Bowman
  • 90,959
  • 16
  • 217
  • 251