Hej我在TestNG和powerMockito 1.5.x上遇到了一些问题我试过1.5.5和1.5.6。问题是当我尝试验证非空的构造函数时。
我已经检查过一些关于从PowerMockTestCase等扩展的例子,但它们似乎对我不起作用。
package com.cdev.common;
public class TestSubject {
String data;
String context;
public TestSubject()
{
this.data = "Mock!!";
this.context = "PowerMockito";
}
public TestSubject(String data)
{
this.data = data;
this.context = "PowerMockito";
}
public TestSubject(String data,String context)
{
this.data = data;
this.context = context;
}
public String getData()
{
throw new UnsupportedOperationException();
}
public String getContext()
{
throw new UnsupportedOperationException();
}
public boolean isNew()
{
throw new UnsupportedOperationException();
}
}
这是testNG测试:
package com.cdev.common.test;
import com.cdev.common.*;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.testng.Assert;
import org.testng.annotations.Test;
@PrepareForTest({TestSubject.class})
public class TestSubjectTest{
@Test
public void TestSubject() throws Exception
{
System.out.println("======== Test ==================");
TestSubject mockTestSubject = PowerMockito.mock(TestSubject.class);
PowerMockito.whenNew(TestSubject.class).withArguments("A", "B").thenReturn(mockTestSubject);
PowerMockito.when(mockTestSubject.getData()).thenReturn("A");
PowerMockito.when(mockTestSubject.getContext()).thenReturn("B");
PowerMockito.when(mockTestSubject.isNew()).thenReturn(true);
PowerMockito.verifyNew(TestSubject.class).withArguments("A", "B");
TestSubject lala = new TestSubject("A","B");
System.out.println(lala.getData());
System.out.println(lala.isNew());
Assert.assertTrue(lala.getData().equals("A"), "Not equal");
}
}
这给了我这个输出:
[TestNG] Running:
/tmp/testng-eclipse--2099806353/testng-customsuite.xml
======== Test ==================
FAILED: TestSubject
java.lang.AssertionError: Wanted but not invoked com.cdev.common.TestSubject(
"A",
"B"
);
Actually, there were zero interactions with this mock.
at org.powermock.api.mockito.internal.invocation.InvocationControlAssertionError.throwAssertionErrorForNewSubstitutionFailure(InvocationControlAssertionError.java:93)
at org.powermock.api.mockito.internal.verification.DefaultConstructorArgumentsVerfication.invokeSubstitute(DefaultConstructorArgumentsVerfication.java:51)
at org.powermock.api.mockito.internal.verification.DefaultConstructorArgumentsVerfication.withArguments(DefaultConstructorArgumentsVerfication.java:44)
at com.cdev.common.test.TestSubjectTest.TestSubject(TestSubjectTest.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
===============================================
Default test
Tests run: 1, Failures: 1, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 1, Failures: 1, Skips: 0
===============================================
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@6d9c638: 18 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@2401f4c3: 6 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@5d22bbb7: 3 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@3d012ddd: 31 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 2 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@1175e2db: 5 ms
答案 0 :(得分:1)
您需要在调用完成后将verifyNew
调用移动到:
TestSubject lala = new TestSubject("A","B");
PowerMockito.verifyNew(TestSubject.class).withArguments("A", "B");
答案 1 :(得分:0)
按照说明操作:
@Test
public void TestSubject() throws Exception
{
System.out.println("======== Test ==================");
TestSubject mockTestSubject = PowerMockito.mock(TestSubject.class);
PowerMockito.whenNew(TestSubject.class).withArguments("A", "B").thenReturn(mockTestSubject);
PowerMockito.when(mockTestSubject.getData()).thenReturn("A");
PowerMockito.when(mockTestSubject.getContext()).thenReturn("B");
PowerMockito.when(mockTestSubject.isNew()).thenReturn(true);
**TestSubject lala = new TestSubject("A","B");
PowerMockito.verifyNew(TestSubject.class).withArguments("A", "B");**
System.out.println(lala.getData());
System.out.println(lala.isNew());
Assert.assertTrue(lala.getData().equals("A"), "Not equal");
}
输出:
[TestNG] Running:
/tmp/testng-eclipse--349458877/testng-customsuite.xml
======== Test ==================
FAILED: TestSubject
java.lang.AssertionError: Wanted but not invoked com.cdev.common.TestSubject(
"A",
"B"
);
Actually, there were zero interactions with this mock.
at org.powermock.api.mockito.internal.invocation.InvocationControlAssertionError.throwAssertionErrorForNewSubstitutionFailure(InvocationControlAssertionError.java:93)
at org.powermock.api.mockito.internal.verification.DefaultConstructorArgumentsVerfication.invokeSubstitute(DefaultConstructorArgumentsVerfication.java:51)
at org.powermock.api.mockito.internal.verification.DefaultConstructorArgumentsVerfication.withArguments(DefaultConstructorArgumentsVerfication.java:44)
at com.cdev.common.test.TestSubjectTest.TestSubject(TestSubjectTest.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
===============================================
Default test
Tests run: 1, Failures: 1, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 1, Failures: 1, Skips: 0
===============================================
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@6d9c638: 36 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@2401f4c3: 8 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@5d22bbb7: 13 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@3d012ddd: 45 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 4 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@1175e2db: 5 ms