1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package dev.aherscu.qa.testing.example.scenarios.tutorial2;
18
19 import static com.google.common.collect.Ordering.*;
20 import static com.jayway.jsonpath.JsonPath.*;
21 import static dev.aherscu.qa.testing.utils.MatchersExtensions.*;
22 import static dev.aherscu.qa.testing.utils.MatchersExtensions.both;
23 import static dev.aherscu.qa.testing.utils.MatchersExtensions.closeTo;
24 import static dev.aherscu.qa.testing.utils.MatchersExtensions.containsString;
25 import static dev.aherscu.qa.testing.utils.MatchersExtensions.hasItem;
26 import static dev.aherscu.qa.testing.utils.MatchersExtensions.is;
27 import static dev.aherscu.qa.testing.utils.StreamMatchersExtensions.*;
28 import static java.lang.Math.*;
29 import static java.util.Arrays.*;
30 import static org.hamcrest.MatcherAssert.*;
31 import static org.hamcrest.Matchers.equalTo;
32 import static org.hamcrest.Matchers.startsWith;
33
34 import java.util.stream.*;
35
36 import org.testng.annotations.*;
37
38 import edu.umd.cs.findbugs.annotations.*;
39 import lombok.*;
40
41 public class TestNgWithHamcrest {
42
43 @Test
44 public void _1_shouldSucceed() {
45 assert true;
46 }
47
48 @Test
49 public void _2_shouldAssertOnBoolean() {
50 assertThat(true, is(true));
51 }
52
53 @Test
54 public void _3_shouldAssertOnStringContents() {
55 assertThat("should verify string contents",
56 both(containsString("verify"))
57 .and(containsString("string")));
58 }
59
60 @Test
61 public void _4_shouldAssertOnListContents() {
62 assertThat(asList("should", "verify", "list", "contents"),
63 both(hasItem(startsWith("verify")))
64 .and(hasItem(equalTo("list"))));
65 }
66
67 @Test
68 public void _5_shouldAssertOnListOrder() {
69 assertThat(asList(1, 2, 3, 4, 5), is(ordered(natural())));
70 }
71
72 @Test
73 public void _6_shouldAssertOnEquality() {
74 @AllArgsConstructor
75 @ToString
76 @EqualsAndHashCode
77 class Foo {
78 final int id;
79 final String contents;
80 }
81
82 assertThat(new Foo(1, "hello"),
83 is(equalTo(new Foo(1, "hello"))));
84 }
85
86 @Test
87 public void _7_shouldAssertOnStreamContents() {
88 @AllArgsConstructor
89 @ToString
90 class Foo {
91 final int id;
92 final String contents;
93 }
94
95 assertThat(Stream.of(
96 new Foo(1, "should"),
97 new Foo(2, "verify"),
98 new Foo(3, "stream"),
99 new Foo(4, "contents")),
100 adaptedStream(f -> f.contents,
101
102 hasItemsMatching(equalTo("stream"))));
103 }
104
105 @Test
106 public void _8_shouldAssertOnStreamOfJsons() {
107 assertThat(Stream.of(
108 "{'id':1, 'contents':'should'}",
109 "{'id':2, 'contents':'verify'}",
110 "{'id':3, 'contents':'json'}",
111 "{'id':4, 'contents':'contents'}"),
112
113 adaptedStream(json -> parse(json).read("$.contents"),
114 hasItemsMatching(equalTo("json"))));
115 }
116
117 @Test(dataProvider = "sinusFunctionTable")
118 public void _9_shouldCalculateSinusWithTolerance(
119 final double angle,
120 final double sinus) {
121 assertThat(sin(angle), is(closeTo(sinus, 1e-10)));
122 }
123
124 @SuppressFBWarnings(
125 value = "UPM_UNCALLED_PRIVATE_METHOD",
126 justification = "called by testng framework")
127 @DataProvider
128 private Object[][] sinusFunctionTable() {
129 return new Object[][] {
130
131
132 { PI, 0 },
133 { 2 * PI, 0 },
134
135
136 };
137 }
138 }