assertThat 使用方法
时间: 2023-10-23 20:14:36 浏览: 155
assertThat 是一个用于测试的断言方法,它通常与JUnit或其他测试框架一起使用。它的基本语法如下:
```
assertThat([actual], [matcher statement]);
```
其中,`actual` 表示被测试的实际值,`matcher statement` 表示匹配语句,用来判断 `actual` 是否符合预期。常用的匹配语句包括以下几种:
- `is()`:判断两个对象是否相等。
- `not()`:取反操作。
- `equalTo()`:判断两个对象是否相等。
- `instanceOf()`:判断一个对象是否是某个类的实例。
- `hasProperty()`:判断一个对象是否有某个属性。
- `hasItem()`:判断一个集合是否包含某个元素。
- `contains()`:判断两个集合是否包含相同的元素。
例如,判断一个字符串是否等于预期值可以使用以下代码:
```
String actual = "Hello, world!";
assertThat(actual, is(equalTo("Hello, world!")));
```
如果 `actual` 的值与预期值相同,则测试通过;否则,测试将会失败并输出相应的错误信息。
相关问题
assertthat
assertThat是JUnit测试框架中的一个断言方法,用于验证实际值与预期值是否相等或满足某些条件。它可以用于检查数组、集合、字符串等数据类型的断言。
在引用中,assertThat使用arrayContaining断言方法来验证数组中的元素是否按照给定顺序存在。在这个例子中,断言的是新创建的字符串数组["my","you"]中的元素与预期的数组["my","you"]中的元素一致。
在引用中,assertThat使用arrayWithSize断言方法来验证数组的长度是否与预期长度一致。在这个例子中,断言的是新创建的字符串数组["my","you"]的长度是否等于预期长度2。
在引用中,assertThat使用hasItem断言方法来验证集合中是否包含特定的元素。在这个例子中,断言的是新创建的字符串集合["my","mine"]中是否包含元素"my"。
assertj assertThat
assertj是一个Java库,它提供了一组易于使用的断言方法,可以帮助您编写更具可读性和可维护性的测试代码。assertThat是assertj库中最常用的方法之一,它用于对实际值进行断言。以下是assertThat的一些示例:
1. 对字符串进行断言
```java
String str = "hello world";
assertThat(str).isEqualTo("hello world"); // 断言str等于"hello world"
assertThat(str).startsWith("hello"); // 断言str以"hello"开头
assertThat(str).endsWith("world"); // 断言str以"world"结尾
assertThat(str).contains("o w"); // 断言str包含"o w"
```
2. 对数字进行断言
```java
int num = 10;
assertThat(num).isEqualTo(10); // 断言num等于10
assertThat(num).isGreaterThan(5); // 断言num大于5
assertThat(num).isLessThan(20); // 断言num小于20
```
3. 对集合进行断言
```java
List<String> list = Arrays.asList("apple", "banana", "orange");
assertThat(list).hasSize(3); // 断言list的大小为3
assertThat(list).contains("apple", "banana"); // 断言list包含"apple"和"banana"
assertThat(list).doesNotContain("pear"); // 断言list不包含"pear"
```
4. 对对象进行断言
```java
Person person = new Person("John", 30);
assertThat(person).hasFieldOrPropertyWithValue("name", "John"); // 断言person的name属性值为"John"
assertThat(person).hasFieldOrPropertyWithValue("age", 30); // 断言person的age属性值为30
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)