BAEL-3822 Spring Boot MVC controller return HTML (#8988)
Co-authored-by: Oskar <>
This commit is contained in:
parent
1734250d97
commit
8bfb7df3b2
|
@ -0,0 +1,12 @@
|
|||
package com.baeldung.html;
|
||||
|
||||
import org.springframework.boot.*;
|
||||
import org.springframework.boot.autoconfigure.*;
|
||||
|
||||
@SpringBootApplication
|
||||
public class HtmlApplication
|
||||
{
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(HtmlApplication.class, args);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.baeldung.html;
|
||||
|
||||
import org.springframework.http.*;
|
||||
import org.springframework.stereotype.*;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@Controller
|
||||
public class HtmlController
|
||||
{
|
||||
@GetMapping(value = "/welcome", produces = MediaType.TEXT_HTML_VALUE)
|
||||
@ResponseBody
|
||||
public String welcomeAsHTML()
|
||||
{
|
||||
return "<html>\n" + "<header><title>Welcome</title></header>\n" +
|
||||
"<body>\n" + "Hello world\n" + "</body>\n" + "</html>";
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue