AssertJ String Methods - AssertJ 155

AssertJ String Methods – AssertJ 155

AssertJ String Methods

是一個 Java 庫,提供了一組豐富的斷言和真正有用的錯誤訊息,驗證 String 是否為空值、空集合、指定類別實例、字串相等, 提高了測試程式碼的可讀性,使用 AssertJ String Java Methods 流式斷言,本篇增加了範例,並透過單元測試來驗證產出結果。

檔案目錄

./
   +- src
       +- test
       |   +- org
       |       +- ruoxue
       |           +- spring_boot_168
       |               +- test
       |                   +- assertj
       |                       +- string
       |                           +- AssertJStringMethodsTest.java   

單元測試

AssertJ String Methods Java 斷言字串的主要目的是取得字串以進行斷言。

isNull

驗證是否為空值,若不成立,則會拋出 AssertionError 。

	@Test
	public void isNull() {
		String value = null;
		System.out.println(value);
		assertThat(value).isNull();

		String value2 = null;
		System.out.println(value2);
		assertThat(value2).isNull();
	}
null
null

isNotNull

AssertJ String Methods Java 驗證是否不為空值,若不成立,則會拋出 AssertionError 。

	@Test
	public void isNotNull() {
		String value = "AssertJ 155";
		System.out.println(value);
		assertThat(value).isNotNull();

		value = "AssertJ";
		System.out.println(value);
		assertThat(value).isNotNull();
	}
AssertJ 155
AssertJ

isEmpty

AssertJ String Methods Java 驗證是否為空集合,若不成立,則會拋出 AssertionError 。

	@Test
	public void isEmpty() {
		String value = "";
		System.out.println(value);
		assertThat(value).isEmpty();

		String value2 = "";
		System.out.println(value2);
		assertThat(value2).isEmpty();
	}


isNotEmpty

AssertJ String Methods Java 驗證是否不為空集合,若不成立,則會拋出 AssertionError 。

	@Test
	public void isNotEmpty() {
		String value = "AssertJ 155";
		System.out.println(value);
		assertThat(value).isNotEmpty();

		value = "AssertJ";
		System.out.println(value);
		assertThat(value).isNotEmpty();
	}
AssertJ 155
AssertJ

isNullOrEmpty

AssertJ String Methods Java 驗證是否為空值或空集合,若不成立,則會拋出 AssertionError 。

	@Test
	public void isNullOrEmpty() {
		String value = null;
		System.out.println(value);
		assertThat(value).isNullOrEmpty();
		value = "";
		System.out.println(value);
		assertThat(value).isNullOrEmpty();
	}
null

isEqualTo

AssertJ String Methods in Java 驗證字串是否相等,若不成立,則會拋出 AssertionError 。

	@Test
	public void isEqualTo() {
		String value = "AssertJ 155";
		System.out.println(value);
		assertThat(value).isEqualTo("AssertJ 155");

		value = "AssertJ";
		System.out.println(value);
		assertThat(value).isEqualTo("AssertJ");
	}
AssertJ 155
AssertJ

isNotEqualTo

AssertJ String Methods in Java 驗證字串是否不相等,若不成立,則會拋出 AssertionError 。

	@Test
	public void isNotEqualTo() {
		String value = "AssertJ 155";
		System.out.println(value);
		assertThat(value).isNotEqualTo("AssertJ");

		value = "AssertJ";
		System.out.println(value);
		assertThat(value).isNotEqualTo("JUnit");
	}
AssertJ 155
AssertJ

isSameAs

AssertJ String Functions in Java 驗證實例是否相等,若不成立,則會拋出 AssertionError 。

	@Test
	public void isSameAs() {
		String value = "AssertJ 155";
		String value2 = value;
		System.out.println(value);
		System.out.println(value2);
		assertThat(value).isSameAs(value2);

		value = "AssertJ";
		value2 = value;
		System.out.println(value);
		System.out.println(value2);
		assertThat(value).isSameAs(value2);
	}
AssertJ 155
AssertJ

isNotSameAs

AssertJ String Functions in Java 驗證實例是否不相等,若不成立,則會拋出 AssertionError 。

	@Test
	public void isNotSameAs() {
		String value = "AssertJ 155";
		String value2 = new String("AssertJ 155");
		System.out.println(value);
		System.out.println(value2);
		assertThat(value).isNotSameAs(value2);

		value = "AssertJ";
		value2 = new String("AssertJ");
		System.out.println(value);
		System.out.println(value2);
		assertThat(value).isNotSameAs(value2);
	}
AssertJ 155
AssertJ

AssertJStringMethodsTest.java

AssertJ String Methods in Java 新增單元測試,驗證是否符合預期。

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

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

import org.junit.jupiter.api.Test;

public class AssertJStringMethodsTest {

	@Test
	public void isNull() {
		String value = null;
		System.out.println(value);
		assertThat(value).isNull();

		String value2 = null;
		System.out.println(value2);
		assertThat(value2).isNull();
	}

	@Test
	public void isNotNull() {
		String value = "AssertJ 155";
		System.out.println(value);
		assertThat(value).isNotNull();

		value = "AssertJ";
		System.out.println(value);
		assertThat(value).isNotNull();
	}

	@Test
	public void isEmpty() {
		String value = "";
		System.out.println(value);
		assertThat(value).isEmpty();

		String value2 = "";
		System.out.println(value2);
		assertThat(value2).isEmpty();
	}

	@Test
	public void isNotEmpty() {
		String value = "AssertJ 155";
		System.out.println(value);
		assertThat(value).isNotEmpty();

		value = "AssertJ";
		System.out.println(value);
		assertThat(value).isNotEmpty();
	}

	@Test
	public void isNullOrEmpty() {
		String value = null;
		System.out.println(value);
		assertThat(value).isNullOrEmpty();
		value = "";
		System.out.println(value);
		assertThat(value).isNullOrEmpty();
	}

	@Test
	public void isEqualTo() {
		String value = "AssertJ 155";
		System.out.println(value);
		assertThat(value).isEqualTo("AssertJ 155");

		value = "AssertJ";
		System.out.println(value);
		assertThat(value).isEqualTo("AssertJ");
	}

	@Test
	public void isNotEqualTo() {
		String value = "AssertJ 155";
		System.out.println(value);
		assertThat(value).isNotEqualTo("AssertJ");

		value = "AssertJ";
		System.out.println(value);
		assertThat(value).isNotEqualTo("JUnit");
	}

	@Test
	public void isSameAs() {
		String value = "AssertJ 155";
		String value2 = value;
		System.out.println(value);
		System.out.println(value2);
		assertThat(value).isSameAs(value2);

		value = "AssertJ";
		value2 = value;
		System.out.println(value);
		System.out.println(value2);
		assertThat(value).isSameAs(value2);
	}

	@Test
	public void isNotSameAs() {
		String value = "AssertJ 155";
		String value2 = new String("AssertJ 155");
		System.out.println(value);
		System.out.println(value2);
		assertThat(value).isNotSameAs(value2);

		value = "AssertJ";
		value2 = new String("AssertJ");
		System.out.println(value);
		System.out.println(value2);
		assertThat(value).isNotSameAs(value2);
	}
}

心得分享

AssertJ String Functions in Java 為用 Java 編寫的測試程式提供了流暢的斷言語句,這些斷言語句通常與 JUnit 測試一起使用, AssertJ 斷言的基本方法是 assertThat 方法,善用 AssertJ String Methods in Java 將有助於驗證效率的提升。

發佈留言