-
Notifications
You must be signed in to change notification settings - Fork 420
/
ExampleControllerAPITest.java
58 lines (46 loc) · 2.02 KB
/
ExampleControllerAPITest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package example;
import example.person.Person;
import example.person.PersonRepository;
import example.weather.WeatherResponse;
import example.weather.WeatherClient;
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.test.web.servlet.MockMvc;
import java.util.Optional;
import static org.mockito.BDDMockito.given;
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;
@WebMvcTest(controllers = ExampleController.class)
public class ExampleControllerAPITest {
@Autowired
private MockMvc mockMvc;
@MockBean
private PersonRepository personRepository;
@MockBean
private WeatherClient weatherClient;
@Test
public void shouldReturnHelloWorld() throws Exception {
mockMvc.perform(get("/hello"))
.andExpect(content().string("Hello World!"))
.andExpect(status().is2xxSuccessful());
}
@Test
public void shouldReturnFullName() throws Exception {
var peter = new Person("Peter", "Pan");
given(personRepository.findByLastName("Pan")).willReturn(Optional.of(peter));
mockMvc.perform(get("/hello/Pan"))
.andExpect(content().string("Hello Peter Pan!"))
.andExpect(status().is2xxSuccessful());
}
@Test
public void shouldReturnCurrentWeather() throws Exception {
var weatherResponse = new WeatherResponse("raining", "a light drizzle");
given(weatherClient.fetchWeather()).willReturn(Optional.of(weatherResponse));
mockMvc.perform(get("/weather"))
.andExpect(status().is2xxSuccessful())
.andExpect(content().string("raining: a light drizzle"));
}
}