Skip to content

Instantly share code, notes, and snippets.

@gbzarelli
Created July 30, 2020 01:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gbzarelli/a733e7c2faa15c58b7ebb6c081ea2cb3 to your computer and use it in GitHub Desktop.
Save gbzarelli/a733e7c2faa15c58b7ebb6c081ea2cb3 to your computer and use it in GitHub Desktop.
Used in Medium article about Tests
@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