带有TestNG和Mockito框架的PowerMockito

时间:2015-01-29 06:41:28

标签: java testng mockito powermock

我在我的项目中使用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模块-TestNG的剂-1.6.1.jar
  • 的Mockito-全1.9.5.jar
  • TestNG的-6.8.7.jar
  • objenesis-2.1.jar
  • 了Javassist-3.18.1-GA.jar
  • CGLIB的节点p-2.2.2.jar
  • BSH-2.0b4.jar
  • hamcrest核-1.1.jar
  • jcommander-1.27.jar
  • snakeyaml-1.12.jar

请帮忙。

2 个答案:

答案 0 :(得分:1)

为什么你甚至想模拟静态方法?为什么不将这些静态方法包装在可以用mockito模拟的类中?

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

然后你可以创建一个你的Wrapper的模拟。根本不需要使用Powermock ......

答案 1 :(得分:0)

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

application/json

并且仅从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{ 传递给了Mockito,但是当Mockito尝试将它投射到PowerMockMaker时,有来自两个不同类加载器的类,导致MockMaker

另一个想法是确保ClassCastExceptionpowermock-mockito依赖JAR是相同的版本。