Created
July 30, 2020 01:30
-
-
Save gbzarelli/a733e7c2faa15c58b7ebb6c081ea2cb3 to your computer and use it in GitHub Desktop.
Used in Medium article about Tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@SpringBootTest | |
@WebMvcTest(controllers = ExampleController.class) | |
class ExampleControllerAPITest { | |
@Autowired | |
private MockMvc mockMvc; | |
@MockBean | |
private PersonRepository personRepository; | |
@MockBean | |
private WeatherClient weatherClient; | |
@Test | |
void shouldReturnFullName() throws Exception { | |
Person 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 | |
void shouldReturnCurrentWeather() throws Exception { | |
WeatherResponse weatherResponse = new WeatherResponse("Hamburg, 8°C raining"); | |
given(weatherClient.fetchWeather()).willReturn(Optional.of(weatherResponse)); | |
mockMvc.perform(get("/weather")) | |
.andExpect(status().is2xxSuccessful()) | |
.andExpect(content().string("Hamburg, 8°C raining")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment