使用Powermock和mockito

本文介绍如何使用PowerMock进行Java单元测试,包括依赖配置、基本用法及示例。通过模拟不可见方法、构造函数调用等场景,展示PowerMock的强大功能。

使用powermock所需要的jar如下:

maven:

<!-- mock单元测试 start -->
		<dependency>  
    		<groupId>org.powermock</groupId>  
    		<artifactId>powermock-module-junit4</artifactId>  
    		<version>${powermock.version}</version>  
    		<scope>test</scope>  
		</dependency>
		<dependency>
			<groupId>org.powermock</groupId>
			<artifactId>powermock-api-mockito</artifactId>
			<version>${powermock.version}</version>
			<scope>test</scope>
		</dependency>
		<!-- mock单元测试  end -->

import java.io.File;

import org.junit.Assert;
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 com.yihaodian.wap.test.ClassDependency;
import com.yihaodian.wap.test.ClassUnderTest;

//必须要有
@RunWith(PowerMockRunner.class)
//需要模拟的类
@PrepareForTest({ClassUnderTest.class,File.class})
public class TestClassUnderTest {

    @Test
    public void testCallArgumentInstance() {
        showClassLoader("testCallArgumentInstance");
        File file = PowerMockito.mock(File.class);
        ClassUnderTest underTest = new ClassUnderTest();
        PowerMockito.when(file.exists()).thenReturn(true);
        Assert.assertTrue(underTest.callArgumentInstance(file));
    }
    
    @Test
    public void testCallInternalInstance() throws Exception {
        showClassLoader("testCallInternalInstance");
        File file = PowerMockito.mock(File.class);
        ClassUnderTest underTest = new ClassUnderTest();
        PowerMockito.whenNew(File.class).withArguments("bbb").thenReturn(file);
        PowerMockito.when(file.exists()).thenReturn(true);
        Assert.assertTrue(underTest.callInternalInstance("bbb"));
    }
    
    @Test
    public void testCallFinalMethod() {
        showClassLoader("testCallFinalMethod");
        ClassDependency depencency = PowerMockito.mock(ClassDependency.class);
        ClassUnderTest underTest = new ClassUnderTest();
        PowerMockito.when(depencency.isAlive()).thenReturn(true);
        Assert.assertTrue(underTest.callFinalMethod(depencency));
    }
    
    private void showClassLoader(String methodName) {
        System.out.println("=============="+methodName+"===============");
        System.out.println("TestClassUnderTest: " + TestClassUnderTest.class.getClassLoader());
        System.out.println("ClassUnderTest: " + ClassUnderTest.class.getClassLoader());
        System.out.println("ClassDependency: " + ClassDependency.class.getClassLoader());
    }
}


public class ClassDependency {
	
	public boolean isAlive(){
		return false;
	}

}

import java.io.File;

public class ClassUnderTest {
	
	public boolean callArgumentInstance(File file) {
		     return file.exists();
	}
	
	public boolean callInternalInstance(String path) { 
        File file = new File(path); 
        return file.exists(); 
    } 
	
	public boolean callFinalMethod(ClassDependency classDependency){
		return classDependency.isAlive();
	}

}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值