Skip to content

Commit 4e431f0

Browse files
authored
[refactor] replace JUnit assertions by AssertJ (#16765)
in many cases, it produces a better error message in case of test failure.
1 parent f2b2ccb commit 4e431f0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+548
-586
lines changed

java/src/org/openqa/selenium/HasDownloads.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ public String getName() {
9595
return name;
9696
}
9797

98+
public boolean hasExtension(String extension) {
99+
return extension.startsWith(".") ? name.endsWith(extension) : name.endsWith('.' + extension);
100+
}
101+
98102
public long getCreationTime() {
99103
return creationTime;
100104
}

java/test/org/openqa/selenium/CookieImplementationTest.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
package org.openqa.selenium;
1919

2020
import static org.assertj.core.api.Assertions.assertThat;
21-
import static org.junit.jupiter.api.Assertions.assertThrows;
21+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2222
import static org.junit.jupiter.api.Assumptions.assumeTrue;
2323
import static org.openqa.selenium.testing.drivers.Browser.ALL;
2424
import static org.openqa.selenium.testing.drivers.Browser.CHROME;
@@ -311,9 +311,7 @@ public void testShouldWalkThePathToDeleteACookie() {
311311
assertThat(driver.manage().getCookieNamed("rodent")).isNull();
312312

313313
Set<Cookie> cookies = driver.manage().getCookies();
314-
assertThat(cookies).hasSize(2);
315-
assertThat(cookies).contains(cookie1);
316-
assertThat(cookies).contains(cookie3);
314+
assertThat(cookies).containsExactlyInAnyOrder(cookie1, cookie3);
317315

318316
driver.manage().deleteAllCookies();
319317
driver.get(domainHelper.getUrlForFirstValidHostname("child/grandchild/grandchildPage.html"));
@@ -485,14 +483,22 @@ public void testDeleteNotExistedCookie() {
485483

486484
@Test
487485
public void testDeleteEmptyNamedCookie() {
488-
assertThrows(IllegalArgumentException.class, () -> driver.manage().deleteCookieNamed(""));
489-
assertThrows(IllegalArgumentException.class, () -> driver.manage().deleteCookieNamed(" "));
486+
assertThatThrownBy(() -> driver.manage().deleteCookieNamed(""))
487+
.isInstanceOf(IllegalArgumentException.class)
488+
.hasMessage("Cookie name cannot be empty");
489+
assertThatThrownBy(() -> driver.manage().deleteCookieNamed(" "))
490+
.isInstanceOf(IllegalArgumentException.class)
491+
.hasMessage("Cookie name cannot be empty");
490492
}
491493

492494
@Test
493495
public void testGetEmptyNamedCookie() {
494-
assertThrows(IllegalArgumentException.class, () -> driver.manage().getCookieNamed(""));
495-
assertThrows(IllegalArgumentException.class, () -> driver.manage().getCookieNamed(" "));
496+
assertThatThrownBy(() -> driver.manage().getCookieNamed(""))
497+
.isInstanceOf(IllegalArgumentException.class)
498+
.hasMessage("Cookie name cannot be empty");
499+
assertThatThrownBy(() -> driver.manage().getCookieNamed(" "))
500+
.isInstanceOf(IllegalArgumentException.class)
501+
.hasMessage("Cookie name cannot be empty");
496502
}
497503

498504
@Test
@@ -554,7 +560,7 @@ private void assertCookieIsPresentWithName(final String key) {
554560
String documentCookie = getDocumentCookieOrNull();
555561
if (documentCookie != null) {
556562
assertThat(documentCookie)
557-
.as("Cookie was not present with name " + key + ", got: " + documentCookie)
563+
.as(() -> "Cookie was not present with name " + key + ", got: " + documentCookie)
558564
.contains(key + "=");
559565
}
560566
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.openqa.selenium;
19+
20+
import static org.assertj.core.api.Assertions.assertThat;
21+
22+
import org.junit.jupiter.api.Test;
23+
import org.openqa.selenium.HasDownloads.DownloadedFile;
24+
25+
class DownloadedFileTest {
26+
@Test
27+
void hasExtension() {
28+
DownloadedFile file = new DownloadedFile("hello.pdf", 0, 0, 15);
29+
assertThat(file.hasExtension("pdf")).isTrue();
30+
assertThat(file.hasExtension(".pdf")).isTrue();
31+
32+
assertThat(file.hasExtension("df")).isFalse();
33+
assertThat(file.hasExtension("f")).isFalse();
34+
assertThat(file.hasExtension("p")).isFalse();
35+
assertThat(file.hasExtension(".p")).isFalse();
36+
37+
assertThat(file.hasExtension(".txt")).isFalse();
38+
assertThat(file.hasExtension("txt")).isFalse();
39+
assertThat(file.hasExtension("")).isFalse();
40+
}
41+
}

java/test/org/openqa/selenium/bidi/input/DefaultWheelTest.java

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,9 @@
1717

1818
package org.openqa.selenium.bidi.input;
1919

20-
import static org.junit.jupiter.api.Assertions.assertFalse;
21-
import static org.junit.jupiter.api.Assertions.assertThrows;
22-
import static org.junit.jupiter.api.Assertions.assertTrue;
23-
import static org.openqa.selenium.testing.drivers.Browser.CHROME;
24-
import static org.openqa.selenium.testing.drivers.Browser.EDGE;
25-
import static org.openqa.selenium.testing.drivers.Browser.FIREFOX;
20+
import static org.assertj.core.api.Assertions.assertThat;
21+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
22+
import static org.openqa.selenium.testing.drivers.Browser.*;
2623

2724
import org.junit.jupiter.api.BeforeEach;
2825
import org.junit.jupiter.api.Test;
@@ -65,11 +62,11 @@ void shouldScrollToElement() {
6562
appServer.whereIs("scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html"));
6663
WebElement iframe = driver.findElement(By.tagName("iframe"));
6764

68-
assertFalse(inViewport(iframe));
65+
assertThat(inViewport(iframe)).isFalse();
6966

7067
input.perform("iframe", getBuilder(driver).scrollToElement(iframe).getSequences());
7168

72-
assertTrue(inViewport(iframe));
69+
assertThat(inViewport(iframe)).isTrue();
7370
}
7471

7572
@Test
@@ -88,7 +85,7 @@ void shouldScrollFromElementByGivenAmount() {
8885

8986
driver.switchTo().frame(iframe);
9087
WebElement checkbox = driver.findElement(By.name("scroll_checkbox"));
91-
assertTrue(inViewport(checkbox));
88+
assertThat(inViewport(checkbox)).isTrue();
9289
driver.switchTo().window(windowHandle);
9390
}
9491

@@ -108,26 +105,28 @@ void shouldScrollFromElementByGivenAmountWithOffset() {
108105
WebElement iframe = driver.findElement(By.tagName("iframe"));
109106
driver.switchTo().frame(iframe);
110107
WebElement checkbox = driver.findElement(By.name("scroll_checkbox"));
111-
assertTrue(inViewport(checkbox));
108+
assertThat(inViewport(checkbox)).isTrue();
112109
}
113110

114111
@Test
115112
@NotYetImplemented(EDGE)
116113
@NotYetImplemented(CHROME)
117114
void throwErrorWhenElementOriginIsOutOfViewport() {
118-
assertThrows(
119-
BiDiException.class,
120-
() -> {
121-
driver.get(
122-
appServer.whereIs(
123-
"scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html"));
124-
WebElement footer = driver.findElement(By.tagName("footer"));
125-
WheelInput.ScrollOrigin scrollOrigin = WheelInput.ScrollOrigin.fromElement(footer, 0, 50);
126-
127-
input.perform(
128-
windowHandle,
129-
getBuilder(driver).scrollFromOrigin(scrollOrigin, 0, 200).getSequences());
130-
});
115+
assertThatThrownBy(
116+
() -> {
117+
driver.get(
118+
appServer.whereIs(
119+
"scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html"));
120+
WebElement footer = driver.findElement(By.tagName("footer"));
121+
WheelInput.ScrollOrigin scrollOrigin =
122+
WheelInput.ScrollOrigin.fromElement(footer, 0, 50);
123+
124+
input.perform(
125+
windowHandle,
126+
getBuilder(driver).scrollFromOrigin(scrollOrigin, 0, 200).getSequences());
127+
})
128+
.isInstanceOf(BiDiException.class)
129+
.hasMessageContaining("move target out of bounds");
131130
}
132131

133132
@NeedsFreshDriver
@@ -148,7 +147,7 @@ void shouldScrollFromViewportByGivenAmount() {
148147

149148
wait.until(driver -> driver.findElement(By.name("nested_scrolling_frame")).isDisplayed());
150149

151-
assertTrue(inViewport(footer));
150+
assertThat(inViewport(footer)).isTrue();
152151
}
153152

154153
@NeedsFreshDriver
@@ -168,22 +167,24 @@ void shouldScrollFromViewportByGivenAmountFromOrigin() {
168167
WebElement iframe = driver.findElement(By.tagName("iframe"));
169168
driver.switchTo().frame(iframe);
170169
WebElement checkbox = driver.findElement(By.name("scroll_checkbox"));
171-
assertTrue(inViewport(checkbox));
170+
assertThat(inViewport(checkbox)).isTrue();
172171
driver.switchTo().window(windowHandle);
173172
}
174173

175174
@Test
176175
void throwErrorWhenOriginOffsetIsOutOfViewport() {
177-
assertThrows(
178-
BiDiException.class,
179-
() -> {
180-
driver.get(appServer.whereIs("scrolling_tests/frame_with_nested_scrolling_frame.html"));
181-
WheelInput.ScrollOrigin scrollOrigin = WheelInput.ScrollOrigin.fromViewport(-10, -10);
182-
183-
input.perform(
184-
windowHandle,
185-
getBuilder(driver).scrollFromOrigin(scrollOrigin, 0, 200).getSequences());
186-
});
176+
assertThatThrownBy(
177+
() -> {
178+
driver.get(
179+
appServer.whereIs("scrolling_tests/frame_with_nested_scrolling_frame.html"));
180+
WheelInput.ScrollOrigin scrollOrigin = WheelInput.ScrollOrigin.fromViewport(-10, -10);
181+
182+
input.perform(
183+
windowHandle,
184+
getBuilder(driver).scrollFromOrigin(scrollOrigin, 0, 200).getSequences());
185+
})
186+
.isInstanceOf(BiDiException.class)
187+
.hasMessageContaining("move target out of bounds");
187188
}
188189

189190
private boolean inViewport(WebElement element) {

java/test/org/openqa/selenium/build/Build.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
// under the License.
1717
package org.openqa.selenium.build;
1818

19-
import static org.junit.jupiter.api.Assertions.fail;
2019
import static org.openqa.selenium.Platform.WINDOWS;
2120
import static org.openqa.selenium.build.DevMode.isInDevMode;
2221

@@ -57,7 +56,7 @@ public void go() {
5756
executeBuild(builder);
5857
} catch (Exception e) {
5958
e.printStackTrace(System.err);
60-
fail("Cannot build");
59+
throw new AssertionError("Cannot build", e);
6160
}
6261
}
6362

@@ -83,7 +82,7 @@ private void executeBuild(ProcessBuilder builder) throws Exception {
8382
buildWatcher.start();
8483
int exitValue = process.waitFor();
8584
if (exitValue != 0) {
86-
fail("Unable to build artifacts");
85+
throw new AssertionError("Unable to build artifacts");
8786
}
8887
}
8988

java/test/org/openqa/selenium/chrome/ChromeOptionsTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import static org.assertj.core.api.InstanceOfAssertFactories.LIST;
2424
import static org.assertj.core.api.InstanceOfAssertFactories.MAP;
2525
import static org.assertj.core.api.InstanceOfAssertFactories.STRING;
26-
import static org.junit.jupiter.api.Assertions.assertEquals;
2726
import static org.openqa.selenium.chromium.ChromiumDriverLogLevel.OFF;
2827
import static org.openqa.selenium.chromium.ChromiumDriverLogLevel.SEVERE;
2928
import static org.openqa.selenium.remote.CapabilityType.ACCEPT_INSECURE_CERTS;
@@ -397,6 +396,6 @@ void shouldBeAbleToMergeAnAndroidOption() {
397396
var caps = new MutableCapabilities();
398397
var merged = original.merge(caps);
399398

400-
assertEquals(original.asMap(), merged.asMap());
399+
assertThat(merged.asMap()).isEqualTo(original.asMap());
401400
}
402401
}

java/test/org/openqa/selenium/devtools/CdpVersionFinderTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ void shouldReturnAnExactMatchIfFound() {
7676
CdpVersionFinder finder = new CdpVersionFinder(5, ImmutableList.of(v84, v85));
7777

7878
Optional<CdpInfo> info = finder.match(chrome85);
79-
assertThat(info).isEqualTo(Optional.of(v85));
79+
assertThat(info).contains(v85);
8080

8181
info = finder.match(edge84);
82-
assertThat(info).isEqualTo(Optional.of(v84));
82+
assertThat(info).contains(v84);
8383
}
8484

8585
@Test
@@ -89,7 +89,7 @@ void shouldReturnThePreviousLowestMatchIfNoExactMatchFoundWithinFuzzFactor() {
8989
CdpVersionFinder finder = new CdpVersionFinder(5, ImmutableList.of(v84));
9090

9191
Optional<CdpInfo> info = finder.match(chrome85);
92-
assertThat(info).isEqualTo(Optional.of(v84));
92+
assertThat(info).contains(v84);
9393
}
9494

9595
@Test
@@ -112,9 +112,9 @@ void canUseBrowserVersionIfNecessary() {
112112
CdpVersionFinder finder = new CdpVersionFinder(5, ImmutableList.of(v84, v85));
113113

114114
Optional<CdpInfo> info = finder.match(chromeVersion);
115-
assertThat(info).isEqualTo(Optional.of(v85));
115+
assertThat(info).contains(v85);
116116

117117
info = finder.match(edgeVersion);
118-
assertThat(info).isEqualTo(Optional.of(v84));
118+
assertThat(info).contains(v84);
119119
}
120120
}

0 commit comments

Comments
 (0)