From 550291e2e0adaa2b451e8306d1156bcafa6e1e39 Mon Sep 17 00:00:00 2001 From: YuCheng Hu Date: Mon, 6 Jun 2022 12:26:06 -0400 Subject: [PATCH] =?UTF-8?q?Java=208=20AssertJ=20=E7=9A=84=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=EF=BC=88Exception=20=EF=BC=89=E6=96=AD=E8=A8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- testing-modules/assertion-libraries/README.md | 2 -- .../exceptions/Java8StyleAssertions.java | 36 ++++++++++--------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/testing-modules/assertion-libraries/README.md b/testing-modules/assertion-libraries/README.md index 5483bcf194..d055e3a074 100644 --- a/testing-modules/assertion-libraries/README.md +++ b/testing-modules/assertion-libraries/README.md @@ -7,8 +7,6 @@ 如果计算结果为 false ,则断言失败,抛出 AssertionError。 -assertion(断言) 在软件开发中是一种常用的调试方式,很多开发语言中都支持这种机制。 - ### 相关文章 - [AssertJ’s Java 8 Features](http://www.baeldung.com/assertJ-java-8-features) diff --git a/testing-modules/assertion-libraries/src/test/java/com/ossez/assertj/exceptions/Java8StyleAssertions.java b/testing-modules/assertion-libraries/src/test/java/com/ossez/assertj/exceptions/Java8StyleAssertions.java index 266e4e5c73..c1a9cd08dc 100644 --- a/testing-modules/assertion-libraries/src/test/java/com/ossez/assertj/exceptions/Java8StyleAssertions.java +++ b/testing-modules/assertion-libraries/src/test/java/com/ossez/assertj/exceptions/Java8StyleAssertions.java @@ -8,8 +8,15 @@ import static org.assertj.core.api.Assertions.catchThrowable; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; + import org.junit.Test; +/** + * AssertJ Exception example + *

https://www.ossez.com/t/assertj-exception/13988

+ * + * @author YuCheng Hu + */ public class Java8StyleAssertions { @Test @@ -18,13 +25,11 @@ public class Java8StyleAssertions { ArrayList myStringList = new ArrayList(Arrays.asList("Strine one", "String two")); myStringList.get(2); }).isInstanceOf(IndexOutOfBoundsException.class) - .hasMessageStartingWith("Index: 2") - .hasMessageContaining("2") - .hasMessageEndingWith("Size: 2") - .hasMessageContaining("Index: 2, Size: 2") - .hasMessage("Index: %s, Size: %s", 2, 2) - .hasMessageMatching("Index: \\d+, Size: \\d+") - .hasNoCause(); + .hasMessageStartingWith("Index 2") + .hasMessageContaining("2") + .hasMessageEndingWith("length 2") + .hasMessageContaining("Index 2") + .hasNoCause(); } @Test @@ -36,18 +41,18 @@ public class Java8StyleAssertions { throw new RuntimeException(e); } }).isInstanceOf(RuntimeException.class) - .hasCauseInstanceOf(IOException.class) - .hasStackTraceContaining("IOException"); + .hasCauseInstanceOf(IOException.class) + .hasStackTraceContaining("IOException"); } @Test public void whenDividingByZero_thenArithmeticException() { assertThatExceptionOfType(ArithmeticException.class).isThrownBy(() -> { - int numerator = 10; - int denominator = 0; - int quotient = numerator / denominator; - }) - .withMessageContaining("/ by zero"); + int numerator = 10; + int denominator = 0; + int quotient = numerator / denominator; + }) + .withMessageContaining("/ by zero"); // Alternatively: @@ -60,7 +65,6 @@ public class Java8StyleAssertions { // then assertThat(thrown).isInstanceOf(ArithmeticException.class) - .hasMessageContaining("/ by zero"); - + .hasMessageContaining("/ by zero"); } }