PowerMock的初使用

在写UT的时候,会经常使用到Mockito来写测试用例,但是Mockito无法对静态方法、私有方法以及final方法进行mock。这里可以使用功能强大的PowerMock来实现。
 PowerMock使用一个自定义类加载器和字节码操作来模拟静态方法,构造函数,final类和方法,私有方法,去除静态初始化器等等。通过使用自定义的类加载器,简化采用的IDE或持续集成服务器不需要做任何改变。目前PowerMock支持EasyMock和Mockito。
 闲话少说,先上代码:
 这里需要用到一些依赖包,项目用maven管理,添加如下依赖

 		<dependency>
            <groupId>org.powermockgroupId>
            <artifactId>powermock-api-mockito2artifactId>
            <version>2.0.2version>
            <scope>testscope>
        dependency>

        
        <dependency>
            <groupId>org.powermockgroupId>
            <artifactId>powermock-coreartifactId>
            <version>2.0.2version>
            <scope>testscope>
        dependency>
        
        <dependency>
            <groupId>org.mockitogroupId>
            <artifactId>mockito-allartifactId>
            <version>2.0.2-betaversion>
            <scope>testscope>
        dependency>
		 <dependency>
            <groupId>org.powermockgroupId>
            <artifactId>powermock-module-junit4artifactId>
            <version>2.0.2version>
            <scope>testscope>
        dependency>

先是一个util类:Calculate.java

public class CalculateUtil {

    public static int add(int a, int b){
        return a + b;
    }
}

然后是测试类

import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;

import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import static org.junit.Assert.*;


@RunWith(PowerMockRunner.class)
@PrepareForTest( CalculateUtil.class )
public class CalculateUtilTest {

    @Test
    public void add() {
        PowerMockito.mockStatic(CalculateUtil.class);
        PowerMockito.when(CalculateUtil.add(1, 1)).thenReturn(0);
        assertEquals(0,CalculateUtil.add(1,1));
    }
}

这里可以看出,我们让add方法在执行1+1操作时,返回的结果为0,然后使用assertEquals返回的是相等。可以让静态方法返回指定值了。

异常:
当执行报如下异常时:

java.lang.NoClassDefFoundError: org/powermock/core/classloader/annotations/Mock

	at org.powermock.api.extension.listener.AnnotationEnabler.getMockAnnotations(AnnotationEnabler.java:128)
	at org.powermock.api.extension.listener.AnnotationEnabler.standardInject(AnnotationEnabler.java:84)
	at org.powermock.api.extension.listener.AnnotationEnabler.beforeTestMethod(AnnotationEnabler.java:63)
	at org.powermock.tests.utils.impl.PowerMockTestNotifierImpl.notifyBeforeTestMethod(PowerMockTestNotifierImpl.java:82)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:308)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:131)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.access$100(PowerMockJUnit47RunnerDelegateImpl.java:59)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner$TestExecutorStatement.evaluate(PowerMockJUnit47RunnerDelegateImpl.java:147)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.evaluateStatement(PowerMockJUnit47RunnerDelegateImpl.java:107)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:298)
	at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:87)
	at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:50)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:218)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:160)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:134)
	at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
	at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:136)
	at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:117)

这里应该是导入的包出错了。我当时就遇到了这个情况,是


            org.powermock
            powermock-api-mockito2
            2.0.2
            test
        

导入错了,导入成了


            org.powermock
            powermock-api-mockito
            2.0.2
            test
        

注意:两者的artifctld的不一样的,要使用有2的这个

你可能感兴趣的:(PowerMock,UT,java)