Différences
Ci-dessous, les différences entre deux révisions de la page.
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] jcheron | web:framework:spring:tests [2023/12/18 08:45] (Version actuelle) – [Autorisations] jcheron | ||
---|---|---|---|
Ligne 28: | Ligne 28: | ||
< | < | ||
< | < | ||
- | < | + | < |
< | < | ||
</ | </ | ||
Ligne 71: | Ligne 71: | ||
</ | </ | ||
==== Test ==== | ==== Test ==== | ||
+ | |||
+ | Mocking : | ||
+ | * Serveur : <wrap round box> | ||
+ | * Service HelloService : <wrap round box> | ||
Ligne 110: | Ligne 114: | ||
===== Tests d' | ===== Tests d' | ||
+ | |||
+ | Test d' | ||
+ | |||
+ | <sxh 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(" | ||
+ | String.class)).contains(" | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
==== @SpringBootTest & @AutoConfigureMockMvc ==== | ==== @SpringBootTest & @AutoConfigureMockMvc ==== | ||
+ | |||
+ | Mock du serveur web, remplacé par **MockMvc** : | ||
+ | |||
<sxh java; | <sxh java; | ||
@SpringBootTest | @SpringBootTest | ||
Ligne 152: | Ligne 180: | ||
==== Autorisations ==== | ==== Autorisations ==== | ||
+ | |||
+ | Mocking : | ||
+ | |||
+ | * Serveur web <wrap round box> | ||
+ | * Roles/Users <wrap round box> | ||
<sxh java; | <sxh 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(" | ||
+ | @BeforeEach | ||
+ | void setUp() throws Exception { | ||
+ | WebDriverManager.chromedriver().setup(); | ||
+ | ChromeOptions options = new ChromeOptions(); | ||
+ | options.addArguments(" | ||
+ | options.addArguments(" | ||
+ | options.addArguments(" | ||
+ | driver = new ChromeDriver(options); | ||
+ | baseUrl = " | ||
+ | navigateTo("/ | ||
+ | driver.manage().window().maximize(); | ||
+ | driver.manage().timeouts().implicitlyWait(120, | ||
+ | } | ||
+ | |||
+ | @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, | ||
+ | assertTrue(driver.findElement(ByCssSelector.cssSelector(cssSelector)).getText().contains(text)); | ||
+ | } | ||
+ | |||
+ | private void assertElementAttributeContainsText(String cssSelector, | ||
+ | String text) { | ||
+ | assertTrue(driver.findElement(ByCssSelector.cssSelector(cssSelector)).getAttribute(attribute) | ||
+ | .contains(text)); | ||
+ | } | ||
+ | |||
+ | public void waitForTextToAppear(String textToAppear, | ||
+ | WebDriverWait wait = new WebDriverWait(driver, | ||
+ | wait.until(ExpectedConditions.textToBePresentInElement(element, | ||
+ | } | ||
+ | |||
+ | public void waitForTextToAppear(String textToAppear, | ||
+ | waitForTextToAppear(textToAppear, | ||
+ | } | ||
+ | |||
+ | @Test | ||
+ | void helloRouteShouldReturnBonjour() { | ||
+ | assertTrue(driver.getCurrentUrl().contains(" | ||
+ | assertElementContainsText(" | ||
+ | } | ||
+ | |||
+ | @Test | ||
+ | void helloWithJsRouteShouldReturnLength() { | ||
+ | String msg = " | ||
+ | navigateTo("/ | ||
+ | assertTrue(driver.getCurrentUrl().contains("/ | ||
+ | assertElementAttributeContainsText("# | ||
+ | btnClick("# | ||
+ | assertElementContainsText("# | ||
+ | } | ||
+ | |||
+ | } | ||
</ | </ | ||
Ligne 194: | Ligne 307: | ||
< | < | ||
< | < | ||
- | < | + | < |
< | < | ||
< | < |