My Scenario is as below
class SuperClass{
public void run(){
System.out.println(\"I am running in Super class\");
}
}
class ChildClass extends S
Here is an example for a class that extends another class and it has some other dependencies. In this case, I'll move the superclass call into the other method and then mock the superclass caller method.
class Child extends Parent {
@Autowired
private Dependicy d;
public Authentication authenticate(Authentication auth) {
the code to be tested...
superAuthenticate(auth);// the code that I don't want to deal with it.
return auth;
}
protected Authentication superAuthenticate(Authentication auth) {
return super.authenticate(auth);
}
}
As you can see above, the authenticate method does some logic and then call the super class's method, so I want to mock the superclass call and test my own code block. Here is my test class:
@RunWith(MockitoJUnitRunner.class)
public class ChildTest {
@Mock
private Dependicy d;
@InjectMocks
private Child child = new Child();
@Test
public void testSomething() {
Child spy = Mockito.spy(child);
when(d.aMethod(aParam)).thenReturn(aResult);
doReturn(usefulResult).when(spy).superAuthenticate(any());
Authentication result = spy.authenticate(auth);
assertThat(result).isNotNull;
}
}