Junit4 斷言新方法

來源:互聯網
上載者:User

標籤:rtt   大小   tps   ESS   html   string   org   lib   contains   

話不多少說,直接上代碼

package ASSERTTEST;


import org.junit.Assert;

import org.hamcrest.*;
import org.junit.Test;
import static org.junit.Assert.*;

import java.util.List;
import java.util.Map;

import static org.hamcrest.Matchers.*;

 

public class MYTEST {


@Test

public void testAdd() {

 

//一般匹配符

int s = new T().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 = 5.0;

//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 T().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 T().getList("Magci");

//hasItem:Iterable變數中含有指定元素時,測試通過

assertThat(l, hasItem("Magci"));

 

Map<String, String> m = new T().getMap("mgc", "Magci");

//hasEntry:Map變數中含有指定索引值對時,測試通過

assertThat(m, hasEntry("mgc", "Magci"));

//hasKey:Map變數中含有指定鍵時,測試通過

assertThat(m, hasKey("mgc"));

//hasValue:Map變數中含有指定值時,測試通過

assertThat(m, hasValue("Magci"));
}}

 

最坑的就是這幾個jar 的版本了,,

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

<!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-core -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3.RC2</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-library -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3.RC2</version>
<scope>test</scope>
</dependency>

 

原文:https://www.cnblogs.com/anyehome/p/7113113.html

Junit4 斷言新方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.