0

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.

Alex
  • 21,273
  • 10
  • 61
  • 73
Manuel S.
  • 411
  • 8
  • 21

1 Answers1

8

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)

Community
  • 1
  • 1
Evgeny Tanhilevich
  • 1,119
  • 1
  • 8
  • 17
  • This has done the trick. This code I posted is part of a test, and I wasn't supposed to touch the test class. But I can't find any other way of getting this to work, without your solution. Thank you ver much, – Manuel S. Mar 09 '16 at 22:41
  • 1
    In JUInit5 @RunWith is not needed anymore. Just add @ExtendWith(MockitoExtension.class – matio May 24 '21 at 11:37