简体   繁体   English

测试模拟类中包含的地图

[英]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.19Espresso 2.2.2JUnit 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.

相关问题 测试修改模拟类字段的方法 - Testing a method which modifies the field of the mocked class 使用Mockito进行单元测试会将模拟类的实例变量重置为null - Unit testing with Mockito resets a mocked class' instance variable to null Android:使用接口在类测试期间提供模拟的实现 - Android: Providing a mocked implementation during testing of a class using an interface Mocked static outer class members are null when testing static inner class with PowerMockito - Mocked static outer class members are null when testing static inner class with PowerMockito 测试模拟 object 的良好实践 - good practice for testing mocked object 对具有构造函数和需要使用 mockito 模拟的自动装配字段的类的方法进行单元测试 - Unit testing a method of a class that has a constructor and an autowired field that needs to be mocked using mockito 使用模拟异常测试自定义谓词由于不正确的模拟而失败 class - Testing custom predicate using mocked Exception failing due to incorrect Mock class Spring 模拟依赖的集成测试 - Spring integration testing with mocked dependencies EasyMock:部分嘲笑的课程 - EasyMock: Partially mocked class 单身人士班不被嘲笑 - Singleton class not getting mocked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM