سؤال

I would like to verify if a parameter of a mocked object is correctly mapped (before the methodcall there is a mapping). I've created two objects, one of which has the expected values. I expect the false one to fail, but somehow easymock does not verify if the parameter contains the expected values. How can I fix that?

Real class does:

public SomeObject methodname() {
    MyClass  parameter = mapper.map(source,Dest.class);
    serviceBeingMocked.doSomething(parameter); //<-- want to verify this parameter
}

Test

 MyClass correct = ...;
 correct.setA=a;
 correct.setB=b;

 expect(serviceBeingMocked(correct)).andReturn(iDontCare); //this should work



 MyClass false = ...;
 false.setA=eeeeeeeeeee;
 false.setB=ffffffffff;

 expect(serviceBeingMocked(false)).andReturn(iDontCare); // this test should fail

Thanks in advance!

Edit: Found the answer (using capture to record the paramter and test the values via assert)

Capture paramter = new Capitre(); expect(serviceBeingMocked(EasyMock.capture(parameter)));

assertEquals(parameter.getValue().getWhatever().equals(correct.getWhatever());

هل كانت مفيدة؟

المحلول

If I understand correctly, this test is a test foe methodname(). And you would like to test that, given a source, the correct MyClass paremeter is created by the mapper, and passed to serviceBeingMocked.doSomething().

So in fact, your unit test for methodname() actually tests the mapper.map() method. That's wrong. You should write a unit test for mapper.map() to test that. Once you've ensured that mapper.map() works as expected, you can test methodname() by mocking the mapper.map() method, and verify that the result of this mocked method is passed to the mocked service (although this test wouldn't add much value).

To be complete, if you really want to go your way and check the value passed to serviceBeingMocked(), then you should use a capture.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top