added basic tests for dummy

This commit is contained in:
hirst 2024-06-01 16:16:16 +02:00
parent 59ce9d1a37
commit 806264de9a
8 changed files with 152 additions and 29 deletions

View file

@ -2,6 +2,7 @@
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile default="true" name="Default" enabled="true" />
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />

View file

@ -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<String> test() {
return ResponseEntity.ok("Hello, World!");
}
}

View file

@ -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<String> test() {
return ResponseEntity.ok("Hello, World!");
}
@GetMapping("/greeting")
public @ResponseBody String greeting() {
return greetingService.greet();
}
}

View file

@ -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!";
}
}

View file

@ -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();
}
}

View file

@ -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!")));
}
}

View file

@ -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() {
}
}

View file

@ -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());
}
}