Table of Contents
ToggleUnit Test Number with AssertJ
提供了一組豐富的斷言和真正有用的錯誤訊息,提高了測試程式碼的可讀性,通常與 JUnit 測試一起使用, Unit Test Java AssertJ Number 流式斷言,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- spring_boot_168
| +- test
| +- assertj
| +- number
| +- UnitTestNumberWithAssertJTest.java
單元測試
Java AssertJ Number Unit Test 斷言數字的主要目的是取得數字以進行斷言。
is
驗證是否符合條件,若不成立,則會拋出 AssertionError 。
@Test
public void is() {
int value = 1;
System.out.println(value);
Condition<Integer> greaterThan = new Condition<Integer>(i -> i > 0, "greaterThan");
assertThat(value).is(greaterThan);
double doubleValue = 1d;
System.out.println(doubleValue);
Condition<Double> doubleGreaterThan = new Condition<Double>(i -> Double.compare(i, 0) == 1,
"doubleGreaterThan");
assertThat(doubleValue).is(doubleGreaterThan);
}
1
1.0
isNot
Java AssertJ Number Unit Test 驗證是否不符合條件,若不成立,則會拋出 AssertionError 。
@Test
public void isNot() {
int value = 1;
System.out.println(value);
Condition<Integer> greaterThan = new Condition<Integer>(i -> i < 0, "greaterThan");
assertThat(value).isNot(greaterThan);
double doubleValue = 1d;
System.out.println(doubleValue);
Condition<Double> doubleGreaterThan = new Condition<Double>(i -> Double.compare(i, 0) == -1,
"doubleGreaterThan");
assertThat(doubleValue).isNot(doubleGreaterThan);
}
1
1.0
isIn
Java AssertJ Number Unit Test 驗證是否包含於陣列中,若不成立,則會拋出 AssertionError 。
@Test
public void isIn() {
int value = 1;
System.out.println(value);
assertThat(value).isIn(1, 3, 5);
double doubleValue = 1d;
System.out.println(doubleValue);
assertThat(doubleValue).isIn(1d, 2d, 3d);
}
1
1.0
isNotIn
Java AssertJ Number Unit Test 驗證是否不包含於陣列中,若不成立,則會拋出 AssertionError 。
@Test
public void isNotIn() {
String value = "AssertJ";
System.out.println(value);
assertThat(value).isNotIn("JUnit", "151");
value = "155";
System.out.println(value);
assertThat(value).isNotIn("JUnit", "151");
}
1
1.0
isInstanceOf
Java AssertJ Number Unit Test 驗證是否為指定類別實例,若不成立,則會拋出 AssertionError 。
@Test
public void isInstanceOf() {
int value = 1;
System.out.println(value);
assertThat(value).isInstanceOf(Integer.class);
double doubleValue = 1d;
System.out.println(doubleValue);
assertThat(doubleValue).isInstanceOf(Double.class);
}
1
1.0
isNotInstanceOf
Java AssertJ Number Unit Test 驗證是否不為指定類別實例,若不成立,則會拋出 AssertionError 。
@Test
public void isNotInstanceOf() {
int value = 1;
System.out.println(value);
assertThat(value).isNotInstanceOf(int.class);
double doubleValue = 1d;
System.out.println(doubleValue);
assertThat(doubleValue).isNotInstanceOf(double.class);
}
1
1.0
UnitTestNumberWithAssertJTest.java
Java Unit Test AssertJ Number 新增單元測試,驗證是否符合預期。
package org.ruoxue.spring_boot_168.test.assertj.number;
import static org.assertj.core.api.Assertions.assertThat;
import org.assertj.core.api.Condition;
import org.junit.jupiter.api.Test;
public class UnitTestNumberWithAssertJTest {
@Test
public void is() {
int value = 1;
System.out.println(value);
Condition<Integer> greaterThan = new Condition<Integer>(i -> i > 0, "greaterThan");
assertThat(value).is(greaterThan);
double doubleValue = 1d;
System.out.println(doubleValue);
Condition<Double> doubleGreaterThan = new Condition<Double>(i -> Double.compare(i, 0) == 1,
"doubleGreaterThan");
assertThat(doubleValue).is(doubleGreaterThan);
}
@Test
public void isNot() {
int value = 1;
System.out.println(value);
Condition<Integer> greaterThan = new Condition<Integer>(i -> i < 0, "greaterThan");
assertThat(value).isNot(greaterThan);
double doubleValue = 1d;
System.out.println(doubleValue);
Condition<Double> doubleGreaterThan = new Condition<Double>(i -> Double.compare(i, 0) == -1,
"doubleGreaterThan");
assertThat(doubleValue).isNot(doubleGreaterThan);
}
@Test
public void isIn() {
int value = 1;
System.out.println(value);
assertThat(value).isIn(1, 3, 5);
double doubleValue = 1d;
System.out.println(doubleValue);
assertThat(doubleValue).isIn(1d, 2d, 3d);
}
@Test
public void isNotIn() {
int value = 1;
System.out.println(value);
assertThat(value).isNotIn(2, 4, 6);
double doubleValue = 1d;
System.out.println(doubleValue);
assertThat(doubleValue).isNotIn(2d, 4d, 6d);
}
@Test
public void isInstanceOf() {
int value = 1;
System.out.println(value);
assertThat(value).isInstanceOf(Integer.class);
double doubleValue = 1d;
System.out.println(doubleValue);
assertThat(doubleValue).isInstanceOf(Double.class);
}
@Test
public void isNotInstanceOf() {
int value = 1;
System.out.println(value);
assertThat(value).isNotInstanceOf(int.class);
double doubleValue = 1d;
System.out.println(doubleValue);
assertThat(doubleValue).isNotInstanceOf(double.class);
}
}
心得分享
AssertJ Number Java Unit Test 斷言的基本方法是 assertThat 方法,提高了測試程式碼的可讀性,並且被設計為在主流 IDE 中易於使用,讓開發者將繁瑣的測試驗證過程,變為更簡單,善用 Java Unit Test AssertJ Number 將有助於驗證效率的提升。