Merge pull request '有关异常的断言' (#35) from assertion into main
Reviewed-on: https://src.ossez.com/cwiki-us-docs/java-tutorials/pulls/35
This commit is contained in:
commit
b930d3b74f
@ -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);
|
||||||
|
|
||||||
|
// AssertJ assertThatThrownBy
|
||||||
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");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -7,8 +7,6 @@
|
|||||||
|
|
||||||
如果计算结果为 false ,则断言失败,抛出 AssertionError。
|
如果计算结果为 false ,则断言失败,抛出 AssertionError。
|
||||||
|
|
||||||
assertion(断言) 在软件开发中是一种常用的调试方式,很多开发语言中都支持这种机制。
|
|
||||||
|
|
||||||
### 相关文章
|
### 相关文章
|
||||||
|
|
||||||
- [AssertJ’s Java 8 Features](http://www.baeldung.com/assertJ-java-8-features)
|
- [AssertJ’s Java 8 Features](http://www.baeldung.com/assertJ-java-8-features)
|
||||||
@ -19,4 +17,4 @@ assertion(断言) 在软件开发中是一种常用的调试方式,很多开
|
|||||||
- [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)
|
||||||
|
@ -8,8 +8,15 @@ import static org.assertj.core.api.Assertions.catchThrowable;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AssertJ Exception example
|
||||||
|
* <p><a href="https://www.ossez.com/t/assertj-exception/13988">https://www.ossez.com/t/assertj-exception/13988</a></p>
|
||||||
|
*
|
||||||
|
* @author YuCheng Hu
|
||||||
|
*/
|
||||||
public class Java8StyleAssertions {
|
public class Java8StyleAssertions {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -18,12 +25,10 @@ public class Java8StyleAssertions {
|
|||||||
ArrayList<String> myStringList = new ArrayList<String>(Arrays.asList("Strine one", "String two"));
|
ArrayList<String> myStringList = new ArrayList<String>(Arrays.asList("Strine one", "String two"));
|
||||||
myStringList.get(2);
|
myStringList.get(2);
|
||||||
}).isInstanceOf(IndexOutOfBoundsException.class)
|
}).isInstanceOf(IndexOutOfBoundsException.class)
|
||||||
.hasMessageStartingWith("Index: 2")
|
.hasMessageStartingWith("Index 2")
|
||||||
.hasMessageContaining("2")
|
.hasMessageContaining("2")
|
||||||
.hasMessageEndingWith("Size: 2")
|
.hasMessageEndingWith("length 2")
|
||||||
.hasMessageContaining("Index: 2, Size: 2")
|
.hasMessageContaining("Index 2")
|
||||||
.hasMessage("Index: %s, Size: %s", 2, 2)
|
|
||||||
.hasMessageMatching("Index: \\d+, Size: \\d+")
|
|
||||||
.hasNoCause();
|
.hasNoCause();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -61,6 +66,5 @@ public class Java8StyleAssertions {
|
|||||||
// then
|
// then
|
||||||
assertThat(thrown).isInstanceOf(ArithmeticException.class)
|
assertThat(thrown).isInstanceOf(ArithmeticException.class)
|
||||||
.hasMessageContaining("/ by zero");
|
.hasMessageContaining("/ by zero");
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user