SpringBoot——使用powermock进行静态方法的单元测试


技术公众号:后端技术解忧铺
关注微信公众号:CodingTechWork,一起学习进步。

引言

  xxxService类引用了xxxUtils类的xxxstaticMethod()方法,当对xxxService类进行单元测试的时候,如何进行呢?本文介绍了,使用powermock进行静态方法的mock。

service层单测

无静态方法引用

依赖

<dependency>
   <groupId>org.junit.platform</groupId>
   <artifactId>junit-platform-launcher</artifactId>
   <scope>test</scope>
</dependency>
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
</dependency>

service层类

@Service
public class DemoServiceImpl implements DemoService {
   @Override
   public String aMethod(String key) {
       ... ...
   }
}

service层测试类

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoServiceImplTest{
    @Autowired
    private DemoService demoService;
    
    @Test
    public void testAMethod() {
		String key = "xxx";
		String res = "yyy";
		Assert.assertEquals(demoService.aMethod(key), res);
    }
    
}

有静态方法引用

依赖

<dependency>
   <groupId>org.junit.platform</groupId>
   <artifactId>junit-platform-launcher</artifactId>
   <scope>test</scope>
</dependency>
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
   <groupId>org.powermock</groupId>
   <artifactId>powermock-api-mockito2</artifactId>
   <version>2.0.9</version>
</dependency>
<dependency>
   <groupId>org.powermock</groupId>
   <artifactId>powermock-module-junit4</artifactId>
   <version>2.0.9</version>
</dependency>

service层类

@Service
public class DemoServiceImpl implements DemoService {
   @Override
   public String aMethod(String key) {
       ... ...
       try {
       String res0 = XXXUtils.staticBMethod(key);
       ... ...
	   } catch (IOException) {
	   ... ...
	   }
       ... ...
   }
}

service层测试类

import xxx.xxx.XXXUtil;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.PowerMockRunnerDeletegate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;

@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@PrepareForTest(XXXUtil.class)
@PowerMockIgnore("javax.management.*")
public class DemoServiceImplTest{
    @Autowired
    private DemoService demoService;
    
    @Test
    public void testAMethodWithoutException() {
		String key = "xxx";
		String res0 = "yyy";
		String res = "zzz";
		PowerMockitio.mockStatic(XXXUtil.class);
		PowerMockito.when(XXXUtil.staticBMethod(Mockito.anyString())).thenReturn(res0);
		Assert.assertEquals(demoService.aMethod(key), res);
    }
    @Test
    public void testAMethodWithException() {
		String key = "xxx";
		String res0 = "yyy";
		String res = "zzz";
		PowerMockitio.mockStatic(XXXUtil.class);
		PowerMockito.when(XXXUtil.staticBMethod(Mockito.anyString())).thenThrow(IOException.class);
		Assert.assertEquals(demoService.aMethod(key), res);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值