Skip to content

Commit 25002fc

Browse files
Use assertThrows in ConfigurationUtilsTests (#116971)
This was trying to assert that the code under test threw an exception using the 'try-act-fail-catch-assert' pattern, only the 'fail' step was missing, meaning that the tests would have incorrectly passed if the method didn't throw. This switches it to using `assertThrows`, which is less easy to get wrong.
1 parent abc7f58 commit 25002fc

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

server/src/test/java/org/elasticsearch/ingest/ConfigurationUtilsTests.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,12 @@ public void testReadStringProperty() {
6565
}
6666

6767
public void testReadStringPropertyInvalidType() {
68-
try {
69-
ConfigurationUtils.readStringProperty(null, null, config, "arr");
70-
} catch (ElasticsearchParseException e) {
71-
assertThat(e.getMessage(), equalTo("[arr] property isn't a string, but of type [java.util.Arrays$ArrayList]"));
72-
}
68+
ElasticsearchParseException caught = assertThrows(
69+
ElasticsearchParseException.class,
70+
() -> ConfigurationUtils.readStringProperty(null, null, config, "arr")
71+
);
72+
assertThat(caught.getMessage(), equalTo("[arr] property isn't a string, but of type [java.util.Arrays$ArrayList]"));
73+
7374
}
7475

7576
public void testReadBooleanProperty() {
@@ -83,11 +84,11 @@ public void testReadNullBooleanProperty() {
8384
}
8485

8586
public void testReadBooleanPropertyInvalidType() {
86-
try {
87-
ConfigurationUtils.readBooleanProperty(null, null, config, "arr", true);
88-
} catch (ElasticsearchParseException e) {
89-
assertThat(e.getMessage(), equalTo("[arr] property isn't a boolean, but of type [java.util.Arrays$ArrayList]"));
90-
}
87+
ElasticsearchParseException caught = assertThrows(
88+
ElasticsearchParseException.class,
89+
() -> ConfigurationUtils.readBooleanProperty(null, null, config, "arr", true)
90+
);
91+
assertThat(caught.getMessage(), equalTo("[arr] property isn't a boolean, but of type [java.util.Arrays$ArrayList]"));
9192
}
9293

9394
public void testReadStringOrIntProperty() {
@@ -98,11 +99,11 @@ public void testReadStringOrIntProperty() {
9899
}
99100

100101
public void testReadStringOrIntPropertyInvalidType() {
101-
try {
102-
ConfigurationUtils.readStringOrIntProperty(null, null, config, "arr", null);
103-
} catch (ElasticsearchParseException e) {
104-
assertThat(e.getMessage(), equalTo("[arr] property isn't a string or int, but of type [java.util.Arrays$ArrayList]"));
105-
}
102+
ElasticsearchParseException caught = assertThrows(
103+
ElasticsearchParseException.class,
104+
() -> ConfigurationUtils.readStringOrIntProperty(null, null, config, "arr", null)
105+
);
106+
assertThat(caught.getMessage(), equalTo("[arr] property isn't a string or int, but of type [java.util.Arrays$ArrayList]"));
106107
}
107108

108109
public void testReadMediaProperty() {

0 commit comments

Comments
 (0)