Mocking the methods of hashMap using Mockito

Viewed 31

I am new to java, So I created this class which return if the key is present in map, and if does, it should return the value of the key, but when I testing the code using mockito its not returning the expected. My method

private static Map<String,Long> map1= new HashMap<>();
    public long foo(final String test){
         if(!map1.containsKey(test)){
             return 0L;
         }
         return map1.get(test);
     }

And its test method is

private static Map<String,Long> map1 = mock(Map.class); 
 @Test
 public void testfoo(){
     when(map1.containsKey("test")).thenReturn(true);
     when(map1.get("test")).thenReturn(2L);
     long value = classUndertest.foo("test");
     Assert.assertEquals(2L, value);
 }

But the value I am getting is 0L.

0 Answers
Related