[英]Testing a map contained in a mocked class
I am trying to test a ExampleController
class which works kind of like a facade for my POJO Example
class. 我正在尝试测试
ExampleController
类,该类的工作方式类似于POJO Example
类的外观。
I started to write a InstrumentedTest
for the ExampleController
because it works with Greenrobots EventBus, and got a NullPointerException
when I tried to retrieve values which are stored in a Map
in my Example
class. 我开始为
ExampleController
编写InstrumentedTest
,因为它可以与Greenrobots EventBus一起使用,并且在尝试检索Example
类中存储在Map
中的值时遇到了NullPointerException
。
I use Mockito v2.7.19
, Espresso 2.2.2
and JUnit 4.12
. 我使用
Mockito v2.7.19
, Espresso 2.2.2
和JUnit 4.12
。
I recreated my problem in a Unit test with the following example setup: 我使用以下示例设置在单元测试中重新创建了我的问题:
class Example {
private HashMap<Integer, String> map;
Example(){ map = new HashMap<>(); }
//getter and setter for map
}
class ExampleController {
private Example example;
ExampleController(Example example){ this.example = example; }
public HashMap<Integer, String> getMap(){ return example.getMap(); }
}
Test class: 测试类别:
class ExampleControllerTest {
ExampleController exampleController;
@Before
public void setUp() throws Exception {
Example example = mock(Example.class);
exampleController = new ExampleController(example);
}
@Test
public void testPutThingsInMap() throws Exception {
HashMap<Integer, String> map = exampleController.getMap();
map.put(1, "Test");
exampleController.getMap().putAll(map);
assertEquals(1, exampleController.getMap().size());
}
}
When I run the test class I get the following output: 当我运行测试类时,得到以下输出:
java.lang.AssertionError:
Expected :1
Actual :0
As I am relatively new to testing, I don't know where I went wrong. 由于我是测试的新手,所以我不知道哪里出错了。 When I search for unit testing lists, I only find tests for lists that are not contained in an object.
当我搜索单元测试列表时,我只会找到对象中未包含的列表的测试。
You can't use the "getMap" method like that, because you mocked the Example class in the ExampleController. 您不能使用那样的“ getMap”方法,因为您在ExampleController中模拟了Example类。 You could fix this by adding the following in the before-method:
您可以通过在方法前添加以下内容来解决此问题:
HashMap<Integer, String> mapInMock = new HashMap<>();
when(example.getMap()).thenReturn(mapInMock);
This way you're telling the mocked Example to return that hashMap when the getter is called. 这样,您就告诉模拟的示例在调用getter时返回该hashMap。
I can't find it immediately in the javadoc, but debugging the test shows that the map a new map is returned after each call to exampleController.getMap(). 我无法立即在javadoc中找到它,但是调试测试显示该地图在每次调用exampleController.getMap()之后都会返回一个新地图。 That explains why you can't get anything in the map and retrieve it afterwards with your exampleController.
这就解释了为什么您无法在地图中获得任何内容并随后使用exampleController对其进行检索。
Besides this being the solution you can wonder if you actually need to mock the Example class. 除了这是解决方案之外,您还可以怀疑是否确实需要模拟Example类。 You could also just instantiate it, unless you actually want to mock some parts of it.
您也可以只实例化它,除非您实际上想模拟它的某些部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.