mirror of
https://github.com/OtusTeam/Spring.git
synced 2026-06-20 11:14:34 +00:00
L05 SpringBoot
This commit is contained in:
@@ -10,7 +10,7 @@
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>3.0.0</version>
|
||||
<version>2.7.3</version>
|
||||
</parent>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package ru.otus.service;
|
||||
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import javax.annotation.PostConstruct;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
|
||||
-64
@@ -1,64 +0,0 @@
|
||||
package ru.otus.controller;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
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.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import ru.otus.service.GreetingService;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
|
||||
|
||||
@WebMvcTest({GreetingController.class, GreetingControllerRstyle.class})
|
||||
class GreetingControllerTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@MockBean
|
||||
private GreetingService greetingService;
|
||||
|
||||
@Test
|
||||
void sayHelloTest() throws Exception {
|
||||
String name = "Mr.Test";
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put(name, "Hello, " + name);
|
||||
String expectedResult = "{\"Mr.Test\":\"Hello, Mr.Test\"}";
|
||||
|
||||
when(greetingService.sayHello(name)).thenReturn(map);
|
||||
MockHttpServletResponse result = mockMvc.perform(get(String.format("/hello?name=%s", name)))
|
||||
.andReturn()
|
||||
.getResponse();
|
||||
|
||||
verify(greetingService, times(1)).sayHello(name);
|
||||
System.out.println();
|
||||
assertThat(result.getContentAsString()).isEqualTo(expectedResult);
|
||||
}
|
||||
|
||||
@Test
|
||||
void sayHelloTestMock() throws Exception {
|
||||
String name = "Mr.Test";
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put(name, "Hello, " + name);
|
||||
String expectedResult = "{\"Mr.Test\":\"Hello, Mr.Test\"}";
|
||||
|
||||
when(greetingService.sayHello(name)).thenReturn(map);
|
||||
MockHttpServletResponse result = mockMvc.perform(get(String.format("/hello/%s", name)))
|
||||
.andReturn()
|
||||
.getResponse();
|
||||
|
||||
verify(greetingService, times(1)).sayHello(name);
|
||||
System.out.println();
|
||||
assertThat(result.getContentAsString()).isEqualTo(expectedResult);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user