spring cloud 常用的断言,以及分别代码示例
时间: 2023-08-02 12:41:34 浏览: 69
Spring Cloud 中常用的断言有以下几种:
1. `assertThat`:判断实际值是否与期望值相等
示例代码:
```java
String str = "hello";
assertThat(str).isEqualTo("hello");
```
2. `assertNotNull`:判断对象是否不为空
示例代码:
```java
Object obj = new Object();
assertNotNull(obj);
```
3. `assertThatThrownBy`:判断方法是否抛出了指定的异常
示例代码:
```java
List<String> list = new ArrayList<>();
assertThatThrownBy(() -> list.get(0)).isInstanceOf(IndexOutOfBoundsException.class);
```
4. `assertThatCode`:判断方法是否正常执行,而不抛出异常
示例代码:
```java
List<String> list = new ArrayList<>();
assertThatCode(() -> list.add("hello")).doesNotThrowAnyException();
```
5. `assertThatExceptionOfType`:判断方法是否抛出了指定类型的异常
示例代码:
```java
List<String> list = new ArrayList<>();
assertThatExceptionOfType(IndexOutOfBoundsException.class).isThrownBy(() -> list.get(0));
```
阅读全文