web:framework:spring:tests

Ceci est une ancienne révision du document !


Tests Spring

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<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>
         
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.github.bonigarcia</groupId>
        <artifactId>webdrivermanager</artifactId>
        <version>5.3.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>

Tester un composant (Controller, service…)

HelloControllerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
@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";
  }
}

HelloControllerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
@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")));
  }
}

  • web/framework/spring/tests.1702858663.txt.gz
  • Dernière modification : il y a 16 mois
  • de jcheron