2

Using Spring-Boot 2.1.4.RELEASE. I have PCF bound Redis cache. In CacheSevice i have @Cacheable method that i call to get configurations.

Code works and unit test also works without validateMockitoUsage()

However if i right-click and run "All-tests" or try to deploy using maven-plugin. Test passes but breaks the first test of the next test class. with "Could not load Application Context"

With validateMockitoUsage() only the cacheService test breaks with: "org.mockito.exceptions.misusing.UnfinishedVerificationException:

I saw a discussion on a very similar issue on this thread (in the comments): https://stackoverflow.com/a/24229350/5680752 but no follow up

@RunWith(SpringRunner.class)
@SpringBootTest
@DirtiesContext
public class RediTests {

    @Autowired
    private CacheService cacheService;


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

    @After
    public void validate() {
        validateMockitoUsage();
    }

    @Configuration
    @EnableCaching
    static class Config {

        @Bean
        CacheService cacheService() {
            return Mockito.mock(CacheService.class);
        }

        @Bean
        @SuppressWarnings("unchecked")
        public RedisSerializer<Object> defaultRedisSerializer() {
            return Mockito.mock(RedisSerializer.class);
        }

        @Bean
        @SuppressWarnings("unchecked")
        public RedisTemplate<String, Object> defaultTemplate() {
            return Mockito.mock(RedisTemplate.class);
        }

        @Bean
        public JedisConnectionFactory connectionFactory() {
            JedisConnectionFactory factory = Mockito.mock(JedisConnectionFactory.class);
            RedisConnection connection = Mockito.mock(RedisConnection.class);
            Mockito.when(factory.getConnection()).thenReturn(connection);
            return factory;
        }
        @Bean
        CacheManager cacheManager() {
            return new ConcurrentMapCacheManager("my-cache");
        }
    }

    @Test
    public void testCache_CallsCacheableTwice_WithSameArgument_SkipsMethodCall() throws JSONException {

        Configuration mockConfiguration1  = new Configuration ();
        mockConfiguration1.setName("mockConfig1");

        Configuration mockConfiguration2  = new Configuration ();
        mockConfiguration1.setName("mockConfig2");


        when(cacheService.getConfigurations(ArgumentMatchers.any())).thenReturn(mockConfiguration1, mockConfiguration2);

        Configuration firstCall = cacheService.getConfigurations("1");
        assertEquals(firstCall, mockConfiguration1);

        Configuration secondCall = cacheService.getConfigurations("1");
        assertEquals(secondCall, mockConfiguration1);

        Mockito.verify(cacheService, Mockito.times(1)).getConfigurations("1");

        Configuration thirdCall = cacheService.getConfigurations("2");
        assertEquals(thirdCall, mockConfiguration2);

    }
}


  [1]: https://stackoverflow.com/a/24229350
Giolla
  • 83
  • 7
  • try adding this `@TestExecutionListeners(value = { DirtiesContextTestExecutionListener.class })` to your tests. ( Note it will take far longer to run than before.. you can tweak it later. ) , DirtiesContext requires a listener as far as i know for it to operate. – Michael Michailidis Sep 18 '19 at 12:42
  • @MichaelMichailidis i tried it and it still didn't work. I've got tests commented out for now since i'm just building this project and adding pieces to it. Once i figure it out, i'll post an update here – Giolla Oct 03 '19 at 13:22
  • If you can provide a small project that reproduce the error I may figure out the solution as It seems a bit strange not to reset. Mostly like a missconfiguration – Michael Michailidis Oct 03 '19 at 13:25
  • Any solution @Giolla? – FabianoLothor Jan 06 '20 at 19:27

0 Answers0