In the advice of Mockito about how to write good tests, it is written that we should not mock value objects (https://github.com/mockito/mockito/wiki/How-to-write-good-tests#dont-mock-value-objects). They even say
Because instantiating the object is too painful !? => not a valid reason.
But I don’t understand. What if we really don’t care about what that object contain? Let’s just say that I have a value object class “Foo” that doesn’t have simple constructors (it has an all-args constructor and contains objects that also need all-args constructors). Let’s also say I have the following service:
public class MyService { private SomeService someService; private OtherService otherService; public void myAwesomeMethod() { Foo foo = someService.getFoo(); someOtherService.doStuff(foo); } }
And I need to test that method. Clearly here I don’t care about what foo contains because I just pass it to another method. If I want to test it should I really create a foo object with nested constructors/builders? For me it would be really easy to mock foo
even if it is a POJO :
public class MyServiceTest { @InjectMocks MyService myService; @Mock private SomeService someService; @Mock private OtherService otherService; @Test public void testMyAwesomeMethod() { Foo mockedFoo = Mockito.mock(Foo.class); Mockito.when(someService.getFoo()).thenReturn(mockedFoo); Mockito.verify(otherService).doStuff(mockedFoo); } }
But then it wouldn’t respect the guidelines given by Mockito. Is there another alternative?