|
2 | 2 |
|
3 | 3 | import java.net.*; |
4 | 4 | import java.io.*; |
5 | | -import org.junit.Assert; |
| 5 | + |
6 | 6 | import org.junit.Test; |
7 | | -import org.hamcrest.Matcher; |
8 | | -import org.hamcrest.Matchers; |
| 7 | + |
| 8 | +import static org.hamcrest.CoreMatchers.containsString; |
| 9 | +import static org.hamcrest.MatcherAssert.assertThat; |
9 | 10 |
|
10 | 11 | /** |
11 | 12 | * Unit test for simple App. |
12 | 13 | */ |
13 | | -public class Rails7AppTestIT |
14 | | -{ |
15 | | - private static String appName = "rails7_test-1.0"; |
16 | | - |
| 14 | +public class Rails7AppTestIT { |
17 | 15 | /** |
18 | 16 | * Hit the web app and test the response |
19 | 17 | */ |
20 | 18 | @Test |
21 | | - public void testApp() throws Exception |
22 | | - { |
23 | | - URL route = new URL("http://localhost:8080/" + appName + "/posts"); |
24 | | - URLConnection routeConn = route.openConnection(); |
25 | | - BufferedReader in = new BufferedReader( new InputStreamReader( routeConn.getInputStream())); |
26 | | - |
27 | | - String inputLine; |
28 | | - String content = ""; |
29 | | - |
30 | | - while ((inputLine = in.readLine()) != null) |
31 | | - content += inputLine; |
32 | | - in.close(); |
| 19 | + public void testApp() throws Exception { |
| 20 | + String content = contentFrom("http://localhost:8080/posts/"); |
| 21 | + assertThat(content, containsString("Rails7App")); |
| 22 | + assertThat(content, containsString("Listing posts")); |
| 23 | + } |
33 | 24 |
|
34 | | - Assert.assertThat(content, Matchers.containsString("Rails7App")); |
35 | | - Assert.assertThat(content, Matchers.containsString("Listing posts")); |
| 25 | + private static String contentFrom(String url) throws IOException { |
| 26 | + URL route = new URL(url); |
| 27 | + StringBuilder content = new StringBuilder(); |
| 28 | + try (BufferedReader in = new BufferedReader(new InputStreamReader(route.openStream()))) { |
| 29 | + while (in.ready()) { |
| 30 | + content.append(in.readLine()); |
| 31 | + } |
| 32 | + } |
| 33 | + return content.toString(); |
36 | 34 | } |
37 | 35 | } |
0 commit comments