How to mock Map<String, List<StudentTable> in Junit?

Viewed 41

Map<String, List mapobj = this.buildHashMap("userId", locks);

mapobj.forEach((userId,userlock) ->{

How to mock this can anyone help me?

1 Answers

From your question, it's not obvious what you're trying to achieve. However if you're trying to mock Map instance, then you can do something like:

  public class MTest {

    @Mock
    Map<String, String> map;

    @BeforeEach
    public void init() {
        MockitoAnnotations.openMocks(this);
    }

    @Test
    public void test() {
        // given
        when(map.get("someKey")).thenReturn("someValue");

        // when
        final String value = map.get("someKey");

        // then
        assertEquals("someValue", value);
    } 
  }
Related