Reputation: 1141
import java.lang.Math;
import static org.mockito.Mockito.*;
import java.util.Map;
import java.util.HashMap;
public class MyClass
{
class Test {
int a = 10;
Test() {}
}
class OtherClass
{
public OtherClass()
{}
Map<String, Double> f() {
System.out.print("Just for testing");
return new HashMap<>();
}
Test getT() {
return new Test();
}
}
public static void main(String[] args)
{
OtherClass c = mock(OtherClass.class);
Map<String, Double> test = c.f();
System.out.println(test.size());
MyClass.Test t = c.getT();
System.out.println(t);
}
}
In this example I have created mock object for OtherClass
type.
What strange for me that c.f()
returns empty Map, meanwhile c.getT()
returns null.
Could you please help me to understand this behavior?
Upvotes: 2
Views: 223
Reputation: 15156
If you do not provide a stub for Map
, Mockito will return an empty map. This is a documented feature, confirmed by the JavaDocs:
By default, for all methods that return value, mock returns null, an empty collection or appropriate primitive/primitive wrapper value (e.g: 0, false, ... for int/Integer, boolean/Boolean, ...).
Upvotes: 2