Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes, you are on the right track, putting a mock <code>@Bean</code> in a <code>@Configuration</code> class is one approach, and I'll describe my experience:</p> <p>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.</p> <pre><code>@ContextConfiguration(locations = {"context1-test.xml", "context2-test.xml", ...}) </code></pre> <p>And the "-test-xml" files go in <code>src/test/resources</code>.</p> <p>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.</p> <p>I also chose to put the mocks (I had 5 of them) all together in an own configuration:</p> <pre><code>@Configuration public class MockServicesProvider { @Bean public AnotherBean myMock() { return mock(AnotherBean.class); } } </code></pre> <p>Another interesting part of this problem is the common usage of <code>initMocks(this);</code> in the <code>@Before</code> method of your test class.</p> <p>If the mocks are being used in other places (and they are, that's why you are wiring them up...) then <code>initMocks(this)</code> 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").</p> <p>The solution to this was to call mockito's <code>reset(mockObject)</code> in the <code>@Before</code> method before each test. The same mocks are reset (all the <code>when</code>'s and interactions), without creating new mocks.</p> <p>Note that the Mockito docs for <code>reset</code> 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 :)</p> <p>Have fun!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload