View Javadoc
1   /*
2    * Copyright 2023 Adrian Herscu
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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; // false; // no description generated for this failure
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 // lombok generates a constructor
75          @ToString // lombok generates a nice toString method
76          @EqualsAndHashCode // comment to make it fail the test
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, // we need to "adapt" Foo into a
101                                            // String
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             // we need to adapt JSON structure into contents value
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             // @formatter:off
131             // there are toooooooo much possibilities...
132             { PI,                0 },
133             { 2 * PI,            0 },
134             // { pow(2, 1000) * PI, 0 }, // this will fail due to much precision loss
135             // @formatter:on
136         };
137     }
138 }