Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
77 lines (65 sloc) 2.17 KB
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()
.queryParam("firstName", "Norbert")
.queryParam("lastName", "Siegmund")
.when()
.post("/api/user")
.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"));
}
}