Skip to content

Commit 43e7972

Browse files
committed
Tutorial 8: Integratietests maken voor de API met JUnit
Nu we de REST routes in de applicatie hebben geschreven, gaan we hiervoor een aantal integratie tests schrijven. Met behulp van deze tests kunnen we eenvoudig controleren of alle functionaliteit die we gemaakt hebben nog steeds werkt na het uitbreiden / aanpassen van onze code.
1 parent 0488b78 commit 43e7972

File tree

8 files changed

+168
-4
lines changed

8 files changed

+168
-4
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/src/main/java/keys
2+
13
## https://github.com/github/gitignore/blob/master/Global/JetBrains.gitignore
24
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
35
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

.idea/modules/tutorial_test.iml

Lines changed: 21 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ dependencies {
4141
compile("com.squareup.retrofit2:retrofit:2.0.1")
4242
compile("com.squareup.retrofit2:converter-gson:2.0.1")
4343

44+
testCompile("org.springframework.boot:spring-boot-starter-test")
45+
testCompile("junit:junit")
46+
testCompile("com.jayway.restassured:rest-assured:2.9.0")
47+
4448
}
4549

4650
task wrapper(type: Wrapper) {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Fri Aug 12 11:40:32 CEST 2016
1+
#Sat Aug 13 11:30:28 CEST 2016
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-2.3-bin.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-2.3-all.zip

src/main/resources/application-dev.properties

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ debug=true
22

33
flyway.locations=classpath:db/migration,classpath:db/fixtures
44

5-
spring.datasource.name=testdb
5+
spring.datasource.name=testdb
6+
7+
application.youtube.api_key={key}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package tutorial;
2+
3+
import com.jayway.restassured.RestAssured;
4+
import org.junit.Before;
5+
import org.junit.runner.RunWith;
6+
import org.springframework.beans.factory.annotation.Value;
7+
import org.springframework.boot.test.IntegrationTest;
8+
import org.springframework.boot.test.SpringApplicationConfiguration;
9+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
10+
import org.springframework.test.context.web.WebAppConfiguration;
11+
12+
@RunWith(SpringJUnit4ClassRunner.class)
13+
@SpringApplicationConfiguration(Application.class)
14+
@WebAppConfiguration
15+
@IntegrationTest("server.port:0")
16+
public abstract class ApplicationIntegrationTest {
17+
18+
@Value("${local.server.port}")
19+
int port;
20+
21+
@Before
22+
public void setUp() throws Exception {
23+
RestAssured.port = port;
24+
}
25+
26+
}
27+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package tutorial.api.controllers;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
import tutorial.ApplicationIntegrationTest;
6+
7+
import static com.jayway.restassured.RestAssured.get;
8+
9+
public class HomeControllerTest extends ApplicationIntegrationTest {
10+
11+
@Test
12+
public void index() throws Exception {
13+
String result = get("/api/hello").asString();
14+
15+
Assert.assertEquals("Hello Backend Server", result);
16+
}
17+
18+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package tutorial.api.controllers;
2+
3+
import com.jayway.restassured.response.Response;
4+
import org.junit.Assert;
5+
import org.junit.Test;
6+
import tutorial.ApplicationIntegrationTest;
7+
8+
import java.util.HashMap;
9+
10+
import static com.jayway.restassured.RestAssured.get;
11+
import static com.jayway.restassured.RestAssured.given;
12+
import static com.jayway.restassured.path.json.JsonPath.from;
13+
14+
public class ProductControllerTest extends ApplicationIntegrationTest {
15+
16+
private HashMap<String, Object> stubProduct1 = new HashMap<String,
17+
Object>() {{
18+
put("id", 1);
19+
put("name", "Macbook Air");
20+
put("price", 999.95F);
21+
}};
22+
23+
private HashMap<String, Object> stubProduct2 = new HashMap<String,
24+
Object>() {{
25+
put("id", 2);
26+
put("name", "Magic Mouse");
27+
put("price", 69.95F);
28+
}};
29+
30+
private HashMap<String, Object> stubProduct3 = new HashMap<String,
31+
Object>() {{
32+
put("id", 3);
33+
put("name", "Sunglasses");
34+
put("price", 14.50F);
35+
}};
36+
37+
@Test
38+
public void showAll() throws Exception {
39+
Response response = get("/api/products");
40+
Assert.assertEquals(200, response.getStatusCode());
41+
42+
String json = response.asString();
43+
44+
HashMap<String, Object> product1 = from(json).get("[0]");
45+
46+
for (String key : stubProduct1.keySet()) {
47+
Assert.assertTrue(product1.containsKey(key));
48+
Assert.assertEquals(product1.get(key), stubProduct1.get(key));
49+
}
50+
51+
HashMap<String, Object> product2 = from(json).get("[1]");
52+
53+
for (String key : stubProduct2.keySet()) {
54+
Assert.assertTrue(product2.containsKey(key));
55+
Assert.assertEquals(product2.get(key), stubProduct2.get(key));
56+
}
57+
}
58+
59+
@Test
60+
public void show() throws Exception {
61+
Response response = get("/api/products/{productId}", 1);
62+
Assert.assertEquals(200, response.getStatusCode());
63+
String json = response.asString();
64+
65+
HashMap<String, Object> product1 = from(json).get();
66+
67+
for (String key : stubProduct1.keySet()) {
68+
Assert.assertTrue(product1.containsKey(key));
69+
Assert.assertEquals(product1.get(key), stubProduct1.get(key));
70+
}
71+
}
72+
73+
@Test
74+
public void insert() throws Exception {
75+
Response insertResponse = given().parameters("name", stubProduct3.
76+
get("name"), "price", stubProduct3.get("price")).post("/api/products");
77+
Assert.assertEquals(200, insertResponse.getStatusCode());
78+
79+
Response getResponse = get("/api/products/{productId}", 3);
80+
Assert.assertEquals(200, getResponse.getStatusCode());
81+
String json = getResponse.asString();
82+
83+
HashMap<String, Object> product = from(json).get();
84+
85+
for (String key : stubProduct3.keySet()) {
86+
Assert.assertTrue(product.containsKey(key));
87+
Assert.assertEquals(product.get(key), stubProduct3.get(key));
88+
}
89+
}
90+
91+
}

0 commit comments

Comments
 (0)