@[toc]
Java native Assertion
Assert, as a keyword in Java, already acts as an assertion, as shown in the following example
assert true;
assert false;
Copy the code
Testng assertion
Testng comes with the Assert class, which has a variety of static Assert methods to use. Here are some common ones
// assert true or false
Assert.assertTrue(true);
// Predicate match, the first parameter is the expected value, the last parameter is the actual value
Assert.assertEquals(1.1);
Copy the code
However, testng does not have a functional interface to pass arguments, so it is not possible to use lambda expressions to verify multiple values. Therefore, if multiple values are checked in a test case, we can consider setting a flag variable to do so, or use junit assertion mechanism that can verify multiple values
Junit5 assertion
I currently use junit5 Assertions, which are very flexible and use Assertions in the org.junit.jupiter. API package, some of the Assertions commonly used are also listed here
// Check whether it is true or false
Assertions.assertTrue(true);
// Predicate matches
Assertions.assertEquals(1.1);
// Use lambda expressions to make multiple assertions. If one of them is wrong, place the assertion in false
Assertions.assertAll(
() -> {Assertions.assertTrue(true); }, () -> {Assertions.assertEquals(1.1); }, () -> {Assertions.assertEquals("a"."a");}
);
Copy the code
It is worth noting that after junit 4.4, Hamcrest’s assertThat assertion was introduced, making it easy to humanize assertions
Hamcrest assertion
Hamcrest is a framework that allows you to define matching rules directly when writing matcher objects. There are a number of matchers that are intrusive, such as UI validation or data filtering, but match objects are most commonly used in writing flexible tests
It is also worth noting that the Hamcrest dependencies have been integrated with the SpringBoot Web development framework and the RestAssured Interface testing framework
The Hamcrest parameter is preceded by the actual value, followed by the expected value, and followed by the Matcher comparator
// Make the same judgment
MatcherAssert.assertThat("b", Matchers.is("a"));
// Equalto can compare objects
MatcherAssert.assertThat("a", Matchers.equalTo("a"));
Copy the code
There are also various comparators, such as hasItem, hasItems, which tests that a collection contains an element, and so on
when().
get("/store").
then().
body("store.book.findAll { it.price < 10 }.title", hasItems("Sayings of the Century"."Moby Dick"));
Copy the code
RESTassured assertion
RESTassured is an interface testing framework that can be introduced through MVN dependencies
Two types of assertions are commonly used when writing RESTassured interface tests
Method one: Simply use the RESTassured Body method in conjunction with the comparator in Hamcrest to assert without writing the assertion into the test
when().
get("/store").
then().
body("store.book.findAll { it.price < 10 }.title", hasItems("Sayings of the Century"."Moby Dick"));
Copy the code
Method 2: Extract ().response() to extract the Response class, and then operate the response in the test class combined with some assertion methods
// Response is returned
when().
get("/store").
then().log().
body().
extract().response();
// Assert in the test class, extract the USER from JSON for comparison assertion
Assert.assertEquals("xxx", response.path("user"));
Copy the code
Last but not least, REST Assured offers a very flexible and rich assertion mechanism, which I will not go into details here, but you need to check out the official website to explore!