2 回答
TA贡献1829条经验 获得超6个赞
在调用方法中,你使用字符串.class[paramter]而不是Class[][parameterArray]。
无需为 Test2 创建其他对象.class已经使用过@injectMocks只需在 invoke 方法中使用变量即可。
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贡献1827条经验 获得超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));
}
}
添加回答
举报