74

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?

1
  • 1
    The problem is that MockitoJUnitRunner does not work with Spring context, so that will create mocks for B & C, set them into A, but it will ignore the Autowired annotation so d attribute of A will be empty. You need a SpringRunner (like in the accepted answer) to inject beans and use mock annotations too.
    – Aníbal
    Commented Aug 5, 2021 at 13:57

3 Answers 3

68

It should be something like

@RunWith(SpringJUnit4ClassRunner.class)
public class aTest () {

    @Mock
    private B b;

    @Mock
    private C c;

    @Autowired
    @InjectMocks
    private A a;

}

If you want D to be Autowired dont need to do anything in your Test class. Your Autowired A should have correct instance of D. Also i think you need to use SpringJUnit4ClassRunner for Autowiring to work, with contextConfiguration set correctly. Because you are not using MockitoJunitRunner you need to initialize your mocks yourself using

MockitoAnnotations.initMocks(java.lang.Object testClass)

9
  • Is there any way i can continue using @RunWith(MockitoJUnitRunner.class) and solve this issue? the above code is just part of bigger test class, and i cannot change the way we run junits Commented Dec 3, 2015 at 14:21
  • 1
    Yes you can, but you cannot use Autowired in that case then, you have to manually code yourself to initialise spring context load the instance of A Commented Dec 3, 2015 at 14:38
  • 2
    There is a case when this approach won't work: when A is annotated with @Transactional (or has methods annotated with @Transactional); see stackoverflow.com/questions/12857981/… and stackoverflow.com/questions/21124326/… Commented Sep 23, 2016 at 13:44
  • 34
    It is important to note that the object will be created by Autowiring, and the mocks will be injected by setters. This never occurred to me, and because my objects had only a constructor, and no setters for those fields, the mocked instances were not being injected (and doesn't report that it didn't inject the mock). This breaks the model that I'd hoped for (immutable objects), so, like others, I believe that InjectMocks is likely a bad idea. Commented Nov 3, 2016 at 23:12
  • 4
    An important gotcha: If you have @Autowired in the constructor of your class and the field you are trying to mock is set as final then the injection will not work.
    – Toofy
    Commented Jul 5, 2019 at 13:28
23

I was facing same problem and tried the answer by Sajan Chandran. It didn't work in my case because I'm using @SpringBootTest annotation to load only a subset of all my beans. The goal is not to load the beans that I'm mocking since they have lot of other dependencies and configurations.

And I found the following variant of the solution to work for me, which is usable in normal case also.

@RunWith(SpringRunner.class)
@SpringBootTest(classes={...classesRequired...})
public class aTest () {

    @Mock
    private B b;

    @Mock
    private C c;

    @Autowired
    @Spy
    private D d;

    @InjectMocks
    private A a;

    @Before
    public void init(){
        MockitoAnnotations.initMocks(this);
    }

}
3
  • 2
    This is the better solution since you can use class D even if that is not @Autowired inside of class A!
    – rufreakde
    Commented Jul 17, 2020 at 16:11
  • 1
    The problem with this is that A is not initialized by Spring, so any lifecycle method or @Value won't work. If using spring-boot, better use @MockBean for mocks, and inject A normally with @Autowired.
    – Aníbal
    Commented Oct 14, 2022 at 12:41
  • @Aníbal Good point, this was not a concern when I was facing this problem, which is why I settled on a sub-optimal solution that worked for me. Thanks for pointing out the better option here.
    – Thomas
    Commented Nov 5, 2022 at 18:32
18

In addition to accepted answer, if you are using spring-boot, it's easier to use @MockBean annotation (that creates a mock and add it as a bean to the context, replacing it if it exists):

@RunWith(SpringRunner.class)
public class aTest () {

    @MockBean
    private B b;

    @MockBean
    private C c;

    @Autowired
    private A a;
}

In case you are not using spring-boot, the problem with @Autowired + @InjectMocks is that Spring will load unneeded instances for beans B and C first, and then they are replaced by the mocks. This is a waste and could have transitive dependencies that you don't want/can't load. It's always recommended to load the minimum Spring context for your testing. I would recommend this:

@RunWith(SpringRunner.class)
@Import({A.class, D.class})
@ContextConfiguration(classes = aTest.class)
public class aTest () {

    @Bean
    private B b() {
        return Mockito.mock(B.class);
    }

    @Bean
    private C c() {
        return Mockito.mock(C.class);
    }

    @Autowired
    private A a;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.