Cannot instantiate @InjectMocks field named exception with java class
if you do a employee = new Employee(param1, param2);
you may as well skip @InjectMocks
.
It is supposed to do the following:
@InjectMocks
ClassUnderTest cut;
@Mock
Dependency1 dep1;
@Mock
Dependency2 dep2;
@Before
public void setup() {
initMocks(this);
}
omitting @InjectMocks
the same behaviour can be achieved with the following code:
ClassUnderTest cut;
@Mock
Dependency1 dep1;
@Mock
Dependency2 dep2;
@Before
public void setup() {
initMocks(this);
cut = new ClassUnderTest(dep1, dep2);
}
In your specific case, you should mock param1
and param2
. Never call the constructor manually when using @InjectMocks
.