我有实际和期望的对象,每个对象都包含一些数据成员。对于一个数据成员,我需要进行包含检查而不是等于检查,对于其余的,等于检查完成。有没有办法做到这一点?
答案 0 :(得分:0)
不含蓄,但您至少有以下两个选项:
<强>依赖关系:强>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.9.0</version>
<scope>test</scope>
</dependency>
<强>代码:强>
import org.testng.annotations.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.testng.Assert.assertTrue;
public class MyTest {
@Test
public void shouldCheckStringContains() {
String myString = "This is my string";
// TestNG
assertTrue(myString.startsWith("This")
&& myString.contains("is")
&& myString.contains("my")
&& myString.endsWith("string"));
// Hamcrest
assertThat(myString, allOf(startsWith("This"),
containsString("is"),
containsString("my"),
endsWith("string")));
//AssertJ
assertThat(myString).startsWith("This")
.contains("is")
.contains("my")
.endsWith("string");
}
}