I am using JUnit5.
I have following scenario:
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.junit.jupiter.MockitoExtension;
@ExtendWith(MockitoExtension.class)
public class SomeClassTest {
// needs to be mocked but not injected
@Mock
private EntityManager entityManager;
// needs to be mocked and injected
@Mock
private SomeDao someDao;
@InjectMocks
private SomeClass someClass = new someClass(entityManager);
public class SomeClass{
EntityManager entityManager;
public SomeClass(EntityManager entityManager) {
if (entityManager == null) {
throw new NullpointerException("Manager is null");
} else {
this.entityManager = entityManager;
}
The problem is:
- mocked EntityManager object is needed to create the class I want to test
- must inject SomeDao which is needed in SomeClass
- I get a nullpointer because the mocked object appears not to be created when I give it as argument to the constructor
Has anybody an idea how to solve this? I could create a no-arg constructor but that would give me a constructor which I only need for testing which isn't that 'clean'.