I would like to perform a junit test using Mockito on the toEntity function.
@Component
public class MyEntityTransform {
public Function<MyDTO , MyEntity> toEntity = new Function<MyDTO , MyEntity >() {
@Override
public MyEntity apply(MyDTO record) {
return new MyEntity();
}
};
}
Unfortunately the toEntity is NULL when I mock the class and I don't know how I can test it correctly.
@RunWith(MockitoJUnitRunner.class)
public class MyTest {
@InjectMocks
private MyService _classUnderTest;
@Mock
private MyEntityTransform myEntityTransform
@Before
public void setUp() {
Mockito.when(this.myEntityTransform.toEntity.apply(Mockito.anyObject())).thenReturn(...);
}
}
When I RUN the JUNIT test, Mockito give me the error :
java.lang.NullPointerException org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Misplaced argument matcher detected here:
-> at com.example.MyTest.setUp(MyTest.java:38)
You cannot use argument matchers outside of verification or stubbing. Examples of correct usage of argument matchers: when(mock.get(anyInt())).thenReturn(null); doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject()); verify(mock).someMethod(contains("foo"))
Also, this error might show up because you use argument matchers with methods that cannot be mocked. Following methods cannot be stubbed/verified: final/private/equals()/hashCode(). Mocking methods declared on non-public parent classes is not supported.
Do you have suggestions?
Function
why isMockito
involved? – Sotirios Delimanolisapply()
(as you posted it here) is just a getter for thetoEntity
property. Either the classMyEntityTransform
is a DTO without any logic, that this woule be OK but then it should not be mocked. OrMyEntityTransform
contais some business logic, then nobody else should access its properties (directly). – Timothy Truckle