From 806264de9a1358ee149432f21b29bfb95787952d Mon Sep 17 00:00:00 2001 From: hirst Date: Sat, 1 Jun 2024 16:16:16 +0200 Subject: [PATCH] added basic tests for dummy --- .idea/compiler.xml | 1 + .../java/com/learningpulse/dummy/Test.java | 16 ----- .../learningpulse/dummy/TestController.java | 29 +++++++++ .../dummy/services/GreetingService.java | 12 ++++ .../dummy/DummyApplicationSmokeTest.java | 27 ++++++++ .../dummy/DummyApplicationTest.java | 62 +++++++++++++++++++ .../dummy/DummyApplicationTests.java | 13 ---- .../dummy/services/GreetingServiceTest.java | 21 +++++++ 8 files changed, 152 insertions(+), 29 deletions(-) delete mode 100644 dummy/src/main/java/com/learningpulse/dummy/Test.java create mode 100644 dummy/src/main/java/com/learningpulse/dummy/TestController.java create mode 100644 dummy/src/main/java/com/learningpulse/dummy/services/GreetingService.java create mode 100644 dummy/src/test/java/com/learningpulse/dummy/DummyApplicationSmokeTest.java create mode 100644 dummy/src/test/java/com/learningpulse/dummy/DummyApplicationTest.java delete mode 100644 dummy/src/test/java/com/learningpulse/dummy/DummyApplicationTests.java create mode 100644 dummy/src/test/java/com/learningpulse/dummy/services/GreetingServiceTest.java diff --git a/.idea/compiler.xml b/.idea/compiler.xml index b38cbb0..e946f82 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -2,6 +2,7 @@ + diff --git a/dummy/src/main/java/com/learningpulse/dummy/Test.java b/dummy/src/main/java/com/learningpulse/dummy/Test.java deleted file mode 100644 index 7da105e..0000000 --- a/dummy/src/main/java/com/learningpulse/dummy/Test.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.learningpulse.dummy; - -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -@RestController -@RequestMapping("/api/v1/dummy") -public class Test { - - @GetMapping("/test") - public ResponseEntity test() { - return ResponseEntity.ok("Hello, World!"); - } -} diff --git a/dummy/src/main/java/com/learningpulse/dummy/TestController.java b/dummy/src/main/java/com/learningpulse/dummy/TestController.java new file mode 100644 index 0000000..bb0cc02 --- /dev/null +++ b/dummy/src/main/java/com/learningpulse/dummy/TestController.java @@ -0,0 +1,29 @@ +package com.learningpulse.dummy; + +import com.learningpulse.dummy.services.GreetingService; +import lombok.NoArgsConstructor; +import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/api/v1/dummy") +@RequiredArgsConstructor +public class TestController { + + private final GreetingService greetingService; + + @GetMapping("/test") + public ResponseEntity test() { + return ResponseEntity.ok("Hello, World!"); + } + + @GetMapping("/greeting") + public @ResponseBody String greeting() { + return greetingService.greet(); + } +} diff --git a/dummy/src/main/java/com/learningpulse/dummy/services/GreetingService.java b/dummy/src/main/java/com/learningpulse/dummy/services/GreetingService.java new file mode 100644 index 0000000..0832d65 --- /dev/null +++ b/dummy/src/main/java/com/learningpulse/dummy/services/GreetingService.java @@ -0,0 +1,12 @@ +package com.learningpulse.dummy.services; + +import lombok.NoArgsConstructor; +import org.springframework.stereotype.Service; + +@Service +@NoArgsConstructor +public class GreetingService { + public String greet() { + return "I work!"; + } +} diff --git a/dummy/src/test/java/com/learningpulse/dummy/DummyApplicationSmokeTest.java b/dummy/src/test/java/com/learningpulse/dummy/DummyApplicationSmokeTest.java new file mode 100644 index 0000000..8760285 --- /dev/null +++ b/dummy/src/test/java/com/learningpulse/dummy/DummyApplicationSmokeTest.java @@ -0,0 +1,27 @@ +package com.learningpulse.dummy; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +@SpringBootTest +public class DummyApplicationSmokeTest { + + @Autowired + private TestController testController; + + @Autowired + private DummyApplication dummyApplication; + + @Test + void TestControllerExist() throws Exception { + assertThat(testController).isNotNull(); + } + + @Test + void DummyApplicationExist() throws Exception { + assertThat(dummyApplication).isNotNull(); + } +} diff --git a/dummy/src/test/java/com/learningpulse/dummy/DummyApplicationTest.java b/dummy/src/test/java/com/learningpulse/dummy/DummyApplicationTest.java new file mode 100644 index 0000000..683e96b --- /dev/null +++ b/dummy/src/test/java/com/learningpulse/dummy/DummyApplicationTest.java @@ -0,0 +1,62 @@ +package com.learningpulse.dummy; + +import com.learningpulse.dummy.services.GreetingService; +import org.apache.catalina.security.SecurityConfig; +import org.junit.jupiter.api.BeforeEach; +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.boot.test.mock.mockito.MockBean; +import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +import static org.hamcrest.Matchers.containsString; +import static org.mockito.Mockito.when; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@WebMvcTest(TestController.class) +@ContextConfiguration(classes = {TestController.class, SecurityConfig.class}) +@WithMockUser(username = "admin", password = "admin") +class DummyApplicationTest { + + @Autowired + private WebApplicationContext webApplicationContext; + + private MockMvc mockMvc; + + @MockBean + private GreetingService greetingService; + + @BeforeEach + void setup() { + mockMvc = MockMvcBuilders + .webAppContextSetup(webApplicationContext) + .apply(SecurityMockMvcConfigurers.springSecurity()) + .build(); + } + + @Test + void shouldReturnDefaultMessage() throws Exception { + this.mockMvc.perform(get("/api/v1/dummy/test")) + .andDo(print()) + .andExpect(status().isOk()) + .andExpect(content().string(containsString("Hello, World!"))); + } + + @Test + void greetingShouldReturnMessageFromService() throws Exception { + when(greetingService.greet()).thenReturn("I work!"); + this.mockMvc.perform(get("/api/v1/dummy/greeting")) + .andDo(print()) + .andExpect(status().isOk()) + .andExpect(content().string(containsString("I work!"))); + } +} diff --git a/dummy/src/test/java/com/learningpulse/dummy/DummyApplicationTests.java b/dummy/src/test/java/com/learningpulse/dummy/DummyApplicationTests.java deleted file mode 100644 index 16a7b3c..0000000 --- a/dummy/src/test/java/com/learningpulse/dummy/DummyApplicationTests.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.learningpulse.dummy; - -import org.junit.jupiter.api.Test; -import org.springframework.boot.test.context.SpringBootTest; - -@SpringBootTest -class DummyApplicationTests { - - @Test - void contextLoads() { - } - -} diff --git a/dummy/src/test/java/com/learningpulse/dummy/services/GreetingServiceTest.java b/dummy/src/test/java/com/learningpulse/dummy/services/GreetingServiceTest.java new file mode 100644 index 0000000..3362f31 --- /dev/null +++ b/dummy/src/test/java/com/learningpulse/dummy/services/GreetingServiceTest.java @@ -0,0 +1,21 @@ +package com.learningpulse.dummy.services; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class GreetingServiceTest { + + private GreetingService greetingService; + + @BeforeEach + void setUp() { + greetingService = new GreetingService(); + } + + @Test + void whenGreet_thenReturnGreet() { + assertEquals("I work!", greetingService.greet()); + } +}