AssertJ Number Assertions - AssertJ 155

AssertJ Number Assertions – AssertJ 155

AssertJ Number Assertions

提供驗證數字等於零、正負數等方法, AssertJ 是一個 Java 庫,提供了一組豐富的斷言和真正有用的錯誤訊息,類似的流暢或鍊式寫法,語法跟自然語言相近,對於編寫測試時力求容易閱讀及維護這之上提供了相當大的改進, Asserting Number with AssertJ 使用流式斷言,並透過單元測試來驗證產出結果。

檔案目錄

./
   +- src
       +- test
       |   +- org
       |       +- ruoxue
       |           +- spring_boot_168
       |               +- test
       |                   +- assertj
       |                       +- number
       |                           +- AssertJNumberAssertionsTest.java   

單元測試

Number Elements with AssertJ 斷言數字的主要目的是取得數字以進行斷言。

isZero

驗證數字等於零,若不成立,則會拋出 AssertionError 。

	@Test
	public void isZero() {
		int value = 0;
		System.out.println(value);
		assertThat(value).isZero();

		double doubleValue = 0d;
		System.out.println(doubleValue);
		assertThat(doubleValue).isZero();
	}
0
0.0

isNotZero

Number Elements with AssertJ 驗證數字不等於零,若不成立,則會拋出 AssertionError 。

	@Test
	public void isNotZero() {
		int value = 155;
		System.out.println(value);
		assertThat(value).isNotZero();

		double doubleValue = 151.2d;
		System.out.println(doubleValue);
		assertThat(doubleValue).isNotZero();
	}
155
151.2

isPositive

Number Elements with AssertJ 驗證數字為正數,若不成立,則會拋出 AssertionError 。

	@Test
	public void isPositive() {
		int value = 155;
		System.out.println(value);
		assertThat(value).isPositive();

		double doubleValue = 151.2d;
		System.out.println(doubleValue);
		assertThat(doubleValue).isPositive();
	}
155
151.2

isNotPositive

Number Elements with AssertJ 驗證數字為負數,若不成立,則會拋出 AssertionError 。

	@Test
	public void isNotPositive() {
		int value = -155;
		System.out.println(value);
		assertThat(value).isNotPositive();

		double doubleValue = -151.2d;
		System.out.println(doubleValue);
		assertThat(doubleValue).isNotPositive();
	}
-155
-151.2

isOne

Testing Java Number with AssertJ 驗證數字等於 1 ,若不成立,則會拋出 AssertionError 。

	@Test
	public void isOne() {
		int value = 1;
		System.out.println(value);
		assertThat(value).isOne();

		double doubleValue = 1d;
		System.out.println(doubleValue);
		assertThat(doubleValue).isOne();
	}
1
1.0

isCloseTo

Testing Java Number with AssertJ 驗證數字近似偏移指定值,若不成立,則會拋出 AssertionError 。

	@Test
	public void isCloseTo() {
		int value = 155;
		System.out.println(value);
		assertThat(value).isCloseTo(150, Offset.offset(5));
		assertThat(value).isCloseTo(160, Offset.offset(5));

		double doubleValue = 151.2d;
		System.out.println(doubleValue);
		assertThat(doubleValue).isCloseTo(146d, offset(5.2d));
		assertThat(doubleValue).isCloseTo(156d, offset(5.2d));
	}
155
151.2

isNotCloseTo

Testing Java Number with AssertJ 驗證數字不近似偏移指定值,若不成立,則會拋出 AssertionError 。

	@Test
	public void isNotCloseTo() {
		int value = 155;
		System.out.println(value);
		assertThat(value).isNotCloseTo(145, Offset.offset(5));
		assertThat(value).isNotCloseTo(165, Offset.offset(5));

		double doubleValue = 151.2d;
		System.out.println(doubleValue);
		assertThat(doubleValue).isNotCloseTo(141d, offset(5.2d));
		assertThat(doubleValue).isNotCloseTo(161d, offset(5.2d));
	}
155
151.2

isCloseToWithin

Testing Java Number with AssertJ 驗證數字近似偏移指定值,若不成立,則會拋出 AssertionError 。

	@Test
	public void isCloseToWithin() {
		int value = 155;
		System.out.println(value);
		assertThat(value).isCloseTo(150, within(5));
		assertThat(value).isCloseTo(160, within(5));

		double doubleValue = 151.2d;
		System.out.println(doubleValue);
		assertThat(doubleValue).isCloseTo(146d, within(5.2d));
		assertThat(doubleValue).isCloseTo(156d, within(5.2d));
	}
155
151.2

isNotCloseToWithin

Testing Java Number with AssertJ 驗證數字不近似偏移指定值,若不成立,則會拋出 AssertionError 。

	@Test
	public void isNotCloseToWithin() {
		int value = 155;
		System.out.println(value);
		assertThat(value).isNotCloseTo(145, within(5));
		assertThat(value).isNotCloseTo(165, within(5));

		double doubleValue = 151.2d;
		System.out.println(doubleValue);
		assertThat(doubleValue).isNotCloseTo(141d, within(5.2d));
		assertThat(doubleValue).isNotCloseTo(161d, within(5.2d));
	}
155
151.2

AssertJNumberAssertionsTest.java

Testing Java Number with AssertJ 新增單元測試,驗證是否符合預期。

package org.ruoxue.spring_boot_168.test.assertj.number;

import static org.assertj.core.api.Assertions.*;

import org.assertj.core.data.Offset;
import org.junit.jupiter.api.Test;

public class AssertJNumberAssertionsTest {

	@Test
	public void isZero() {
		int value = 0;
		System.out.println(value);
		assertThat(value).isZero();

		double doubleValue = 0d;
		System.out.println(doubleValue);
		assertThat(doubleValue).isZero();
	}

	@Test
	public void isNotZero() {
		int value = 155;
		System.out.println(value);
		assertThat(value).isNotZero();

		double doubleValue = 151.2d;
		System.out.println(doubleValue);
		assertThat(doubleValue).isNotZero();
	}

	@Test
	public void isPositive() {
		int value = 155;
		System.out.println(value);
		assertThat(value).isPositive();

		double doubleValue = 151.2d;
		System.out.println(doubleValue);
		assertThat(doubleValue).isPositive();
	}

	@Test
	public void isNotPositive() {
		int value = -155;
		System.out.println(value);
		assertThat(value).isNotPositive();

		double doubleValue = -151.2d;
		System.out.println(doubleValue);
		assertThat(doubleValue).isNotPositive();
	}

	@Test
	public void isOne() {
		int value = 1;
		System.out.println(value);
		assertThat(value).isOne();

		double doubleValue = 1d;
		System.out.println(doubleValue);
		assertThat(doubleValue).isOne();
	}

	@Test
	public void isCloseTo() {
		int value = 155;
		System.out.println(value);
		assertThat(value).isCloseTo(150, Offset.offset(5));
		assertThat(value).isCloseTo(160, Offset.offset(5));

		double doubleValue = 151.2d;
		System.out.println(doubleValue);
		assertThat(doubleValue).isCloseTo(146d, offset(5.2d));
		assertThat(doubleValue).isCloseTo(156d, offset(5.2d));
	}

	@Test
	public void isNotCloseTo() {
		int value = 155;
		System.out.println(value);
		assertThat(value).isNotCloseTo(145, Offset.offset(5));
		assertThat(value).isNotCloseTo(165, Offset.offset(5));

		double doubleValue = 151.2d;
		System.out.println(doubleValue);
		assertThat(doubleValue).isNotCloseTo(141d, offset(5.2d));
		assertThat(doubleValue).isNotCloseTo(161d, offset(5.2d));
	}

	@Test
	public void isCloseToWithin() {
		int value = 155;
		System.out.println(value);
		assertThat(value).isCloseTo(150, within(5));
		assertThat(value).isCloseTo(160, within(5));

		double doubleValue = 151.2d;
		System.out.println(doubleValue);
		assertThat(doubleValue).isCloseTo(146d, within(5.2d));
		assertThat(doubleValue).isCloseTo(156d, within(5.2d));
	}

	@Test
	public void isNotCloseToWithin() {
		int value = 155;
		System.out.println(value);
		assertThat(value).isNotCloseTo(145, within(5));
		assertThat(value).isNotCloseTo(165, within(5));

		double doubleValue = 151.2d;
		System.out.println(doubleValue);
		assertThat(doubleValue).isNotCloseTo(141d, within(5.2d));
		assertThat(doubleValue).isNotCloseTo(161d, within(5.2d));
	}
}

心得分享

Assert Number Java 除了提供流式判斷,還針對 Number 做特殊判斷,在許多測試驗證的場景,讓開發者使用更流暢的驗證,不需要再寫迴圈,善用 Testing Java Number with AssertJ 將有助於驗證效率的提升。

發佈留言