Skip to content

Commit 46d8180

Browse files
dbudimkrmahadevan
authored andcommitted
Added assertListContains and assertListContainsObject methods to check if specific object present in List
1 parent ab94cc5 commit 46d8180

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed

CHANGES.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Current
2+
New: Added assertListContains and assertListContainsObject methods to check if specific object present in List (Dmytro Budym)
23
Fixed: GITHUB-2888: Skipped Tests with DataProvider appear as failed (Joaquin Moreira)
34
Fixed: GITHUB-2884: Discrepancies with DataProvider and Retry of failed tests (Krishnan Mahadevan)
45
Fixed: GITHUB-2879: Test listeners specified in parent testng.xml file are not included in testng-failed.xml file (Krishnan Mahadevan)

testng-asserts/src/main/java/org/testng/Assert.java

+55
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.util.Map;
1515
import java.util.Objects;
1616
import java.util.Set;
17+
import java.util.function.Predicate;
1718
import java.util.stream.Collectors;
1819
import java.util.stream.StreamSupport;
1920
import org.testng.collections.Lists;
@@ -2455,4 +2456,58 @@ public static <T extends Throwable> T expectThrows(
24552456
"Expected %s to be thrown, but nothing was thrown", throwableClass.getSimpleName());
24562457
throw new AssertionError(message != null ? message : nothingThrownMessage);
24572458
}
2459+
2460+
/**
2461+
* Asserts that List contains object. Uses java.util.List#contains method, be sure that equals and
2462+
* hashCode of tested object are overridden.
2463+
*
2464+
* @param list the list to search in
2465+
* @param object the object to search in list
2466+
* @param message the fail message
2467+
* @param <T> type of object
2468+
*/
2469+
public static <T> void assertListContainsObject(List<T> list, T object, String message) {
2470+
assertTrue(list.contains(object), "List NOT contains: [" + message + "]");
2471+
}
2472+
2473+
/**
2474+
* Asserts that List not contains object. Opposite to assertListContainsObject() Uses
2475+
* java.util.List#contains method, be sure that equals and hashCode of tested object are
2476+
* overridden.
2477+
*
2478+
* @param list the list to search in
2479+
* @param object to search in list
2480+
* @param message the fail message
2481+
* @param <T> type of object
2482+
*/
2483+
public static <T> void assertListNotContainsObject(List<T> list, T object, String message) {
2484+
assertFalse(list.contains(object), "List contains: [" + message + "]");
2485+
}
2486+
2487+
/**
2488+
* Asserts that List contains object by specific Predicate. Uses
2489+
* java.util.stream.Stream#anyMatch(java.util.function.Predicate) method.
2490+
*
2491+
* @param list the list to search in
2492+
* @param predicate the Predicate to match object
2493+
* @param message the fail message
2494+
* @param <T> type of object
2495+
*/
2496+
public static <T> void assertListContains(List<T> list, Predicate<T> predicate, String message) {
2497+
assertTrue(list.stream().anyMatch(predicate), "List NOT contains: [" + message + "]");
2498+
}
2499+
2500+
/**
2501+
* Asserts that List not contains object by specific Predicate. Opposite to assertListContains()
2502+
* method. Uses java.util.stream.Stream#noneMatch(java.util.function.Predicate) method.
2503+
*
2504+
* @param list the list to search in
2505+
* @param predicate the Predicate to match object
2506+
* @param message the fail message
2507+
* @param <T> type of object
2508+
*/
2509+
public static <T> void assertListNotContains(
2510+
List<T> list, Predicate<T> predicate, String message) {
2511+
assertTrue(list.stream().noneMatch(predicate), "List contains: [" + message + "]");
2512+
}
24582513
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package test.assertion;
2+
3+
import com.google.common.base.Objects;
4+
import java.util.List;
5+
import org.testng.Assert;
6+
import org.testng.annotations.Test;
7+
8+
public class AssertionListContainsTest {
9+
10+
private User userJack = new User("Jack", 22);
11+
private User userJohn = new User("John", 32);
12+
private List<User> users = List.of(userJack, userJohn);
13+
14+
@Test
15+
public void assertListContainsObject() {
16+
Assert.assertListContainsObject(users, userJack, "user Jack");
17+
}
18+
19+
@Test
20+
public void assertListNotContainsObject() {
21+
Assert.assertListNotContainsObject(users, new User("NoName", 34), "user NoName");
22+
}
23+
24+
@Test
25+
public void testAssertListContainsByPredicate() {
26+
Assert.assertListContains(users, user -> user.age.equals(22), "user with age 22");
27+
}
28+
29+
@Test
30+
public void testAssertListNotContainsByPredicate() {
31+
Assert.assertListNotContains(users, user -> user.age.equals(19), "user with age 19");
32+
}
33+
34+
private class User {
35+
36+
private String name;
37+
private Integer age;
38+
39+
public User(String name, Integer age) {
40+
this.name = name;
41+
this.age = age;
42+
}
43+
44+
@Override
45+
public boolean equals(Object o) {
46+
if (this == o) return true;
47+
if (o == null || getClass() != o.getClass()) return false;
48+
User user = (User) o;
49+
return Objects.equal(name, user.name) && Objects.equal(age, user.age);
50+
}
51+
52+
@Override
53+
public int hashCode() {
54+
return Objects.hashCode(name, age);
55+
}
56+
57+
@Override
58+
public String toString() {
59+
final StringBuilder sb = new StringBuilder("User{");
60+
sb.append("name='").append(name).append('\'');
61+
sb.append(", age=").append(age);
62+
sb.append('}');
63+
return sb.toString();
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)