springboot单元测试
  7t9tJT00tBi8 2023年11月02日 53 0


一:junit测试

当你的单元测试代码不需要用到 Spring Boot 功能,而只是一个简单的测试时,你可以直接编写你的 Junit 测试代码:

public class SimpleJunitTest {
   
    @Test
    public void testSayHi() {
        System.out.println("Hi Junit.");
    }
   
}

springboot单元测试_单元测试

 

二:集成测试

springboot单元测试_字符串_02

 

springboot单元测试_字符串_03

 三:单元测试

springboot单元测试_ci_04

 

springboot单元测试_字符串_05

springboot单元测试_ci_06

 四:Assert断言

springboot单元测试_字符串_07

 

springboot单元测试_ci_08

 

springboot单元测试_单元测试_09

 assertThat(String reason, T actual, Matcher matcher) :要求matcher.matches(actual) == true,使用Matcher做自定义的校验。String reason为断言失败时输出的reason

springboot单元测试_单元测试_10

 

springboot单元测试_ci_11

 

public class C {
    public int add(int a, int b) {
        return a + b;
    }

    public double div(double a, double b) {
        return a / b;
    }

    public String getName(String name) {
        return name;
    }

    public List<String> getList(String item) {
        List<String> l = new ArrayList<String>();
        l.add(item);
        return l;
    }

    public Map<String, String> getMap(String key, String value) {
        Map<String, String> m = new HashMap<String, String>();
        m.put(key, value);
        return m;
    }
}
@Test
public void testC() {

    // 一般匹配符
    int s = new C().add(1, 1);
    // allOf:所有条件必须都成立,测试才通过
    assertThat(s, allOf(greaterThan(1), lessThan(3)));
    // anyOf:只要有一个条件成立,测试就通过
    assertThat(s, anyOf(greaterThan(1), lessThan(1)));
    // anything:无论什么条件,测试都通过
    assertThat(s, anything());
    // is:变量的值等于指定值时,测试通过
    assertThat(s, is(2));
    // not:和is相反,变量的值不等于指定值时,测试通过
    assertThat(s, not(1));

    // 数值匹配符
    double d = new C().div(10, 3);
    // closeTo:浮点型变量的值在3.0±0.5范围内,测试通过
    assertThat(d, closeTo(3.0, 0.5));
    // greaterThan:变量的值大于指定值时,测试通过
    assertThat(d, greaterThan(3.0));
    // lessThan:变量的值小于指定值时,测试通过
    assertThat(d, lessThan(3.5));
    // greaterThanOrEuqalTo:变量的值大于等于指定值时,测试通过
    assertThat(d, greaterThanOrEqualTo(3.3));
    // lessThanOrEqualTo:变量的值小于等于指定值时,测试通过
    assertThat(d, lessThanOrEqualTo(3.4));

    // 字符串匹配符
    String n = new C().getName("Magci");
    // containsString:字符串变量中包含指定字符串时,测试通过
    assertThat(n, containsString("ci"));
    // startsWith:字符串变量以指定字符串开头时,测试通过
    assertThat(n, startsWith("Ma"));
    // endsWith:字符串变量以指定字符串结尾时,测试通过
    assertThat(n, endsWith("i"));
    // euqalTo:字符串变量等于指定字符串时,测试通过
    assertThat(n, equalTo("Magci"));
    // equalToIgnoringCase:字符串变量在忽略大小写的情况下等于指定字符串时,测试通过
    assertThat(n, equalToIgnoringCase("magci"));
    // equalToIgnoringWhiteSpace:字符串变量在忽略头尾任意空格的情况下等于指定字符串时,测试通过
    assertThat(n, equalToIgnoringWhiteSpace(" Magci   "));

    // 集合匹配符
    List<String> l = new C().getList("Magci");
    // hasItem:Iterable变量中含有指定元素时,测试通过
    assertThat(l, hasItem("Magci"));

    Map<String, String> m = new C().getMap("mgc", "Magci");
    // hasEntry:Map变量中含有指定键值对时,测试通过
    assertThat(m, hasEntry("mgc", "Magci"));
    // hasKey:Map变量中含有指定键时,测试通过
    assertThat(m, hasKey("mgc"));
    // hasValue:Map变量中含有指定值时,测试通过
    assertThat(m, hasValue("Magci"));
}

springboot单元测试_ci_12

 Mock方法详解

java单元测试_mock系列 - 你的雷哥

作者:你的雷哥


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  anLrwkgbyYZS   2023年12月30日   28   0   0 i++iosi++ioscici
  anLrwkgbyYZS   2023年12月30日   32   0   0 ideciciMaxideMax
7t9tJT00tBi8