单元测试参数化

总共2步
第一步,添加依赖

org.junit.jupiter
junit-jupiter-params
5.2.0
test

第二步,编写用例
主要使用@ParameterizedTest 和@ValueSource两个注解

import org.junit.
Test
;

import org.junit.jupiter.params.
ParameterizedTest
;

import org.junit.jupiter.params.provider.
ValueSource
;

import org.junit.runner.
RunWith
;

import org.springframework.test.context.junit4.SpringRunner
;

@RunWith(SpringRunner.
class)

public
class SomeServiceTest {

@ParameterizedTest

@ValueSource(ints = {
1
,
2
,
3})

public
void
testInt(
int num) {

    System.

out.println(num)
;

}

@ParameterizedTest

@ValueSource(strings = {
“a”
,
“b”
,
“c”})

public
void
testStr(String str)
throws Exception {

    System.

out.println(str)
;

}

@Test

public void
testOther() {

}

}

常见问题及解决办法

  1. java.lang.Exception: No runnable methods
    如果测试类使用@RunWith(SpringRunner.class),则该类需要有至少一个无参测试方法,如上例中的testOther()方法
  2. 校验不通过的坑
    Mockito.verify(abc,Mockito.atLeastOnce()).doSomething(Mockito.anyString()),但是具体方法调用的时候入参是null,那这个校验不通过,大概是因为null不能用任意字符串表示
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值