我正在尝试使用TestNG和Powermockito将单元测试添加到旧版报告生成代码中。
import static org.testng.AssertJUnit.assertTrue;
import static org.powermock.api.support.membermodification.MemberModifier.stub;
import org.testng.annotations.Test;
import org.mockito.Matchers;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
@PrepareForTest(ConfigReader.class)
public class defTest{
@Test
public void testGetPartsSuccessful() throws Exception {
TrendsReportData reportGenerator = new TrendsReportData();
TreeMap<Calendar, String> t = new TreeMap<Calendar, String>();
Calendar c = Calendar.getInstance();
String b = "234";
t.put(c, b);
Properties properties = new Properties();
properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("MetricTest.properties"));
MetricDAO dao = Mockito.mock(MetricDAO.class);
Mockito.when(
dao.getMetricData(Matchers.anyString(), Matchers.anyString(),
Matchers.anyString(), Matchers.any(Calendar.class),
Matchers.any(Calendar.class))).thenReturn(t);
//stub (PowerMockito.method(ConfigReader.class, "readFile")).toReturn(properties);//line 36
PowerMockito.mockStatic(ConfigReader.class); //line 37
Mockito.when(ConfigReader.readFile(Matchers.anyString())).thenReturn(properties);//line 38
reportGenerator.setMetricDao(dao);
reportGenerator.prepareMonthStats();
//assert file here
}
}
此代码产生以下输出:
[testng] org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
[testng] Misplaced argument matcher detected here:
[testng] -> at .....defTest.testGetPartsSuccessful(defTest.java:38)
[testng] You cannot use argument matchers outside of verification or stubbing.
.
.
.
[testng] at myclasspackage.myclassname(class_name.java:38)
[testng] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[testng] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
[testng] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
[testng] at java.lang.reflect.Method.invoke(Method.java:597)
[testng] at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
[testng] at org.testng.internal.Invoker.invokeMethod(Invoker.java:691)
[testng] at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:883)
[testng] at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1208)
[testng] at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
[testng] at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
[testng] at org.testng.TestRunner.privateRun(TestRunner.java:753)
[testng] at org.testng.TestRunner.run(TestRunner.java:613)
[testng] at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
[testng] at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
[testng] at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
[testng] at org.testng.SuiteRunner.run(SuiteRunner.java:240)
[testng] at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
[testng] at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
[testng] at org.testng.TestNG.runSuitesSequentially(TestNG.java:1137)
[testng] at org.testng.TestNG.runSuitesLocally(TestNG.java:1062)
[testng] at org.testng.TestNG.run(TestNG.java:974)
[testng] at org.testng.TestNG.privateMain(TestNG.java:1282)
[testng] at org.testng.TestNG.main(TestNG.java:1246)
如果我注释掉第37行和第38行并使用第36行,它可以正常工作,但它不会产生所需的输出,即它不会模拟ConfigReader的静态方法,但它是模拟MetricDAO的非静态方法
我已经研究了其他类似问题的答案,但没有一个有效。请帮忙。
答案 0 :(得分:0)
根据PowerMock文档,您的测试类应该从PowerMockTestCase
扩展。还提到将此方法添加到测试类中:
@ObjectFactory
public IObjectFactory getObjectFactory() {
return new org.powermock.modules.testng.PowerMockObjectFactory();
}
但这些改变都不适合我:(
因此我切换到JMockit,它就像一个魅力。用Mockito编写的模拟与JMockit部分模拟静态方法一起工作正常。