简体   繁体   中英

Mockito mock interface returning null in Before

I am trying to run a JUnit test using Mockito API.

I've got the following CacheTest class

public class CacheTest {

    public static final String KEY_1 = "key1";
    public static final long VALUE_1 = 1L;
    public static final long VALUE_2 = 2136L;
    private static final String KEY_2 = "key2";
    private Cache<String, Long> objectUnderTest;

    @Mock
    private CacheLoader<String, Long> cacheLoader;

    @Before
    public void setUp() {
        objectUnderTest = new Cache<>(1000L, cacheLoader); //cacheLoader is null here
        when(cacheLoader.load(Matchers.eq(KEY_1))).thenReturn(VALUE_1); //nullpointer when trying to load from null
        when(cacheLoader.load(Matchers.eq(KEY_2))).thenReturn(VALUE_2);
    }

    @Test
    public void shouldLoadElement() {
        // when
        Long value = objectUnderTest.get(KEY_1);

        // then
        assertThat(value).isEqualTo(VALUE_1);
        verify(cacheLoader, Mockito.times(1)).load(Matchers.eq(KEY_1));
    }
}

CacheLoader interface

public interface CacheLoader<KEY, VALUE> {

    VALUE load(KEY key);
}

Cache class which recieves mocked cacheLoader as a parameter

public class Cache<K,V> {

    private long evictionTimeMillis;

    private CacheLoader<K,V> cacheLoader;

    public Cache(final long evictionTimeMillis, final CacheLoader<K, V> cacheLoader) {
        this.evictionTimeMillis = evictionTimeMillis;
        this.cacheLoader = cacheLoader;
    }

    public V get(K key) {
        // TODO
        return this.cacheLoader.load(key);
    }


    public void cleanUp() {
        //TODO
    }

}

And CacheLoader implementation

public class CacheLoaderImpl implements CacheLoader<String, Long>{

    private HashMap<String,Long> map = new HashMap<String, Long>() {
        /**
         * 
         */
        private static final long serialVersionUID = -7591912310318794715L;

        {
            map.put("key1", 1L);
            map.put("key2", 2136L);
        }
    };

    @Override
    public Long load(String key) {
        return map.get(key);
    }

}

The problem is, as the comments suggets in CacheTest class, that the mocked interface is null when executing the @Before method, so when I try to load I get a nullpointer exception.

I'm new to Mockito, so excuse me if this is a noobie mistake.

I've seen similar questions here in SO, but none of them really gave me an answer to my particular case.

I hope my question is clear, if not, please tell me how could I improve it.

You need to annotate the test class with @RunWith(MockitoJUnitRunner.class) . Stock JUnit runner does not initialise mocks. See also @RunWith(MockitoJUnitRunner.class) vs MockitoAnnotations.initMocks(this)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM