Powermock Practice 4

4 篇文章 0 订阅

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>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值