1- package com .baeldung .test ;
2-
3- import static org .apache .commons .lang3 .RandomStringUtils .randomAlphabetic ;
4- import static org .junit .Assert .assertEquals ;
5-
6- import org .junit .Test ;
7- import org .springframework .http .MediaType ;
8-
9- import com .jayway .restassured .RestAssured ;
10- import com .jayway .restassured .response .Response ;
11- import com .jayway .restassured .specification .RequestSpecification ;
12-
13- public class LiveTest {
14-
15- private static String APP_ROOT = "http://localhost:8081" ;
16-
17-
18- @ Test
19- public void givenUser_whenResourceCreatedWithNullName_then400BadRequest () {
20- final Response response = givenAuth ("user" , "pass" ).contentType (MediaType .APPLICATION_JSON .toString ()).body (resourceWithNullName ()).post (APP_ROOT + "/foos" );
21- assertEquals (400 , response .getStatusCode ());
22- }
23-
24- @ Test
25- public void givenUser_whenResourceCreated_then201Created () {
26- final Response response = givenAuth ("user" , "pass" ).contentType (MediaType .APPLICATION_JSON .toString ()).body (resourceString ()).post (APP_ROOT + "/foos" );
27- assertEquals (201 , response .getStatusCode ());
28- }
29-
30- /*@Test
31- public void givenUser_whenGetAllFoos_thenOK() {
32- final Response response = givenAuth("user", "pass").get(APP_ROOT + "/foos");
33- assertEquals(200, response.getStatusCode());
34- }*/
35-
36-
37-
38- //
39-
40- private final String resourceWithNullName () {
41- final String roleData = "{\" name\" :null}" ;
42- return roleData ;
43- }
44-
45- private final String resourceString () {
46- final String roleData = "{\" name\" :\" " + randomAlphabetic (8 ) + "\" }" ;
47- return roleData ;
48- }
49-
50- private final RequestSpecification givenAuth (String username , String password ) {
51- return RestAssured .given ().auth ().preemptive ().basic (username , password );
52- }
53-
1+ package com .baeldung ;
2+
3+ import static org .apache .commons .lang3 .RandomStringUtils .randomAlphabetic ;
4+ import static org .junit .Assert .assertEquals ;
5+
6+ import org .junit .Test ;
7+ import org .springframework .http .MediaType ;
8+
9+ import com .jayway .restassured .RestAssured ;
10+ import com .jayway .restassured .response .Response ;
11+ import com .jayway .restassured .specification .RequestSpecification ;
12+
13+ public class LiveTest {
14+
15+ private static String APP_ROOT = "http://localhost:8081" ;
16+
17+
18+ @ Test
19+ public void givenUser_whenResourceCreatedWithNullName_then400BadRequest () {
20+ final Response response = givenAuth ("user" , "pass" ).contentType (MediaType .APPLICATION_JSON .toString ()).body (resourceWithNullName ()).post (APP_ROOT + "/foos" );
21+ assertEquals (400 , response .getStatusCode ());
22+ }
23+
24+ @ Test
25+ public void givenUser_whenResourceCreated_then201Created () {
26+ final Response response = givenAuth ("user" , "pass" ).contentType (MediaType .APPLICATION_JSON .toString ()).body (resourceString ()).post (APP_ROOT + "/foos" );
27+ assertEquals (201 , response .getStatusCode ());
28+ }
29+
30+ /*@Test
31+ public void givenUser_whenGetAllFoos_thenOK() {
32+ final Response response = givenAuth("user", "pass").get(APP_ROOT + "/foos");
33+ assertEquals(200, response.getStatusCode());
34+ }*/
35+
36+
37+
38+ //
39+
40+ private final String resourceWithNullName () {
41+ return "{\" name\" :null}" ;
42+ }
43+
44+ private final String resourceString () {
45+ return "{\" name\" :\" " + randomAlphabetic (8 ) + "\" }" ;
46+ }
47+
48+ private final RequestSpecification givenAuth (String username , String password ) {
49+ return RestAssured .given ().auth ().preemptive ().basic (username , password );
50+ }
51+
5452}
0 commit comments