Ceci est une ancienne révision du document !
Tests Spring
Dépendances pom.xml
<dependencies> ... <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-test</artifactId> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java --> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager --> <dependency> <groupId>io.github.bonigarcia</groupId> <artifactId>webdrivermanager</artifactId> <version>5.3.1</version> <scope>test</scope> </dependency> </dependencies>
@WebMvcTest
Tester un composant (Controller, service…)
Controller
@Controller
public class HelloController {
@Autowired
private HelloService helloService;
@GetMapping("/hello")
public @ResponseBody String helloAction() {
return helloService.getMessage();
}
@ModelAttribute("message")
public String getMessage() {
return helloService.getMessage();
}
@GetMapping("/hello/view")
public String helloViewAction() {
return "hello";
}
@GetMapping("/auth/hello")
public @ResponseBody String authHelloAction() {
return helloService.getAuthMessage();
}
@GetMapping("/hello/js/{msg}")
public String helloWithJSAction(@PathVariable String msg) {
return "helloJs";
}
}
Test
@WebMvcTest(HelloController.class)
@ContextConfiguration(classes = {WebSecurityConfig.class, SpringTestsApplication.class})
class HelloControllerTest {
@MockBean
private HelloService helloService;
@Autowired
private MockMvc mockMvc;
@Test
void helloShouldReturnBonjour() throws Exception {
// Given
when(helloService.getMessage()).thenReturn("Bonjour");
// When
ResultActions results = this.mockMvc.perform(MockMvcRequestBuilders.get("/hello"));
// Then
results.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(content().string(containsString("Bonjour")));
}
@Test
void helloViewShouldReturnBonjour() throws Exception {
// Given
when(helloService.getMessage()).thenReturn("Bonjour");
// When
ResultActions results = this.mockMvc.perform(MockMvcRequestBuilders.get("/hello/view"));
// Then
results.andExpect(view().name("hello")).andExpect(model().attribute("message", "Bonjour"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(content().string(containsString("Bonjour")));
}
}
Tests d'intégration
@SpringBootTest & @AutoConfigureMockMvc
@SpringBootTest
@AutoConfigureMockMvc
@AutoConfigureTestDatabase(replace = Replace.NONE)
class RestUserControllerTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private UserService userService;
private static User testUser;
@BeforeEach
public void setup() {
testUser = userService.createUser("Bob", "Duke");
}
@AfterEach
public void tearDown() {
userService.deleteAll();
}
@Test
void getAllShouldReturnAllUsers() throws Exception {
this.mockMvc.perform(MockMvcRequestBuilders.get("/rest/users"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.size()").value(1))
.andExpect(MockMvcResultMatchers.jsonPath("$[0].firstname").value("Bob"))
.andExpect(MockMvcResultMatchers.jsonPath("$[0].lastname").value("Duke"));
}
}
Test sécurité
Authentification
Autorisations
@SpringBootTest
@AutoConfigureMockMvc
class SecureApplicationTests {
@Autowired
private MockMvc mockMvc;
@Test
@WithMockUser("admin")
void authHelloWithAdminShouldReturnAdmin() throws Exception {
this.mockMvc.perform(get("/auth/hello")).andDo(print()).andExpect(status().isOk())
.andExpect(content().string(containsString("admin")));
}
@Test
@WithAnonymousUser
void authHelloWithAnonymousUserShouldReturn401() throws Exception {
this.mockMvc.perform(get("/auth/hello")).andDo(print()).andExpect(status().isUnauthorized());
}
}
Selenium tests
Tests du comportement côté client
Couverture
Intégration de Jacoco :
<build> <plugins> ... <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.8.8</version> <executions> <execution> <goals> <goal>prepare-agent</goal> </goals> </execution> <execution> <id>report</id> <phase>prepare-package</phase> <goals> <goal>report</goal> </goals> </execution> </executions> </plugin> </plugins> </build>