2 回答
TA贡献1842条经验 获得超21个赞
在调用方法中,您使用string.class[paramter]而不是 Class[][parameterArray]。
无需为 Test2.class 创建另一个对象,您已使用 @injectMocks 只需使用调用方法中的变量即可。
public class JunitTestCases{
@Mock
Test test;
@InjectMocks
Test2 test2;
@Test
public void Oncall{
MockitoAnnotations.initMocks(this);
Class<?>[] params = new Class<?>[]{String.class};
Method method=Test2.class.getDeclaredMethod("method",params);
method.setAccessible(true);
method.invoke(test2, "data");
}
}
TA贡献1830条经验 获得超9个赞
写作测试演示课程
public class PowerMockDemo {
public Point callPrivateMethod() {
return privateMethod(new Point(1, 1));
}
private Point privateMethod(Point point) {
return new Point(point.getX() + 1, point.getY() + 1);
}
}
演示类的测试类
@RunWith(PowerMockRunner.class)
@PrepareForTest(PowerMockDemo.class)
public class PowerMockDemoTest {
private PowerMockDemo powerMockDemoSpy;
@Before
public void setUp() {
powerMockDemoSpy = PowerMockito.spy(new PowerMockDemo());
}
@Test
public void testMockPrivateMethod() throws Exception {
Point mockPoint = mock(Point.class);
PowerMockito.doReturn(mockPoint)
.when(powerMockDemoSpy, "privateMethod", anyObject());
Point actualMockPoint = powerMockDemoSpy.callPrivateMethod();
assertThat(actualMockPoint, is(mockPoint));
}
}
添加回答
举报