Home > Mobile >  How to add mock for following in junit
How to add mock for following in junit

Time:01-06

        propSet = childRes.getValueMap().keySet();

Above code written in java can anyone help me to write mock in mockito in junit

CodePudding user response:

you can use

Map data = new HashMap<>();// add data to it
YOUR_CLASS_NAME childRes = Mokito.spy(YOUR_CLASS_NAME.class);//Mokito.mock(YOUR_CLASS_NAME.class);
Mockito.when(childRes.getValueMap()).thenReturn(map);

The difference is that in mock, you are creating a complete mock or fake object while in spy, there is the real object and you just spying or stubbing specific methods of it. ... While in spy objects, of course, since it is a real method, when you are not stubbing the method, then it will call the real method behavior.

CodePudding user response:

You have to mock the class type of the above object reference childRes.

For example:

Mockito.mock(ChildResponse.class);

Later you have to stub getValueMap()

Map<String> data = new HashMap<>();
// Populate some data into map
ChildResponse childRes = Mockito.mock(ChildResponse.class);
when(childRes.getValueMap()).thenReturn(data);
  •  Tags:  
  • Related