I have a class I want to test that has several external dependencies, and a couple internal methods. I would like to write a test for MethodA
, but not have Method A's internal call to MethodB
to actually exercise MethodB
. I'd like to mock/stub MethodB
and return something specific instead. Usually I'd use when/thenReturn
but it doesn't behave like I expect - it actually jumps into Method B while creating the mock itself.
MyService.java
@Service
public class MyService {
@Autowired
private ServiceA serviceA;
@Autowired
private ServiceB serviceB;
public SomeObject methodA() {
// some logic using serviceA.method and serviceB.method that creates "output"
SomeObject someObject = methodB(output);
return someObject;
}
public SomeObject methodB(SomeObject someObject) {
// deep mysteries done here to someObject
return someObject
}
}
MyServiceTest.java
public class MyServiceTest {
@Mock
private ServiceA serviceA;
@Mock
private ServiceB serviceB;
@InjectMocks
private MyService myService;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
}
@Test
public void methodATest() {
when(serviceA.method()).thenReturn(stuff);
when(serviceB.method()).thenReturn(otherStuff);
// here is what I would like to do
when(myService.methodB()).thenReturn(mockedSomeObject); //<- doesn't work
assertThat(myService.methodA().getSomeObjectProperty())
.isEqualTo("property");
}
}
I've looked at solutions that manually mock the MyService
class with Mockito.mock(MyService.class)
, but (as the above example is obviously contrived) my actual class has quite a few external dependencies and I'd prefer a solution that still allows me to mock the service using @Mock
for the @Autowired
dependencies and @InitMocks
for the class under test, unless it's simply not possible.
I've tried:
Mockito.doReturn(mockedSomeObject).when(myService.methodB(any(SomeObject.class));
but that also steps into MethodB when creating the mock for that method, which shouldn't be happening.
Thanks in advance for the help!