Oi Sergio,
As versões mais novas do Spring Boot estão utilizando o JUnit 5, sendo necessário adaptar os códigos das classes de teste do projeto para funcionar corretamente:
package br.com.alura.forum;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
@ExtendWith(SpringExtension.class)
@SpringBootTest
public class ForumApplicationTests {
@Test
public void contextLoads() {
assertTrue(true);
}
}
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import br.com.alura.forum.modelo.Curso;
@ExtendWith(SpringExtension.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@ActiveProfiles("test")
public class CursoRepositoryTest {
@Autowired
private CursoRepository repository;
@Autowired
private TestEntityManager em;
@Test
public void deveriaCarregarUmCursoAoBuscarPeloSeuNome() {
String nomeCurso = "HTML 5";
Curso html5 = new Curso();
html5.setNome(nomeCurso);
html5.setCategoria("Programacao");
em.persist(html5);
Curso curso = repository.findByNome(nomeCurso);
assertNotNull(curso);
assertEquals(nomeCurso, curso.getNome());
}
@Test
public void naoDeveriaCarregarUmCursoCujoNomeNaoEstejaCadastrado() {
String nomeCurso = "JPA";
Curso curso = repository.findByNome(nomeCurso);
assertNull(curso);
}
}
import java.net.URI;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
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.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
@ExtendWith(SpringExtension.class)
@SpringBootTest
@AutoConfigureMockMvc
@ActiveProfiles("test")
public class AutenticacaoControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
public void deveriaDevolver400CasoDadosDeAutenticacaoEstejamIncorretos() throws Exception {
URI uri = new URI("/auth");
String json = "{\"email\":\"invalido@email.com\",\"senha\":\"123456\"}";
mockMvc
.perform(MockMvcRequestBuilders
.post(uri)
.content(json)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers
.status()
.is(400));
}
}