@ExtendWith(MockitoExtension.class)
public class SuperHeroControllerMockMvcStandaloneTest {
private MockMvc mvc;
@Mock
private SuperHeroRepository superHeroRepository;
@InjectMocks
private SuperHeroController superHeroController;
private JacksonTester<SuperHero> jsonSuperHero;
@BeforeEach
public void setup() {
JacksonTester.initFields(this, new ObjectMapper());
mvc = MockMvcBuilders.standaloneSetup(superHeroController)
.setControllerAdvice(new SuperHeroExceptionHandler())
.addFilters(new SuperHeroFilter())
.build();
}
@Test
public void canRetrieveByIdWhenExists() throws Exception {
given(superHeroRepository.getSuperHero(2))
.willReturn(new SuperHero("Rob", "Mannon", "RobotMan"));
MockHttpServletResponse response = mvc.perform(
get("/superheroes/2")
.accept(MediaType.APPLICATION_JSON))
.andReturn().getResponse();
assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
assertThat(response.getContentAsString()).isEqualTo(
jsonSuperHero.write(new SuperHero("Rob", "Mannon", "RobotMan")).getJson()
);
}
@Test
public void canRetrieveByIdWhenDoesNotExist() throws Exception {
given(superHeroRepository.getSuperHero(2))
.willThrow(new NonExistingHeroException());
MockHttpServletResponse response = mvc.perform(
get("/superheroes/2")
.accept(MediaType.APPLICATION_JSON))
.andReturn().getResponse();
assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value());
assertThat(response.getContentAsString()).isEmpty();
}
@Test
public void canRetrieveByNameWhenExists() throws Exception {
given(superHeroRepository.getSuperHero("RobotMan"))
.willReturn(Optional.of(new SuperHero("Rob", "Mannon", "RobotMan")));
MockHttpServletResponse response = mvc.perform(
get("/superheroes/?name=RobotMan")
.accept(MediaType.APPLICATION_JSON))
.andReturn().getResponse();
assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
assertThat(response.getContentAsString()).isEqualTo(
jsonSuperHero.write(new SuperHero("Rob", "Mannon", "RobotMan")).getJson()
);
}
@Test
public void canRetrieveByNameWhenDoesNotExist() throws Exception {
given(superHeroRepository.getSuperHero("RobotMan"))
.willReturn(Optional.empty());
MockHttpServletResponse response = mvc.perform(
get("/superheroes/?name=RobotMan")
.accept(MediaType.APPLICATION_JSON))
.andReturn().getResponse();
assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
assertThat(response.getContentAsString()).isEqualTo("null");
}
@Test
public void canCreateANewSuperHero() throws Exception {
MockHttpServletResponse response = mvc.perform(
post("/superheroes/").contentType(MediaType.APPLICATION_JSON).content(
jsonSuperHero.write(new SuperHero("Rob", "Mannon", "RobotMan")).getJson()
)).andReturn().getResponse();
assertThat(response.getStatus()).isEqualTo(HttpStatus.CREATED.value());
}
@Test
public void headerIsPresent() throws Exception {
MockHttpServletResponse response = mvc.perform(
get("/superheroes/2")
.accept(MediaType.APPLICATION_JSON))
.andReturn().getResponse();
assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
assertThat(response.getHeaders("X-SUPERHERO-APP")).containsOnly("super-header");
}
}