2

我正在尝试使用 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 的非静态方法。

我已经查看了其他类似问题的答案,但没有一个有效。请帮忙。

4

1 回答 1

0

根据PowerMock文档,您的测试类应该从PowerMockTestCase. 还提到将此方法添加到您的测试类:

 @ObjectFactory
 public IObjectFactory getObjectFactory() {
     return new org.powermock.modules.testng.PowerMockObjectFactory();
 }

但是这些更改都没有对我有用:(

因此,我切换到了 JMockit,它就像一个魅力一样工作。用 Mockito 编写的模拟与 JMockit 对静态方法的部分模拟一起工作得很好。

于 2013-01-09T23:03:09.517 回答