I have a test which does not do what is stated in .thenReturn() part of mockito when(). It fails as it goes into the method as when() does not exist an that class is not mocked. I'll past code here, help please:
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@ExtendWith(MockitoExtension.class)
class ClassTest {
@Mock private ClassA classA;
@Mock private ClassB classB;
private ClassC classC;
@BeforeAll
void init() {
classC = new classC(classA, classB);
MockitoAnnotations.openMocks(this);
}
@Test
@DisplayName("Test description")
void first_test() {
ClassD classD = new classD();
ClassE classE = new classE();
when(classB.methodInClassB(classE).thenReturn(classD);
ClassF classF = new ClassF();
when(classA.apply(classD)).thenReturn(classF);
ClassE compute = classC.apply(classE);
verify(classB).methodInClassB(classE);
verify(classA).apply(classD);
}
}
For sake of better understanding code as I masked all names for security reasons, here's a Class C as well:
@AllArgsConstructor
public class ClassC {
private ClassB classB;
private ClassA classA;
public ClassE apply (ClassE entity) {
ClassF computedEntity = classA.apply(classB.methodInClassB(entity));
entity.setSomething(computedEntity.getSomething());
return entity;
}
}
Test fails because execution went into classA.apply() even though it is mocked with when().thenReturn() statement...