I have two classes, Parent and Child, and want to unit test some methods in the Child class using Mockito.
public abstract class Parent {
@Resource Service service;
}
@Service // spring service
public class Child extends Parent {
private AnotherService anotherService;
@Autowired
Child(AnotherService anotherService) {
this.anotherService = anotherService;
}
public boolean someMethod() {
}
}
My test class looks like below:
@RunWith(MockitoJUnitRunner.class)
public class ChildTest {
@Mock
Service service;
@Mock
AnotherService anotherService;
@InejctMocks
Child classToTest;
@Test
public void testSomething() {
assertTrue(classToTest.someMethod());
}
}
And the issue I am facing is that anotherService
is being mocked properly but service
is null
. Can someone please tell me how to successfully mock both of the services (dependencies) in my test class?