I have a class which does the following:
public class Transformer {
public void transform(final Car car) throws IOException {
switch (car.getType()) {
case OFFROAD:
OffroadCar offroadCar = new OffroadTransformer().transform(car);
// do something with offorad car
break;
...
}
}
}
I have a test class:
public class TransformerTest {
@InjectMocks
private Transformer transformer;
@Mock
private OffroadTransformer offroadTransformer;
@BeforeEach
public void setup()
MockitoAnnotations.initMocks(this);
}
@Test
public void testTransform() throws IOException {
final Car car = new Car(OFFROAD);
when(offroadTransformer.transform(any(Car.class))).thenReturn(new OffroadCar());
transformer.transform(car);
// make some verifictations
}
}
My problem now is that the when
is not working. The real offroadTransformer.transform
is called instead of the mock. So my assumption is that the mock is not working because the OffroadTransformer
is no member of the class Transformer
and the instance is created inline.
Is that correct?
If yes: How can I anyway mock it? If no: What else could be the cause?