我正在尝试使用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的非静态方法。
我研究了其他类似问题提供的答案,但它们都没有起作用。请帮助。
根据PowerMock文档,您的测试类应从PowerMockTestCase
延长。还提到了将此方法添加到您的测试类中:
@ObjectFactory
public IObjectFactory getObjectFactory() {
return new org.powermock.modules.testng.PowerMockObjectFactory();
}
但是这些更改都不适合我:(
因此,我切换到Jmockit,它像魅力一样工作。用Mockito编写的模拟与Jmockit的部分模拟静态方法一起工作。