I have a class A which is using 3 differnt classes with autowiring
public class A () {
@Autowired
private B b;
@Autowired
private C c;
@Autowired
private D d;
}
While testing them, i would like to have only 2 of the classes (B & C) as mocks and have class D to be Autowired as normal running, this code is not working for me:
@RunWith(MockitoJUnitRunner.class)
public class aTest () {
@InjectMocks
private A a;
@Mock
private B b;
@Mock
private C c;
@Autowired
private D d;
}
Is it even possible to do so?
MockitoJUnitRunner
does not work with Spring context, so that will create mocks forB
&C
, set them intoA
, but it will ignore theAutowired
annotation sod
attribute ofA
will be empty. You need aSpringRunner
(like in the accepted answer) to inject beans and use mock annotations too.