繁体   English   中英

如何使用Testng和PowerMockito模拟Map

[英]How to mock Map with testng and powermockito

对于personMap,我使用Powermockito设置值;

但是我无法从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");
    }

}

这是测试课程; 对于test_check() ,无法覆盖每个块; when(personMap.get("RAM")).thenReturn(value); 总是返回空; 即使我正在设置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();
    }

}

帮助我。

为什么要模拟地图? 您可以只创建一个新的Map并将其分配给您的对象。 当我们说Mock我们Mock是动作而不是数据。

我们提供的模拟是为了确保我们使用的对象在调用其方法之一时始终提供一致的值。

这将使我们专注于要测试的代码,而不必担心代码所依赖的方法会给您带来错误的结果。

因此,如果您在代码中使用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.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