我想测试在某个类中创建了一个 ServletFileUpload 类的实例。
这是正在测试的类:
public class ClassBeingTested {
public void run()
{
new ServletFileUpload();
}
}
这是测试:
@RunWith(PowerMockRunner.class)
@PrepareForTest(ServletFileUpload.class)
public class MyTest {
@Test
public void testServletUploadCreation() throws Exception {
// Setup mock
final ServletFileUpload fileUpload = createMockAndExpectNew(
ServletFileUpload.class);
replay(fileUpload, ServletFileUpload.class);
// Invoke method under test
final ClassBeingTested objectUnderTest = new ClassBeingTested();
objectUnderTest.run();
// Verify that ServletFileUpload instance was created
verify(fileUpload, ServletFileUpload.class);
}
}
该verify
语句引发以下异常:
java.lang.AssertionError:
Expectation failure on verify:
org.apache.commons.fileupload.servlet.ServletFileUpload(): expected: 1,
actual: 0
at org.powermock.api.easymock.internal.invocationcontrol.NewInvocationControlAssertionError.throwAssertionErrorForNewSubstitutionFailure(NewInvocationControlAssertionError.java:21)
at org.powermock.api.easymock.PowerMock.verifyClass(PowerMock.java:2279)
at org.powermock.api.easymock.PowerMock.verify(PowerMock.java:1646)
at MyTest.testServletUploadCreation(MyTest.java:21)
为什么?我该如何解决?
请注意,这种模拟构造函数的方式适用于我代码的其他地方。