Reputation: 731
For personMap, I am setting the values with Powermockito;
But I am unable to get the values from 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;
when(personMap.get("RAM")).thenReturn(value);
always returning empty; even though I am setting the values for 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.
Upvotes: 0
Views: 2259
Reputation: 731
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;
}
}
Upvotes: 0
Reputation: 868
Why you want to mock a map? you can just create a new Map and assign it to your object. When we say Mock
we Mock
the action not the data.
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. You don't need to mock it at all.
Upvotes: 2