Skip to content

Commit b87d744

Browse files
committed
2 parents fa57077 + 93e0cda commit b87d744

File tree

4 files changed

+192
-0
lines changed

4 files changed

+192
-0
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
</organization>
2424

2525
<dependencies>
26+
<dependency>
27+
<groupId>org.json</groupId>
28+
<artifactId>json</artifactId>
29+
<version>20160212</version>
30+
</dependency>
31+
2632
<dependency>
2733
<groupId>org.springframework.boot</groupId>
2834
<artifactId>spring-boot-starter-data-jpa</artifactId>
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package sample;
2+
3+
4+
import static org.hamcrest.CoreMatchers.*;
5+
import static org.junit.Assert.*;
6+
7+
import java.util.Arrays;
8+
import org.junit.Test;
9+
10+
/**
11+
* Created by spooky on 2016/8/3.
12+
*/
13+
public class TestAssertions {
14+
@Test
15+
public void testAssertArrayEquals() {
16+
byte[] expected = "trial".getBytes();
17+
byte[] actual = "trial".getBytes();
18+
assertArrayEquals("failure - byte arrays not same", expected, actual);
19+
}
20+
21+
@Test
22+
public void testAssertEquals() {
23+
assertEquals("failure - strings are not equal", "text", "text");
24+
}
25+
26+
@Test
27+
public void testAssertFalse() {
28+
assertFalse("failure - should be false", false);
29+
}
30+
31+
@Test
32+
public void testAssertNotNull() {
33+
assertNotNull("should not be null", new Object());
34+
}
35+
36+
@Test
37+
public void testAssertNotSame() {
38+
assertNotSame("should not be same Object", new Object(), new Object());
39+
}
40+
41+
@Test
42+
public void testAssertNull() {
43+
assertNull("should be null", null);
44+
}
45+
46+
@Test
47+
public void testAssertSame() {
48+
Integer aNumber = Integer.valueOf(768);
49+
assertSame("should be same", aNumber, aNumber);
50+
}
51+
52+
// JUnit Matchers assertThat
53+
@Test
54+
public void testAssertThatBothContainsString() {
55+
assertThat("albumen", both(containsString("a")).and(containsString("b")));
56+
}
57+
58+
@Test
59+
public void testAssertThathasItemsContainsString() {
60+
assertThat(Arrays.asList("one", "two", "three"), hasItems("one", "three"));
61+
}
62+
63+
@Test
64+
public void testAssertThatEveryItemContainsString() {
65+
assertThat(
66+
Arrays.asList(new String[]{"fun", "ban", "net"}),
67+
everyItem(containsString("n"))
68+
);
69+
}
70+
71+
// Core Hamcrest Matchers with assertThat
72+
@Test
73+
public void testAssertThatHamcrestCoreMatchers() {
74+
assertThat("good", allOf(equalTo("good"), startsWith("good")));
75+
76+
assertThat("good", not(allOf(equalTo("bad"), equalTo("good"))));
77+
78+
assertThat("good", anyOf(equalTo("bad"), equalTo("good")));
79+
80+
assertThat(7, not(either(equalTo(3)).or(equalTo(4))));
81+
82+
assertThat(new Object(), not(sameInstance(new Object())));
83+
}
84+
85+
@Test
86+
public void testAssertTrue() {
87+
assertTrue("failure - should be true", true);
88+
}
89+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package test;
2+
3+
import java.io.Closeable;
4+
import java.io.IOException;
5+
6+
import org.junit.After;
7+
import org.junit.AfterClass;
8+
import org.junit.Before;
9+
import org.junit.BeforeClass;
10+
import org.junit.Test;
11+
12+
public class TestFixtures {
13+
14+
// 進行函式的 Mock
15+
static class ExpensiveManagedResource implements Closeable {
16+
@Override
17+
public void close() throws IOException {}
18+
}
19+
20+
static class ManagedResource implements Closeable {
21+
@Override
22+
public void close() throws IOException {}
23+
}
24+
25+
private ManagedResource myManagedResource;
26+
private static ExpensiveManagedResource myExpensiveManagedResource;
27+
28+
29+
@BeforeClass
30+
public static void setUpClass() {
31+
System.out.println("@BeforeClass setUpClass");
32+
myExpensiveManagedResource = new ExpensiveManagedResource();
33+
}
34+
35+
@Before
36+
public void setUp() {
37+
System.out.println("@Before setUp");
38+
this.myManagedResource = new ManagedResource();
39+
}
40+
41+
42+
@Test
43+
public void test1() {
44+
System.out.println("@Test test1()");
45+
}
46+
47+
@Test
48+
public void test2() {
49+
System.out.println("@Test test2()");
50+
}
51+
52+
@After
53+
public void tearDown() throws IOException {
54+
System.out.println("@After tearDown");
55+
56+
this.myManagedResource.close();
57+
this.myManagedResource = null;
58+
}
59+
60+
@AfterClass
61+
public static void tearDownClass() throws IOException {
62+
System.out.println("@AfterClass tearDownClass");
63+
myExpensiveManagedResource.close();
64+
myExpensiveManagedResource = null;
65+
}
66+
67+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package sample.data.rest;
2+
3+
4+
import org.json.JSONObject;
5+
6+
import org.junit.Ignore;
7+
import org.junit.Test;
8+
import org.springframework.web.client.RestTemplate;
9+
10+
import static org.assertj.core.api.Assertions.assertThat;
11+
12+
13+
public class ThirdPartyApiTest {
14+
15+
final RestTemplate template = new RestTemplate();
16+
17+
@Test
18+
@Ignore
19+
public void testCitiesSearch() throws Exception {
20+
String api = "http://localhost:8000/api/cities/search/findByNameAndCountryAllIgnoringCase";
21+
String params = "?name=Melbourne&country=Australia";
22+
String url = api + params;
23+
24+
String message = template.getForObject(url, String.class);
25+
JSONObject jsonObj = new JSONObject(message);
26+
assertThat(jsonObj.has("country")).isTrue();
27+
}
28+
29+
30+
}

0 commit comments

Comments
 (0)