web:framework:spring:tests

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentes Révision précédente
Prochaine révision
Révision précédente
web:framework:spring:tests [2023/12/18 01:28] – [Test] jcheronweb:framework:spring:tests [2023/12/18 08:45] (Version actuelle) – [Autorisations] jcheron
Ligne 28: Ligne 28:
  <groupId>io.github.bonigarcia</groupId>  <groupId>io.github.bonigarcia</groupId>
  <artifactId>webdrivermanager</artifactId>  <artifactId>webdrivermanager</artifactId>
- <version>5.3.1</version>+ <version>5.6.2</version>
  <scope>test</scope>  <scope>test</scope>
  </dependency>  </dependency>
Ligne 71: Ligne 71:
 </sxh> </sxh>
 ==== Test ==== ==== Test ====
 +
 +Mocking :
 +  * Serveur : <wrap round box>@MockMvc</wrap>
 +  * Service HelloService : <wrap round box>@MockBean</wrap>
  
  
Ligne 110: Ligne 114:
  
 ===== Tests d'intégration ===== ===== Tests d'intégration =====
 +
 +Test d'intégration avec lancement du serveur sur Random port (pour éviter les conflits).
 +
 +<sxh java;title:HelloControllerTest.java>
 +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
 +class HttpRequestTest {
 +
 + @LocalServerPort
 + private int port;
 +
 + @Autowired
 + private TestRestTemplate restTemplate;
 +
 + @Test
 + void greetingShouldReturnDefaultMessage() throws Exception {
 + assertThat(this.restTemplate.getForObject("http://127.0.0.1:" + port + "/",
 + String.class)).contains("Hello World!");
 + }
 +}
 +</sxh>
 +
 ==== @SpringBootTest & @AutoConfigureMockMvc ==== ==== @SpringBootTest & @AutoConfigureMockMvc ====
 +
 +Mock du serveur web, remplacé par **MockMvc** :
 +
 <sxh java;title:HelloControllerTest.java> <sxh java;title:HelloControllerTest.java>
 @SpringBootTest @SpringBootTest
Ligne 152: Ligne 180:
  
 ==== Autorisations ==== ==== Autorisations ====
 +
 +Mocking :
 +
 +  * Serveur web <wrap round box>MockMvc</wrap>
 +  * Roles/Users <wrap round box>@WithMockUser</wrap> <wrap round box>@WithAnonymousUser</wrap>
  
 <sxh java;title:SecureApplicationTests.java> <sxh java;title:SecureApplicationTests.java>
Ligne 181: Ligne 214:
 Tests du comportement côté client Tests du comportement côté client
  
-<sxh java;title:SecureApplicationTests.java>+<sxh java;title:SeleniumDemoTest.java> 
 +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) 
 +class SeleniumDemoTest {
  
 +  private WebDriver driver;
 +
 +  @LocalServerPort
 +  int randomServerPort;
 +
 +  String baseUrl;
 +
 +  @SuppressWarnings("deprecation")
 +  @BeforeEach
 +  void setUp() throws Exception {
 +    WebDriverManager.chromedriver().setup();
 +    ChromeOptions options = new ChromeOptions();
 +    options.addArguments("--no-sandbox");
 +    options.addArguments("--disable-dev-shm-usage");
 +    options.addArguments("--headless");
 +    driver = new ChromeDriver(options);
 +    baseUrl = "http://127.0.0.1:" + randomServerPort;
 +    navigateTo("/hello");
 +    driver.manage().window().maximize();
 +    driver.manage().timeouts().implicitlyWait(120, TimeUnit.MILLISECONDS);
 +  }
 +
 +  @AfterEach
 +  void tearDown() throws Exception {
 +    if (driver != null) {
 +      driver.quit();
 +    }
 +  }
 +
 +  private void navigateTo(String relativeURL) {
 +    driver.navigate().to(baseUrl + relativeURL);
 +  }
 +
 +  private void fillElement(String name, String content) {
 +    WebElement elm = driver.findElement(By.name(name));
 +    elm.sendKeys(content);
 +  }
 +
 +  private void btnClick(String cssSelector) {
 +    driver.findElement(ByCssSelector.cssSelector(cssSelector)).click();
 +  }
 +
 +  private void assertElementContainsText(String cssSelector, String text) {
 +    assertTrue(driver.findElement(ByCssSelector.cssSelector(cssSelector)).getText().contains(text));
 +  }
 +
 +  private void assertElementAttributeContainsText(String cssSelector, String attribute,
 +      String text) {
 +    assertTrue(driver.findElement(ByCssSelector.cssSelector(cssSelector)).getAttribute(attribute)
 +        .contains(text));
 +  }
 +
 +  public void waitForTextToAppear(String textToAppear, WebElement element, int timeout) {
 +    WebDriverWait wait = new WebDriverWait(driver, Duration.ofMillis(timeout));
 +    wait.until(ExpectedConditions.textToBePresentInElement(element, textToAppear));
 +  }
 +
 +  public void waitForTextToAppear(String textToAppear, WebElement element) {
 +    waitForTextToAppear(textToAppear, element, 3000);
 +  }
 +
 +  @Test
 +  void helloRouteShouldReturnBonjour() {
 +    assertTrue(driver.getCurrentUrl().contains("hello"));
 +    assertElementContainsText("body", "Bonjour");
 +  }
 +
 +  @Test
 +  void helloWithJsRouteShouldReturnLength() {
 +    String msg = "Bonjour";
 +    navigateTo("/hello/js/" + msg);
 +    assertTrue(driver.getCurrentUrl().contains("/hello/js/" + msg));
 +    assertElementAttributeContainsText("#msg", "value", msg);
 +    btnClick("#bt");
 +    assertElementContainsText("#length", msg.length() + "");
 +  }
 +
 +}
 </sxh> </sxh>
  
Ligne 194: Ligne 307:
      <groupId>org.jacoco</groupId>      <groupId>org.jacoco</groupId>
      <artifactId>jacoco-maven-plugin</artifactId>      <artifactId>jacoco-maven-plugin</artifactId>
-     <version>0.8.8</version>+     <version>0.8.11</version>
      <executions>      <executions>
          <execution>          <execution>
  • web/framework/spring/tests.1702859306.txt.gz
  • Dernière modification : il y a 16 mois
  • de jcheron