forked from jonashackt/spring-boot-vuejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BackendControllerTest.java
110 lines (94 loc) · 2.87 KB
/
BackendControllerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package de.jonashackt.springbootvuejs.controller;
import de.jonashackt.springbootvuejs.SpringBootVuejsApplication;
import de.jonashackt.springbootvuejs.domain.User;
import io.restassured.RestAssured;
import org.apache.http.HttpStatus;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.test.context.junit4.SpringRunner;
import static io.restassured.RestAssured.given;
import static io.restassured.RestAssured.when;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
@RunWith(SpringRunner.class)
@SpringBootTest(
classes = SpringBootVuejsApplication.class,
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT
)
public class BackendControllerTest {
@LocalServerPort
private int port;
@Before
public void init() {
RestAssured.baseURI = "http://localhost";
RestAssured.port = port;
}
@Test
public void saysHello() {
when()
.get("/api/hello")
.then()
.statusCode(HttpStatus.SC_OK)
.assertThat()
.body(is(equalTo(BackendController.HELLO_TEXT)));
}
@Test
public void addNewUserAndRetrieveItBack() {
User norbertSiegmund = new User("Norbert", "Siegmund");
Long userId =
given()
.pathParam("firstName", "Norbert")
.pathParam("lastName", "Siegmund")
.when()
.put("/api/user/{lastName}/{firstName}")
.then()
.statusCode(is(HttpStatus.SC_CREATED))
.extract()
.body().as(Long.class);
User responseUser =
given()
.pathParam("id", userId)
.when()
.get("/api/user/{id}")
.then()
.statusCode(HttpStatus.SC_OK)
.assertThat()
.extract().as(User.class);
// Did Norbert came back?
assertThat(responseUser.getFirstName(), is("Norbert"));
assertThat(responseUser.getLastName(), is("Siegmund"));
}
@Test
public void user_api_should_give_http_404_not_found_when_user_not_present_in_db() {
Long someId = 200L;
given()
.pathParam("id", someId)
.when()
.get("/api/user/{id}")
.then()
.statusCode(HttpStatus.SC_NOT_FOUND);
}
@Test
public void secured_api_should_react_with_unauthorized_per_default() {
given()
.when()
.get("/api/secured")
.then()
.statusCode(HttpStatus.SC_UNAUTHORIZED);
}
@Test
public void secured_api_should_give_http_200_when_authorized() {
given()
.auth().basic("sina", "miller")
.when()
.get("/api/secured")
.then()
.statusCode(HttpStatus.SC_OK)
.assertThat()
.body(is(equalTo(BackendController.SECURED_TEXT)));
}
}