Skip to content

Commit b22498f

Browse files
committed
use proper AssertJ asserts that generate a useful error message
In case of test failure, it's important that the error message shows all the needed information: what was the actual/expected value etc. This commit makes many asserts show this info. For example, * `assertThat(a == b).isTrue()` does NOT show what were `a` and `b` values; * `assertThat(list.contain("foo")).isTrue()` does NOT show what was the `list` value; * `assertThat(model == 32 || model == 64).isTrue()` does NOT show what was `model` value etc.
1 parent 3b1b226 commit b22498f

Some content is hidden

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

49 files changed

+407
-479
lines changed

java/src/org/openqa/selenium/Architecture.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,6 @@ public static Architecture extractFromSysProperty(String arch) {
142142
}
143143
}
144144

145-
throw new UnsupportedOperationException("Unknown architecture: " + arch);
145+
throw new UnsupportedOperationException("Unknown architecture: \"" + arch + '"');
146146
}
147147
}

java/src/org/openqa/selenium/bidi/script/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ java_library(
2020
"//java/src/org/openqa/selenium/json",
2121
"//java/src/org/openqa/selenium/remote/http",
2222
artifact("com.google.auto.service:auto-service-annotations"),
23+
artifact("org.jspecify:jspecify"),
2324
],
2425
)

java/src/org/openqa/selenium/bidi/script/PrimitiveProtocolValue.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,9 @@ public Map<String, Object> toJson() {
5252

5353
return unmodifiableMap(toReturn);
5454
}
55+
56+
@Override
57+
public String toString() {
58+
return String.format("%s{type:%s, value:%s}", getClass().getSimpleName(), type, value);
59+
}
5560
}

java/src/org/openqa/selenium/bidi/script/RegExpValue.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,22 @@
1818
package org.openqa.selenium.bidi.script;
1919

2020
import java.util.Map;
21+
import java.util.Objects;
2122
import java.util.TreeMap;
23+
import org.jspecify.annotations.Nullable;
2224
import org.openqa.selenium.json.JsonInput;
2325

2426
public class RegExpValue extends LocalValue {
2527

2628
private final String pattern;
27-
private String flags;
29+
30+
@Nullable private final String flags;
2831

2932
public RegExpValue(String pattern) {
30-
this.pattern = pattern;
33+
this(pattern, null);
3134
}
3235

33-
public RegExpValue(String pattern, String flags) {
36+
public RegExpValue(String pattern, @Nullable String flags) {
3437
this.pattern = pattern;
3538
this.flags = flags;
3639
}
@@ -78,7 +81,25 @@ public String getPattern() {
7881
return pattern;
7982
}
8083

84+
@Nullable
8185
public String getFlags() {
8286
return flags;
8387
}
88+
89+
@Override
90+
public boolean equals(Object object) {
91+
if (!(object instanceof RegExpValue)) return false;
92+
RegExpValue other = (RegExpValue) object;
93+
return Objects.equals(pattern, other.pattern) && Objects.equals(flags, other.flags);
94+
}
95+
96+
@Override
97+
public int hashCode() {
98+
return Objects.hash(pattern, flags);
99+
}
100+
101+
@Override
102+
public String toString() {
103+
return String.format("%s{pattern:%s, flags:%s}", getClass().getSimpleName(), pattern, flags);
104+
}
84105
}

java/src/org/openqa/selenium/bidi/script/RemoteValue.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,4 +254,10 @@ private static Object deserializeValue(Object value, Type type) {
254254

255255
return finalValue;
256256
}
257+
258+
@Override
259+
public String toString() {
260+
return String.format(
261+
"%s{type:%s, value:%s}", getClass().getSimpleName(), type, value.orElse(null));
262+
}
257263
}

java/src/org/openqa/selenium/print/PageMargin.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
package org.openqa.selenium.print;
1919

20-
import java.util.HashMap;
2120
import java.util.Map;
2221
import org.jspecify.annotations.NullMarked;
2322

@@ -59,12 +58,10 @@ public double getRight() {
5958
}
6059

6160
public Map<String, Object> toMap() {
62-
final Map<String, Object> options = new HashMap<>(7);
63-
options.put("top", getTop());
64-
options.put("bottom", getBottom());
65-
options.put("left", getLeft());
66-
options.put("right", getRight());
67-
68-
return options;
61+
return Map.of(
62+
"top", getTop(),
63+
"bottom", getBottom(),
64+
"left", getLeft(),
65+
"right", getRight());
6966
}
7067
}

java/test/org/openqa/selenium/ArchitectureTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,14 @@ void determineArchARM() {
121121
void determineArchEmpty() {
122122
assertThatExceptionOfType(UnsupportedOperationException.class)
123123
.isThrownBy(() -> Architecture.extractFromSysProperty(""))
124-
.withMessageContaining("Unknown architecture");
124+
.withMessage("Unknown architecture: \"\"");
125125
}
126126

127127
@Test
128128
void determineArchBogus() {
129129
assertThatExceptionOfType(UnsupportedOperationException.class)
130130
.isThrownBy(() -> Architecture.extractFromSysProperty("hoobaflooba"))
131-
.withMessageContaining("Unknown architecture");
131+
.withMessage("Unknown architecture: \"hoobaflooba\"");
132132
}
133133

134134
@Test
@@ -139,7 +139,7 @@ void determineArchMixedCasing() {
139139
@Test
140140
void dataModelIs32Or64BitOnCurrentArchitecture() {
141141
int model = Architecture.getCurrent().getDataModel();
142-
assertThat(model == 32 || model == 64).isTrue();
142+
assertThat(model).isIn(32, 64);
143143
}
144144

145145
@Test

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,7 @@ public void testGetAllCookies() {
108108
assertCookieIsNotPresentWithName(key1);
109109
assertCookieIsNotPresentWithName(key2);
110110

111-
Set<Cookie> cookies = driver.manage().getCookies();
112-
int countBefore = cookies.size();
111+
int countBefore = driver.manage().getCookies().size();
113112

114113
Cookie one = new Cookie.Builder(key1, "value").build();
115114
Cookie two = new Cookie.Builder(key2, "value").build();
@@ -118,11 +117,10 @@ public void testGetAllCookies() {
118117
driver.manage().addCookie(two);
119118

120119
openAnotherPage();
121-
cookies = driver.manage().getCookies();
120+
Set<Cookie> cookies = driver.manage().getCookies();
122121
assertThat(cookies).hasSize(countBefore + 2);
123122

124-
assertThat(cookies.contains(one)).isTrue();
125-
assertThat(cookies.contains(two)).isTrue();
123+
assertThat(cookies).contains(one, two);
126124
}
127125

128126
@Test

java/test/org/openqa/selenium/ElementAttributeTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ void testCanReturnATextApproximationOfTheStyleAttribute() {
258258

259259
String style = driver.findElement(By.id("red-item")).getAttribute("style");
260260

261-
assertThat(style.toLowerCase().contains("background-color")).isTrue();
261+
assertThat(style).containsIgnoringCase("background-color");
262262
}
263263

264264
@Test

java/test/org/openqa/selenium/ElementDomAttributeTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ void testCanReturnATextApproximationOfTheStyleAttribute() {
221221

222222
String style = driver.findElement(By.id("red-item")).getDomAttribute("style");
223223

224-
assertThat(style.toLowerCase().contains("background-color")).isTrue();
224+
assertThat(style).containsIgnoringCase("background-color");
225225
}
226226

227227
@Test

0 commit comments

Comments
 (0)