Powermock Practice 4

本文介绍了如何使用PowerMockito库来模拟构造函数和静态方法,以解决测试覆盖率下降的问题。通过修改@PrepareForTest注解的目标类,以及引入静态工厂方法,可以正确地覆盖代码并保持高覆盖率。同时展示了PowerMockito.mockStatic()的用法,以及配置PowerMockito所需的相关依赖。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. mock new
@PrepareForTest
You need to put the class where the constructor is called into the @PrepareForTest annotation instead of 
the class which is being constructed. 
More info ref: https://github.com/powermock/powermock/wiki/MockConstructor
    
2. Issue: code coverage rate by jcoco dropped down 0, after put the tested class into @PrepareForTest().
    I put the tested class into @PrepareForTest is for using PowerMockito.whenNew to return a mock instance.
    The related test-case executed, but the coverage down to 0.
    
    Solution: I added a factory method for the newed class, so that I can get a mock instance by mocking static method.

Code making the coverage rate to 0

@RunWith(PowerMockRunner.class)
@PrepareForTest(ClassA.class)
public ClassATest {	
	@InjectMocks
	private ClassA classA;

	@Test
	public void test1(){
		ClassB mockB = mock(ClassB.class);
		PowerMockito.whenNew(ClassB.class).withAnyArguments().thenReturn(mockB);
		classA.doSomething();
	}
}

public class ClassA {
	public void doSomething(){
		ClassB b = new ClassB();
		b.f();
	}
}
public class ClassB {
	public void f(){
		System.out.println("f() invoked");
	}
}

Code getting the right coverage rate

@RunWith(PowerMockRunner.class)
@PrepareForTest(ClassB.class)
public ClassATest {	
	@InjectMocks
	private ClassA classA;

	@Test
	public void test1(){
		ClassB mockB = mock(ClassB.class);
		PowerMockito.mockStatic(ClassB.class);
		PowerMockito.when(ClassB.class, "instance").thenReturn(mockB);
		classA.doSomething();
	}
}

public class ClassA {
	public void doSomething(){
		ClassB b = ClassB.instance();
		b.f();
	}
}
public class ClassB {
	public static ClassB instance(){
		return new ClassB();
	}

	public void f(){
		System.out.println("f() invoked");
	}
}

3. static mock

@RunWith(PowerMockRunner.class)
@PrepareForTest(StaticMethodClass)
public class ClassInvokingStaticMethodTest {
	@Test
	public void test1(){
		PowerMockito.mockStatic(StaticMethodClass);
		PowerMockito.when(StaticMethodClass, staticMethodName, args ...).thenReturn(mockObj);
		ClassInvokingStaticMethod.methodInvokingStaticMethod();
	}
}

4. powermock dependency

<dependency>
	<groupId>junit</groupId>
	<artifactId>junit</artifactId> 
	<scope>test</scope>
	<version>4.12</version>
</dependency>

<dependency>
	<groupId>org.powermock</groupId>
	<artifactId>powermock-module-junit4</artifactId>
	<version>2.0.2</version>
	<scope>test</scope>
</dependency>
<dependency>
	<groupId>org.powermock</groupId>
	<artifactId>powermock-api-easymock</artifactId>
	<version>2.0.2</version>
	<scope>test</scope>
</dependency>
<dependency>
	<groupId>org.powermock</groupId>
	<artifactId>powermock-api-mockito2</artifactId>
	<version>2.0.2</version>
	<scope>test</scope>
</dependency>

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值