简体   繁体   English

如何使用Testng和PowerMockito模拟Map

[英]How to mock Map with testng and powermockito

For personMap, I am setting the values with Powermockito; 对于personMap,我使用Powermockito设置值;

But I am unable to get the values from map; 但是我无法从map中获取值;

/**
 * 
 */
package mapmoocer;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class PersonStore {

    Map<String, java.util.List<Person>> personMap = new HashMap<String, List<Person>>();


    public void check() {
        List<Person> list = personMap.get("RAM");
        for(Person person : list) {
            System.out.println(person);
        }
    }

    public void hello() {
        System.out.println("Hello");
    }

}

Here, is the test class; 这是测试课程; for test_check() , not able to cover for each block; 对于test_check() ,无法覆盖每个块; when(personMap.get("RAM")).thenReturn(value); always returning empty; 总是返回空; even though I am setting the values for map; 即使我正在设置map的值;

/**
 * 
 */
package mapmoocer;

import static org.powermock.api.mockito.PowerMockito.when;

import java.util.ArrayList;
import java.util.Map;

import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.powermock.modules.testng.PowerMockObjectFactory;
import org.testng.IObjectFactory;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.ObjectFactory;
import org.testng.annotations.Test;


public class PersonTest {

    @InjectMocks
    PersonStore personStore = new PersonStore();

    @ObjectFactory
    public IObjectFactory getObjectFactory() {
        return new PowerMockObjectFactory();
    }

    @Mock
    Map<String, java.util.List<Person>> personMap;

    @BeforeClass
    public void before(){
        MockitoAnnotations.initMocks(this);
    }

    public void after() {

    }


    @Test
    public void test_hello() {
        personStore.hello();
    }

    @Test
    public void test_check() {
        Person person = new Person();
        person.setEmail("aa");
        java.util.List<Person> value = new ArrayList<Person>();
        when(personMap.get("RAM")).thenReturn(value);
        personStore.check();
    }

}

Help me on this. 帮助我。

Why you want to mock a map? 为什么要模拟地图? you can just create a new Map and assign it to your object. 您可以只创建一个新的Map并将其分配给您的对象。 When we say Mock we Mock the action not the data. 当我们说Mock我们Mock是动作而不是数据。

We provide a mock is to make sure that the object we use will always provide a consistent value when we call one of its methods. 我们提供的模拟是为了确保我们使用的对象在调用其方法之一时始终提供一致的值。

This will make us focus on the code we test, and don't need to worry about the method your code rely on will give you the wrong result. 这将使我们专注于要测试的代码,而不必担心代码所依赖的方法会给您带来错误的结果。

So if you use a Map in your code, you just put the data in to the map, it's done. 因此,如果您在代码中使用Map,只需将数据放入Map中就可以了。 You don't need to mock it at all. 您根本不需要嘲笑它。

I am able to cover for-each snippet with the following code: 我可以使用以下代码覆盖每个代码段:

package mapmoocer;

import static org.powermock.api.mockito.PowerMockito.when;

import java.util.ArrayList;
import java.util.Map;

import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.powermock.modules.testng.PowerMockObjectFactory;
import org.testng.Assert;
import org.testng.IObjectFactory;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.ObjectFactory;
import org.testng.annotations.Test;


public class PersonTest {

    @InjectMocks
    PersonStore personStore = new PersonStore();

    @ObjectFactory
    public IObjectFactory getObjectFactory() {
        return new PowerMockObjectFactory();
    }

    @Mock
    Map<String, java.util.List<Person>> personMap;

    @BeforeClass
    public void before(){
        MockitoAnnotations.initMocks(this);
    }

    @AfterClass
    public void after() {

    }


    @Test
    public void test_hello() {
        personStore.hello();
    }

    @Test(dataProvider="store")
    public void test_check(Object data) {
        java.util.List<Person> persons = (java.util.List<Person>)data;
        when(personMap.get("RAM")).thenReturn(persons);
        personStore.check();
    }

    public Object[][] store() {
        Person person = new Person();
        person.setEmail("aa");
        person.setName("AA");

        java.util.List<Person> value = new ArrayList<Person>();
        value.add(person);

        Object[][] result = {
                {value}
        };
        return result;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM