PowerMockito与TestNG和Mockito框架



我正在我的项目中使用Mockito+TestNG框架。它用于正常工作,但为了模拟我在类路径中注入PowerMock库并更新代码以使用 PowerMock 的一些静态方法。
我发现每当我尝试执行时:

 PowerMockito.doNothing().when(CustomImportUtil.class, "init", serviceMgr);

测试用例在初始化mockobject [s = mock(Service.class);]时失败,并显示错误消息:


java.lang.ExceptionInInitializerError
    at org.mockito.internal.exceptions.stacktrace.ConditionalStackTraceFilter.<init>(ConditionalStackTraceFilter.java:17)
    at org.mockito.exceptions.base.MockitoException.filterStackTrace(MockitoException.java:30)
    at org.mockito.exceptions.base.MockitoException.<init>(MockitoException.java:19)
    at org.mockito.exceptions.misusing.MockitoConfigurationException.<init>(MockitoConfigurationException.java:18)
    at org.mockito.internal.configuration.ClassPathLoader.loadImplementations(ClassPathLoader.java:145)
    at org.mockito.internal.configuration.ClassPathLoader.findPluginImplementation(ClassPathLoader.java:110)
    at org.mockito.internal.configuration.ClassPathLoader.findPlatformMockMaker(ClassPathLoader.java:106)
    at org.mockito.internal.configuration.ClassPathLoader.<clinit>(ClassPathLoader.java:59)
    at org.mockito.internal.configuration.GlobalConfiguration.createConfig(GlobalConfiguration.java:38)
    at org.mockito.internal.configuration.GlobalConfiguration.<init>(GlobalConfiguration.java:32)
    at org.mockito.MockitoAnnotations.initMocks(MockitoAnnotations.java:94)
    at com.facetime.imcoreserver.importer.facetime.collaboration.CVImporterTestCase.beforeMethod(CVImporterTestCase.java:93)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
    at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:653)
    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)
Caused by: java.lang.NullPointerException
    at org.mockito.internal.exceptions.stacktrace.StackTraceFilter.<clinit>(StackTraceFilter.java:21)
    ... 38 more

下面是我的示例 java 测试类。 请帮助我解决此问题。


package com.facetime.imcoreserver.importer.facetime.collaboration;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.internal.verification.Times;
//import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.testng.PowerMockTestCase;
import org.testng.Assert;
import org.testng.IObjectFactory;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.ObjectFactory;
import org.testng.annotations.Test;
import com.actiance.coreserver.frameWork.CustomImportUtil;
import com.facetime.ftcore.service.Service;

@PrepareForTest(CustomImportUtil.class)
public class MyTestCase extends PowerMockTestCase{
    MYImporter importer = null;
    Service s = null;
    String importerName = "MYImporter";
    String fileLocation = "Location";
    int networkId = 2;
    int companyId = -1;
    int importerNum = 1;
    int serverId = 1; 
    private IMNetworkConnectorInfo networkConnectorInfo = null;
    private IMAuditNetworkRegistrationDBMgr networkRegistrationDBMgr = null;
    public void initialization() {
        System.out.println("nitesh");
    }
    @BeforeMethod(groups = { "import" })
    public void beforeMethod(){
        s = mock(Service.class);
        PowerMockito.mockStatic( CustomImportUtil.class);
    }
    @ObjectFactory
    public IObjectFactory getObjectFactory() {
        return new org.powermock.modules.testng.PowerMockObjectFactory();
    }
    @Test(groups = { "import" })
    public  void testMYImporterConstructorCaseCustomPropsAreEmpty(){
        mockMYImporter();
        mockInitCustomImport(configProperties, libProperties);
        boolean exCaught = false;
        try {
            importer = new MYImporter(s, importerNum, companyId, 0);
        } catch (CVImportConfigException e2) {
            exCaught = true;
        }
        Assert.assertNull(importer, "MYImporter object should be null");
    }

    private void mockMYImporter(){
        when(s.service(FTServiceMgr.class)).thenReturn(serviceMgr);
        try {
            //PowerMockito.spy(CustomImportUtil.class);
            //PowerMockito.doNothing().when(CustomImportUtil.class);
            PowerMockito.doNothing().when(CustomImportUtil.class, "init", serviceMgr);
        } catch (Exception e) {
        }
    }

}

我的模拟和测试库是:

  • Powermock-mockito-1.5.3-full.jar
  • powermock-module-testng-agent-1.6.1.jar
  • 模拟全部-1.9.5.jar
  • 测试-6.8.7.jar
  • 对象-2.1.jar
  • javassist-3.18.1-GA
  • .jar
  • cglib-nodep-2.2.2.jar
  • BSH-2.0B4.jar
  • 哈姆克雷斯特核心-1.1.jar
  • 指挥官-1.27.jar
  • 斯纳克亚姆尔-1.12.jar

请帮忙。

为什么还要模拟静态方法?为什么不将这些静态方法包装在一个可以用 mockito 模拟的类中呢?

class CustomerUtilWraper {
   void someMethod() {
     CustomerUtil.someStaticMethod()
   }
}

然后,您可以创建包装器的模拟。根本不需要使用Powermock...

我在使用相同的堆栈跟踪时遇到了问题,我通过从测试类中删除 PowerMock 对象工厂方法解决了它

// delete this method from your test class
@ObjectFactory
public IObjectFactory getObjectFactory() {
    return new org.powermock.modules.testng.PowerMockObjectFactory();
}

并且仅从 PowerMock 测试用例扩展。

// keep this the way it is
public class MyTestCase extends PowerMockTestCase{

我对这个问题进行了一些深入研究,看起来 PowerMock 将PowerMockMaker传递给了 Mockito,但是当 Mockito 尝试将其转换为MockMaker时,有两个不同类加载器的类,导致ClassCastException


另一个想法是确保powermock-mockitopowermock-module-testng-agent依赖项 JAR 是相同的版本。

相关内容

  • 没有找到相关文章

最新更新