SimpleA.java类是目标类。我必须模拟SimpleB.java类的getList方法,如果add方法是非静态的,它的工作正常,但它显示静态添加方法的问题
SimpleA.java
import java.util.ArrayList;
public class SimpleA {
private static SimpleB obj1 ;
public static int add(){
ArrayList<Integer> arr1 = obj1.getList1();
ArrayList<Integer> arr2 = obj1.getList2();
int res=0;
for(Integer val : arr1){
res+=val;
}
for(Integer val1 : arr2){
res+=val1;
}
return res;
}
}
SimpleB.java
import java.util.ArrayList;
import java.util.Iterator;
public class SimpleB {
public ArrayList<Integer> getList1(){
System.out.println("inside class SimpleB and getlist1");
ArrayList<Integer> lst1 = new ArrayList<Integer>();
lst1.add(10);
lst1.add(20);
return lst1;
}
public ArrayList<Integer> getList2(){
System.out.println("inside class SimpleB and getlist2");
ArrayList<Integer> lst2 = new ArrayList<Integer>();
lst2.add(30);
lst2.add(40);
return lst2;
}
}
SimpleATest.java
这是测试类,我正在测试SimpleA.java类
import java.lang.reflect.Field;
import java.util.ArrayList;
import org.easymock.EasyMock;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
@RunWith(PowerMockRunner.class)
@PrepareForTest({SimpleA.class})
public class SimpleATest {
SimpleA simpleA1;
SimpleB mockSample;
@Before
public void setUp(){
ArrayList<Integer> lst = new ArrayList<Integer>();
lst.add(30);
lst.add(40);
lst.add(100);
simpleA1 = new SimpleA();
mockSample = PowerMock.createMock(SimpleB.class);
PowerMock.mockStatic(SimpleA.class);
EasyMock.expect(mockSample.getList1()).andReturn(lst);
EasyMock.expect(mockSample.getList2()).andReturn(lst);
PowerMock.replay(mockSample);
Whitebox.setInternalState(simpleA1,mockSample);
System.out.println("Init method is invoked");
}
@Test
public void testAdd(){
int res = simpleA1.add();
System.out.println("res = "+res);
}
@After
public void destroy(){
System.out.println("Destroy method is invoked");
}
}
答案 0 :(得分:0)
我过去曾见过这种行为。方法是首先在静态方法上设置默认答案,然后填写行为:
@RunWith(PowerMockRunner.class)
@PrepareForTest(MyClassWithStaticMethods.class)
public testClass{
@Test
public void testSomething(){
// this prepares the class; all static methods get a default return value (in this case type Object is returned and the default returned object is null)
PowerMockito.mockStatic(MyClassWithStaticMethods.class, new Answer<Object>()
{
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return null;
}
});
//Now the class is prepared, we can just change the behaviour of the static method in the way we would like.
String in = "example input string";
String out = "example output string";
// after preparing the class, you can override a specific static method. For this example we use the matchers, could also have used Matchers.any(String.class) or any other matcher combination as long as it maps on the method.
PowerMockito.when(MyClassWithStaticMethods.staticMethod(Matchers.eq(in))).thenReturn(out);
}
//..body
}