JAVA-3541: Move spring-5-mvc into spring-web-modules

This commit is contained in:
Krzysztof Woyke
2021-01-06 11:16:55 +01:00
parent bd75fcb4a4
commit 108522e190
28 changed files with 67 additions and 69 deletions
@@ -0,0 +1,56 @@
package com.baeldung;
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.springframework.http.MediaType;
import com.jayway.restassured.RestAssured;
import com.jayway.restassured.response.Response;
import com.jayway.restassured.specification.RequestSpecification;
public class LiveTest {
private static String APP_ROOT = "http://localhost:8081";
@Test
public void givenUser_whenResourceCreatedWithNullName_then400BadRequest() {
final Response response = givenAuth("user", "pass").contentType(MediaType.APPLICATION_JSON.toString())
.body(resourceWithNullName())
.post(APP_ROOT + "/foos");
assertEquals(400, response.getStatusCode());
}
@Test
public void givenUser_whenResourceCreated_then201Created() {
final Response response = givenAuth("user", "pass").contentType(MediaType.APPLICATION_JSON.toString())
.body(resourceString())
.post(APP_ROOT + "/foos");
assertEquals(201, response.getStatusCode());
}
/*@Test
public void givenUser_whenGetAllFoos_thenOK() {
final Response response = givenAuth("user", "pass").get(APP_ROOT + "/foos");
assertEquals(200, response.getStatusCode());
}*/
//
private final String resourceWithNullName() {
return "{\"name\":null}";
}
private final String resourceString() {
return "{\"name\":\"" + randomAlphabetic(8) + "\"}";
}
private final RequestSpecification givenAuth(String username, String password) {
return RestAssured.given()
.auth()
.preemptive()
.basic(username, password);
}
}
@@ -0,0 +1,16 @@
package com.baeldung;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Spring5Application.class)
public class Spring5ApplicationIntegrationTest {
@Test
public void contextLoads() {
}
}
@@ -0,0 +1,30 @@
package com.baeldung.html;
import org.junit.jupiter.api.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.boot.test.autoconfigure.web.servlet.*;
import org.springframework.test.web.servlet.*;
import org.springframework.test.web.servlet.request.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@WebMvcTest(HtmlController.class)
class HtmlControllerUnitTest {
@Autowired
private MockMvc mockMvc;
private final String expectedHtmlResponse =
"<html>\n" + "<header><title>Welcome</title></header>\n" +
"<body>\n" + "Hello world\n" + "</body>\n" + "</html>";
@Test
void whenGETRequestToCorrectURL_thenReturnCorrectWelcomeMessage() throws Exception {
MvcResult result = mockMvc.perform(MockMvcRequestBuilders.get("/welcome"))
.andExpect(status().isOk())
.andReturn();
String resultDOW = result.getResponse().getContentAsString();
assertEquals(expectedHtmlResponse, resultDOW);
}
}