我正在用Mockito编写JUnit Test案例,该类有两个方法methodA,methodB。我想在我的测试用例中模拟对方法B的方法A的调用。有人帮我.pls
这是班级:
public Class Test{
public static List<Object> methodA() {
...
return list;
}
public static List<Object> methodB() {
...
list = methodA();
return list;
}
}
答案 0 :(得分:1)
您需要使用PowerMockito来模拟静态方法。
示例:
package unitest;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
@PrepareForTest(unitest.Test.class)
public class TestTest {
@Test
public void testMethodB() {
PowerMockito.mockStatic(unitest.Test.class);
PowerMockito.when(unitest.Test.methodA()).thenReturn(new ArrayList());
List b = unitest.Test.methodB();
org.junit.Assert.assertNotNull(b);
}
}