From 1f6daeb4181ae74d02b970737be9ad50495adec3 Mon Sep 17 00:00:00 2001 From: mperor Date: Sun, 30 Mar 2025 19:02:29 +0200 Subject: [PATCH] Introduction to @SpringBootTest and testing toolbox --- spring-web-lab/README.md | 3 +- spring-web-lab/pom.xml | 4 -- .../spring/controller/HelloController.java | 16 +++++++ .../mperor/lab/spring/SpringMockMvcTest.java | 47 +++++++++++++++++++ .../spring/SpringWebLabApplicationTest.java | 20 +------- .../lab/spring/SpringWithTomcatTest.java | 34 ++++++++++++++ .../controller/HelloControllerTest.java | 22 +++++++++ .../lab/spring/jackson/JacksonTest.java | 3 +- 8 files changed, 124 insertions(+), 25 deletions(-) create mode 100644 spring-web-lab/src/main/java/pl/mperor/lab/spring/controller/HelloController.java create mode 100644 spring-web-lab/src/test/java/pl/mperor/lab/spring/SpringMockMvcTest.java create mode 100644 spring-web-lab/src/test/java/pl/mperor/lab/spring/SpringWithTomcatTest.java create mode 100644 spring-web-lab/src/test/java/pl/mperor/lab/spring/controller/HelloControllerTest.java diff --git a/spring-web-lab/README.md b/spring-web-lab/README.md index e57bce6..de52b55 100644 --- a/spring-web-lab/README.md +++ b/spring-web-lab/README.md @@ -3,4 +3,5 @@ ## Sources 🔗 * [Java Senior Quiz 🧩](https://youtube.com/playlist?list=PLO5muTI694A5yRrEDNY1czqEUVbYHG_AH&si=KteWFowVcOSTQ8F-) -* [Action Jackson! Effective JSON processing in Spring Boot Applications by Joris Kuipers @ Spring I/O](https://youtu.be/BhiF6e24l5k?si=IxS0X6FtgfWzdP-c) \ No newline at end of file +* [Action Jackson! Effective JSON processing in Spring Boot Applications by Joris Kuipers @ Spring I/O](https://youtu.be/BhiF6e24l5k?si=IxS0X6FtgfWzdP-c) +* [Spring Boot testing: Zero to Hero by Daniel Garnier-Moiroux](https://youtu.be/u5foQULTxHM?si=WPZKUZY-DMqcHGdG) \ No newline at end of file diff --git a/spring-web-lab/pom.xml b/spring-web-lab/pom.xml index c38cda5..3db1ca5 100644 --- a/spring-web-lab/pom.xml +++ b/spring-web-lab/pom.xml @@ -26,10 +26,6 @@ org.springframework.boot spring-boot-starter-actuator - - org.springframework.boot - spring-boot-starter-webflux - \ No newline at end of file diff --git a/spring-web-lab/src/main/java/pl/mperor/lab/spring/controller/HelloController.java b/spring-web-lab/src/main/java/pl/mperor/lab/spring/controller/HelloController.java new file mode 100644 index 0000000..596ddc4 --- /dev/null +++ b/spring-web-lab/src/main/java/pl/mperor/lab/spring/controller/HelloController.java @@ -0,0 +1,16 @@ +package pl.mperor.lab.spring.controller; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RequestMapping("/hello") +@RestController +class HelloController { + + @GetMapping + String hello() { + return "🗺️ Hello World!"; + } + +} diff --git a/spring-web-lab/src/test/java/pl/mperor/lab/spring/SpringMockMvcTest.java b/spring-web-lab/src/test/java/pl/mperor/lab/spring/SpringMockMvcTest.java new file mode 100644 index 0000000..f4fad7a --- /dev/null +++ b/spring-web-lab/src/test/java/pl/mperor/lab/spring/SpringMockMvcTest.java @@ -0,0 +1,47 @@ +package pl.mperor.lab.spring; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.web.client.MockMvcClientHttpRequestFactory; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.assertj.MockMvcTester; +import org.springframework.web.client.RestClient; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@SpringBootTest +@AutoConfigureMockMvc +public class SpringMockMvcTest { + + @Autowired + private MockMvc mvc; + @Autowired + private MockMvcTester tester; + + @Test + public void shouldReturnHealthStatusUp() throws Exception { + mvc.perform(get("/actuator/health")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.status").value("UP")); + + var restClientBody = RestClient.builder() + .requestFactory(new MockMvcClientHttpRequestFactory(mvc)).build() + .get() + .uri("/actuator/health") + .retrieve() + .body(String.class); + Assertions.assertThat(restClientBody).contains("{\"status\":\"UP\"}"); + + tester.get().uri("/actuator/health") + .exchange() + .assertThat() + .hasStatusOk() + .bodyJson().extractingPath("$.status").isEqualTo("UP"); + } + +} \ No newline at end of file diff --git a/spring-web-lab/src/test/java/pl/mperor/lab/spring/SpringWebLabApplicationTest.java b/spring-web-lab/src/test/java/pl/mperor/lab/spring/SpringWebLabApplicationTest.java index c1d4648..83850e0 100644 --- a/spring-web-lab/src/test/java/pl/mperor/lab/spring/SpringWebLabApplicationTest.java +++ b/spring-web-lab/src/test/java/pl/mperor/lab/spring/SpringWebLabApplicationTest.java @@ -1,30 +1,12 @@ package pl.mperor.lab.spring; import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.http.HttpStatus; -import org.springframework.test.web.reactive.server.WebTestClient; -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) -@AutoConfigureWebTestClient +@SpringBootTest public class SpringWebLabApplicationTest { - @Autowired - private WebTestClient client; - @Test public void contextLoads() { } - - @Test - public void shouldReturnHealthStatusUp() { - client.get().uri("/actuator/health") - .exchange() - .expectStatus().isEqualTo(HttpStatus.OK) - .expectBody() - .jsonPath("$.status").isEqualTo("UP"); - } - } \ No newline at end of file diff --git a/spring-web-lab/src/test/java/pl/mperor/lab/spring/SpringWithTomcatTest.java b/spring-web-lab/src/test/java/pl/mperor/lab/spring/SpringWithTomcatTest.java new file mode 100644 index 0000000..580c656 --- /dev/null +++ b/spring-web-lab/src/test/java/pl/mperor/lab/spring/SpringWithTomcatTest.java @@ -0,0 +1,34 @@ +package pl.mperor.lab.spring; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.boot.test.web.server.LocalServerPort; +import org.springframework.web.client.RestClient; + +import java.net.URI; + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +public class SpringWithTomcatTest { + + @LocalServerPort + private int port; + @Autowired + private TestRestTemplate restTemplate; + + @Test + public void shouldReturnHealthStatusUp() { + var body = RestClient.create() + .get() + .uri("http://localhost:{port}/actuator/health", port) + .retrieve() + .body(String.class); + Assertions.assertThat(body).contains("{\"status\":\"UP\"}"); + + var restTemplateBody = restTemplate.getForObject(URI.create("/actuator/health"), String.class); + Assertions.assertThat(restTemplateBody).contains("{\"status\":\"UP\"}"); + } + +} \ No newline at end of file diff --git a/spring-web-lab/src/test/java/pl/mperor/lab/spring/controller/HelloControllerTest.java b/spring-web-lab/src/test/java/pl/mperor/lab/spring/controller/HelloControllerTest.java new file mode 100644 index 0000000..dfcbc31 --- /dev/null +++ b/spring-web-lab/src/test/java/pl/mperor/lab/spring/controller/HelloControllerTest.java @@ -0,0 +1,22 @@ +package pl.mperor.lab.spring.controller; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.test.web.servlet.assertj.MockMvcTester; + +@WebMvcTest +class HelloControllerTest { + + @Autowired + MockMvcTester tester; + + @Test + void shouldReturnHelloWorld() { + tester.get() + .uri("/hello") + .exchange() + .assertThat().hasStatusOk() + .bodyText().isEqualTo("🗺️ Hello World!"); + } +} \ No newline at end of file diff --git a/spring-web-lab/src/test/java/pl/mperor/lab/spring/jackson/JacksonTest.java b/spring-web-lab/src/test/java/pl/mperor/lab/spring/jackson/JacksonTest.java index 7fdc321..39fe0b9 100644 --- a/spring-web-lab/src/test/java/pl/mperor/lab/spring/jackson/JacksonTest.java +++ b/spring-web-lab/src/test/java/pl/mperor/lab/spring/jackson/JacksonTest.java @@ -15,6 +15,7 @@ public class JacksonTest { @Autowired private JacksonTester sampleTester; + @Autowired private JacksonTester> wrappedTester; @@ -36,7 +37,7 @@ void testSampleMarshalling() throws IOException { Assertions.assertThat(wrappedTester.write(wrapped)).isEqualToJson(""" { "sample" : { - "integer" : 1, + "integer" : 1, "string" : "Text" } }