From 66f3b0da4cbb4d90f42264c975a1725b6345b298 Mon Sep 17 00:00:00 2001 From: Jan Klattenhoff Date: Wed, 2 Oct 2024 12:55:50 +0200 Subject: [PATCH 1/2] test: add WelcomeTest for welcome endpoint validation --- .../integration/welcome/WelcomeTest.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/test/java/de/szut/lf8_starter/integration/welcome/WelcomeTest.java diff --git a/src/test/java/de/szut/lf8_starter/integration/welcome/WelcomeTest.java b/src/test/java/de/szut/lf8_starter/integration/welcome/WelcomeTest.java new file mode 100644 index 0000000..8128642 --- /dev/null +++ b/src/test/java/de/szut/lf8_starter/integration/welcome/WelcomeTest.java @@ -0,0 +1,24 @@ +package de.szut.lf8_starter.integration.welcome; + +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.servlet.MockMvc; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@SpringBootTest +@AutoConfigureMockMvc +class WelcomeTest { + @Autowired + private MockMvc mockMvc; + + @Test + void welcomeTest() throws Exception { + mockMvc.perform(get("/welcome")) + .andExpect(status().isOk()) + .andExpect(content().string("welcome to lf8_starter")); + } +} -- 2.45.2 From 0fb924f93dfc1e259d5c39ce371f9eacdedb7509 Mon Sep 17 00:00:00 2001 From: Jan Klattenhoff Date: Wed, 2 Oct 2024 13:05:44 +0200 Subject: [PATCH 2/2] test(hello): add integration test for CreateHello endpoint --- .../integration/hello/CreateHelloTest.java | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/test/java/de/szut/lf8_starter/integration/hello/CreateHelloTest.java diff --git a/src/test/java/de/szut/lf8_starter/integration/hello/CreateHelloTest.java b/src/test/java/de/szut/lf8_starter/integration/hello/CreateHelloTest.java new file mode 100644 index 0000000..b7c2d73 --- /dev/null +++ b/src/test/java/de/szut/lf8_starter/integration/hello/CreateHelloTest.java @@ -0,0 +1,50 @@ +package de.szut.lf8_starter.integration.hello; + +import de.szut.lf8_starter.hello.HelloRepository; +import org.json.JSONObject; +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.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +import java.time.LocalDate; +import java.util.Arrays; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.hamcrest.Matchers.is; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@SpringBootTest +@AutoConfigureMockMvc(addFilters = false) +class CreateHelloTest { + @Autowired + private MockMvc mockMvc; + @Autowired + private HelloRepository helloRepository; + @Test + void createProjectTest() throws Exception { + String content = """ + { + "message": "test" + } + """; + + final var contentAsString = this.mockMvc.perform( + post("/hello").content(content).contentType(MediaType.APPLICATION_JSON) + ) + .andExpect(status().isOk()) + .andExpect(jsonPath("message", is("test"))) + .andReturn() + .getResponse() + .getContentAsString(); + + final var id = Long.parseLong(new JSONObject(contentAsString).get("id").toString()); + + final var hello = this.helloRepository.findById(id); + assertThat(hello.get().getMessage()).isEqualTo("test"); + } +} -- 2.45.2