Java 8 AssertJ 的异常(Exception )断言 https://www.ossez.com/t/assertj-exception/13988

This commit is contained in:
YuCheng Hu 2022-06-06 14:30:33 -04:00
parent 550291e2e0
commit 4f1cdf5208
2 changed files with 16 additions and 10 deletions

View File

@ -2,13 +2,14 @@ package com.ossez.number.foramt;
import org.apache.commons.lang3.math.NumberUtils; import org.apache.commons.lang3.math.NumberUtils;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance; import org.junit.jupiter.api.TestInstance;
import java.util.Arrays; import java.util.Arrays;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.*;
import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.jupiter.api.Assertions.assertThrows;
/** /**
@ -26,20 +27,25 @@ public class NumberFormatExceptionTest {
@Test @Test
public void ConstructorNumberFormatException() { public void ConstructorNumberFormatException() {
// Exception exception = assertThrows(NumberFormatException.class, () -> { // JUNIT 5 Assert
// new Integer("one"); Exception exception = assertThrows(NumberFormatException.class, () -> {
// }); new Integer("one");
});
System.out.println(exception);
assertThatThrownBy(() -> { // AssertJ assertThatThrownBy
assertThatThrownBy(() -> {
new Integer("one"); new Integer("one");
}).isInstanceOf(NumberFormatException.class).hasMessageStartingWith("For input string"); }).isInstanceOf(NumberFormatException.class).hasMessageStartingWith("For input string");
// System.out.println(exception); // AssertJ assertThatExceptionOfType
assertThatExceptionOfType(NumberFormatException.class).isThrownBy(() -> {
new Integer("one");
});
Integer aIntegerObj = new Integer("1");
// Integer aIntegerObj = new Integer("one"); // Integer aIntegerObj = new Integer("one");
Double doubleDecimalObj = new Double("two.2"); // Double doubleDecimalObj = new Double("two.2");
} }

View File

@ -17,4 +17,4 @@
- [Guide to JSpec](http://www.baeldung.com/jspec) - [Guide to JSpec](http://www.baeldung.com/jspec)
- [Custom Assertions with AssertJ](http://www.baeldung.com/assertj-custom-assertion) - [Custom Assertions with AssertJ](http://www.baeldung.com/assertj-custom-assertion)
- [Using Conditions with AssertJ Assertions](http://www.baeldung.com/assertj-conditions) - [Using Conditions with AssertJ Assertions](http://www.baeldung.com/assertj-conditions)
- [AssertJ Exception Assertions](http://www.baeldung.com/assertj-exception-assertion) - [AssertJ 的异常Exception )断言](https://www.ossez.com/t/assertj-exception/13988)