Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jordisilvazup/5495cbeaf1297a6730dba1bdc04bf6a4 to your computer and use it in GitHub Desktop.
Save jordisilvazup/5495cbeaf1297a6730dba1bdc04bf6a4 to your computer and use it in GitHub Desktop.
package br.com.zup.edu.nossozenity.zupper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.web.server.ResponseStatusException;
import java.time.LocalDate;
import static org.junit.jupiter.api.Assertions.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest
@AutoConfigureMockMvc
@ActiveProfiles("test")
class RemoverCertificadoControllerTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private CertificadoRepository certificadoRepository;
@Autowired
private ZupperRepository zupperRepository;
private Zupper zupper;
@BeforeEach
void setUp() {
certificadoRepository.deleteAll();
zupperRepository.deleteAll();
this.zupper = new Zupper(
"Rafael Ponte",
"Dev Back End",
LocalDate.now(),
"[email protected]"
);
zupperRepository.save(zupper);
}
@Test
@DisplayName("deve remover o certificado")
void teste() throws Exception {
Certificado certificado = new Certificado(
"Testes de Integracao com Spring Test e Boot",
"Zup Edu",
"bit.ly/qwdQWES",
zupper,
TipoCertificado.CURSO
);
certificadoRepository.save(certificado);
MockHttpServletRequestBuilder request = MockMvcRequestBuilders.delete("/certificados/{id}", certificado.getId())
.contentType(MediaType.APPLICATION_JSON);
mockMvc.perform(request)
.andExpect(
status().isNoContent()
);
assertFalse(certificadoRepository.existsById(certificado.getId()), "Não deve existir um certificado para este id");
}
@Test
@DisplayName("nao deve remover um certificado não cadastrado")
void test1() throws Exception {
MockHttpServletRequestBuilder request = MockMvcRequestBuilders.delete("/certificados/{id}", Integer.MAX_VALUE)
.contentType(MediaType.APPLICATION_JSON);
Exception resolvedException = mockMvc.perform(request)
.andExpect(
status().isNotFound()
)
.andReturn()
.getResolvedException();
assertNotNull(resolvedException);
assertEquals(ResponseStatusException.class,resolvedException.getClass());
assertEquals("Certificado não cadastrado.",((ResponseStatusException)resolvedException).getReason());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment