Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inject a Mock in a Spring Context [duplicate]

I have a test that is using some Spring contexts. In these contexts, a number of beans are declared. I want the test to use the actual implementation of the beans of the contexts, EXCEPT for one of them, for which I want to use a MOCK.

I tried to make the Test a Configuration component (with @Configuration annotation), but the XML seems to take priority over the @Bean annotation, so it doesn't work, this way:

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = {"context1.xml", "context2.xml", ...})
@Configuration
public class MyTest{

    @Inject
    private MyTargetBean target;

    private AnotherBean myMock = mock(AnotherBean.class);

    @Bean
    public AnotherBean myMock() { return myMock; }

    .....

I know that i can define the Mocks in XML, but for that I would need an extra XML file for each test in which I wish to do this. I want to avoid this complexity.

Is there a way to inject a bean (like a mock) in a context apart than through XML?

Thank you!

like image 638
Mr.Eddart Avatar asked Nov 06 '13 09:11

Mr.Eddart


People also ask

How do you inject a mock in SpringBootTest?

If you want to create just a Mockito test you could use the annotation @RunWith(MockitoJUnitRunner. class) instead of @SpringBootTest . But if you want to create a Spring Boot integration test then you should use @MockBean instead of @Mock and @Autowired instead of @InjectMocks .

Can we have multiple context in Spring?

Overview. It's possible to create separate contexts and organize them in a hierarchy in Spring Boot. A context hierarchy can be defined in different ways in Spring Boot application. In this article, we'll look at how we can create multiple contexts using the fluent builder API.

How do you mock and inject a mock?

Mockito @InjectMocks annotations allow us to inject mocked dependencies in the annotated class mocked object. This is useful when we have external dependencies in the class we want to mock. We can specify the mock objects to be injected using @Mock or @Spy annotations.

What is difference between inject mock and mock?

@Mock is used to create mocks that are needed to support the testing of the class to be tested. @InjectMocks is used to create class instances that need to be tested in the test class. Annotated class to be tested dependencies with @Mock annotation.


2 Answers

Yes, you are on the right track, putting a mock @Bean in a @Configuration class is one approach, and I'll describe my experience:

The trick is that you need to use a different set of .xml files purely for testing which exclude the live versions of those beans.

@ContextConfiguration(locations = {"context1-test.xml", "context2-test.xml", ...})

And the "-test-xml" files go in src/test/resources.

At least that was my experience in doing the same thing. Maybe there is some way to "override" the beans with the mock versions, but as yet I am not aware of it.

I also chose to put the mocks (I had 5 of them) all together in an own configuration:

@Configuration
public class MockServicesProvider {
     @Bean
     public AnotherBean myMock() { return mock(AnotherBean.class); }
}

Another interesting part of this problem is the common usage of initMocks(this); in the @Before method of your test class.

If the mocks are being used in other places (and they are, that's why you are wiring them up...) then initMocks(this) will blow them away between tests (not literally - just that new mocks will be created and any other mocks wired up in other objects will be "lost").

The solution to this was to call mockito's reset(mockObject) in the @Before method before each test. The same mocks are reset (all the when's and interactions), without creating new mocks.

Note that the Mockito docs for reset say very sternly that this method should not commonly be used, except in the context of mocks being applied via dependency injection, as we are indeed doing in this case :)

Have fun!

like image 114
vikingsteve Avatar answered Oct 14 '22 07:10

vikingsteve


It is indeed a duplicate of

Injecting Mockito mocks into a Spring bean

The Springockito-annotation is exactly what I was looking for

https://bitbucket.org/kubek2k/springockito/wiki/springockito-annotations

like image 39
Mr.Eddart Avatar answered Oct 14 '22 09:10

Mr.Eddart