1

I tried to make a junit test for my class, I would like to Mock my Cache variable when calling the methot "get". My variable cache is an instance of CacheManger who call my database. But i don't know how to test my method. anyone got an idea ? Thank's for your answer !

private static Cache<String, Integer> cache;

private static final String QUERY = "EXEC myQuery";

public static Integer getLanguageId(String language) {
    if (language == null) {
        return null;
    }
    Integer languageId = cache.get(language);
    if (languageId == null) {
        throw new Exception();
    }
    return languageId;
}

static void configureCache() {
    if (cache == null) {
        //CacheManager call database
        cache = CacheManager.getInstance()
                .createCache(QUERY, new RuleConfigurer.StringAndIntegerRowMapper());
    }
}
gaetan0221
  • 21
  • 2
  • Mockito mocks objects, not classes. If everything is static and doesn't use dependency injection, you'll have a hard time using Mockito. – JB Nizet Mar 18 '19 at 16:53
  • From where does the configureCache() method called? If it is not called in the constructor you can white box a mocked cache. If it's called in the constructor you will need to use powermock to mock the CacheManger itself. – LeoN Mar 18 '19 at 16:55
  • My method configureCache() is not alled on my constructor, but from an other class ! – gaetan0221 Mar 19 '19 at 08:31

2 Answers2

0

Not sure if I am following your question completely or not. But to verify static methods you can use PowerMockito. That is the correct way of doing it.

You can see examples here.

Also see one sample class I created below

@RunWith(PowerMockRunner.class)
@PrepareForTest(DriverManager.class)
public class Mocker {

    @Test
    public void shouldVerifyParameters() throws Exception {

        //given
        PowerMockito.mockStatic(DriverManager.class);
        BDDMockito.given(DriverManager.getConnection(...)).willReturn(...);

        //when
        sut.execute(); // System Under Test (sut)

        //then
        PowerMockito.verifyStatic();
        DriverManager.getConnection(...);

    }

More Information

Why doesn't Mockito mock static methods?

Kunal Vohra
  • 2,703
  • 2
  • 15
  • 33
0

Here is a simple unit test for your class.

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import javax.cache.Cache;
import java.lang.reflect.Field;

@RunWith(PowerMockRunner.class)
@PrepareForTest({SomeClass.class})
public class SomeClassTest {

    @Before
    public void setup() throws IllegalAccessException {
        Cache<String, Integer> cache = Mockito.mock(Cache.class);
        Mockito.when(cache.get("Language1")).thenReturn(1);

        Field field = PowerMockito.field(SomeClass.class, "cache");
        field.set(SomeClass.class, cache);
    }

    @Test
    public void should_return_1_when_Language1_is_the_input() throws Exception {
        Integer expectedResult = 1;
        Assert.assertEquals(expectedResult, SomeClass.getLanguageId("Language1"));
    }

    @Test
    public void should_return_null_when_input_is_null() throws Exception {
        Assert.assertNull(SomeClass.getLanguageId(null));
    }

    @Test (expected = Exception.class)
    public void should_throw_exception_when_unknown_language_is_input() throws Exception {
        SomeClass.getLanguageId("UnknownLanguage");
    }
}
LeoN
  • 1,590
  • 1
  • 11
  • 19