Skip to content

Commit c63c5fa

Browse files
committed
Use expectThrows() instead of try-catch blocks for testing expected exceptions
1 parent 3d3dd71 commit c63c5fa

19 files changed

+305
-608
lines changed

core/src/test/java/org/elasticsearch/VersionTests.java

+8-25
Original file line numberDiff line numberDiff line change
@@ -97,40 +97,23 @@ public void testVersionFromString() {
9797
}
9898

9999
public void testTooLongVersionFromString() {
100-
try {
101-
Version.fromString("1.0.0.1.3");
102-
fail("Expected IllegalArgumentException");
103-
} catch (IllegalArgumentException e) {
104-
assertThat(e.getMessage(), containsString("needs to contain major, minor, and revision"));
105-
}
100+
Exception e = expectThrows(IllegalArgumentException.class, () -> Version.fromString("1.0.0.1.3"));
101+
assertThat(e.getMessage(), containsString("needs to contain major, minor, and revision"));
106102
}
107103

108104
public void testTooShortVersionFromString() {
109-
try {
110-
Version.fromString("1.0");
111-
fail("Expected IllegalArgumentException");
112-
} catch (IllegalArgumentException e) {
113-
assertThat(e.getMessage(), containsString("needs to contain major, minor, and revision"));
114-
}
115-
105+
Exception e = expectThrows(IllegalArgumentException.class, () -> Version.fromString("1.0"));
106+
assertThat(e.getMessage(), containsString("needs to contain major, minor, and revision"));
116107
}
117108

118109
public void testWrongVersionFromString() {
119-
try {
120-
Version.fromString("WRONG.VERSION");
121-
fail("Expected IllegalArgumentException");
122-
} catch (IllegalArgumentException e) {
123-
assertThat(e.getMessage(), containsString("needs to contain major, minor, and revision"));
124-
}
110+
Exception e = expectThrows(IllegalArgumentException.class, () -> Version.fromString("WRONG.VERSION"));
111+
assertThat(e.getMessage(), containsString("needs to contain major, minor, and revision"));
125112
}
126113

127114
public void testVersionNoPresentInSettings() {
128-
try {
129-
Version.indexCreated(Settings.builder().build());
130-
fail("Expected IllegalArgumentException");
131-
} catch (IllegalStateException e) {
132-
assertThat(e.getMessage(), containsString("[index.version.created] is not present"));
133-
}
115+
Exception e = expectThrows(IllegalStateException.class, () -> Version.indexCreated(Settings.builder().build()));
116+
assertThat(e.getMessage(), containsString("[index.version.created] is not present"));
134117
}
135118

136119
public void testIndexCreatedVersion() {

core/src/test/java/org/elasticsearch/action/support/single/instance/TransportInstanceSingleOperationActionTests.java

+2
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ public static void startThreadPool() {
137137
THREAD_POOL = new TestThreadPool(TransportInstanceSingleOperationActionTests.class.getSimpleName());
138138
}
139139

140+
@Override
140141
@Before
141142
public void setUp() throws Exception {
142143
super.setUp();
@@ -156,6 +157,7 @@ public void setUp() throws Exception {
156157
);
157158
}
158159

160+
@Override
159161
@After
160162
public void tearDown() throws Exception {
161163
super.tearDown();

core/src/test/java/org/elasticsearch/action/update/UpdateRequestTests.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,8 @@ public void testUpdateRequestWithTTL() throws Exception {
226226
// Related to issue #15822
227227
public void testInvalidBodyThrowsParseException() throws Exception {
228228
UpdateRequest request = new UpdateRequest("test", "type", "1");
229-
try {
230-
request.fromXContent(new byte[] { (byte) '"' });
231-
fail("Should have thrown a ElasticsearchParseException");
232-
} catch (ElasticsearchParseException e) {
233-
assertThat(e.getMessage(), equalTo("Failed to derive xcontent"));
234-
}
229+
Exception e = expectThrows(ElasticsearchParseException.class, () -> request.fromXContent(new byte[] { (byte) '"' }));
230+
assertThat(e.getMessage(), equalTo("Failed to derive xcontent"));
235231
}
236232

237233
// Related to issue 15338

core/src/test/java/org/elasticsearch/cluster/metadata/DateMathExpressionResolverTests.java

+16-28
Original file line numberDiff line numberDiff line change
@@ -177,43 +177,31 @@ public void testExpression_CustomTimeZoneInIndexName() throws Exception {
177177
}
178178

179179
public void testExpressionInvalidUnescaped() throws Exception {
180-
try {
181-
expressionResolver.resolve(context, Arrays.asList("<.mar}vel-{now/d}>"));
182-
fail("Expected ElasticsearchParseException");
183-
} catch (ElasticsearchParseException e) {
184-
assertThat(e.getMessage(), containsString("invalid dynamic name expression"));
185-
assertThat(e.getMessage(), containsString("invalid character at position ["));
186-
}
180+
Exception e = expectThrows(ElasticsearchParseException.class,
181+
() -> expressionResolver.resolve(context, Arrays.asList("<.mar}vel-{now/d}>")));
182+
assertThat(e.getMessage(), containsString("invalid dynamic name expression"));
183+
assertThat(e.getMessage(), containsString("invalid character at position ["));
187184
}
188185

189186
public void testExpressionInvalidDateMathFormat() throws Exception {
190-
try {
191-
expressionResolver.resolve(context, Arrays.asList("<.marvel-{now/d{}>"));
192-
fail("Expected ElasticsearchParseException");
193-
} catch (ElasticsearchParseException e) {
194-
assertThat(e.getMessage(), containsString("invalid dynamic name expression"));
195-
assertThat(e.getMessage(), containsString("date math placeholder is open ended"));
196-
}
187+
Exception e = expectThrows(ElasticsearchParseException.class,
188+
() -> expressionResolver.resolve(context, Arrays.asList("<.marvel-{now/d{}>")));
189+
assertThat(e.getMessage(), containsString("invalid dynamic name expression"));
190+
assertThat(e.getMessage(), containsString("date math placeholder is open ended"));
197191
}
198192

199193
public void testExpressionInvalidEmptyDateMathFormat() throws Exception {
200-
try {
201-
expressionResolver.resolve(context, Arrays.asList("<.marvel-{now/d{}}>"));
202-
fail("Expected ElasticsearchParseException");
203-
} catch (ElasticsearchParseException e) {
204-
assertThat(e.getMessage(), containsString("invalid dynamic name expression"));
205-
assertThat(e.getMessage(), containsString("missing date format"));
206-
}
194+
Exception e = expectThrows(ElasticsearchParseException.class,
195+
() -> expressionResolver.resolve(context, Arrays.asList("<.marvel-{now/d{}}>")));
196+
assertThat(e.getMessage(), containsString("invalid dynamic name expression"));
197+
assertThat(e.getMessage(), containsString("missing date format"));
207198
}
208199

209200
public void testExpressionInvalidOpenEnded() throws Exception {
210-
try {
211-
expressionResolver.resolve(context, Arrays.asList("<.marvel-{now/d>"));
212-
fail("Expected ElasticsearchParseException");
213-
} catch (ElasticsearchParseException e) {
214-
assertThat(e.getMessage(), containsString("invalid dynamic name expression"));
215-
assertThat(e.getMessage(), containsString("date math placeholder is open ended"));
216-
}
201+
Exception e = expectThrows(ElasticsearchParseException.class,
202+
() -> expressionResolver.resolve(context, Arrays.asList("<.marvel-{now/d>")));
203+
assertThat(e.getMessage(), containsString("invalid dynamic name expression"));
204+
assertThat(e.getMessage(), containsString("date math placeholder is open ended"));
217205
}
218206

219207
}

core/src/test/java/org/elasticsearch/common/TableTests.java

+15-48
Original file line numberDiff line numberDiff line change
@@ -28,71 +28,43 @@
2828
import static org.hamcrest.Matchers.is;
2929

3030
public class TableTests extends ESTestCase {
31+
3132
public void testFailOnStartRowWithoutHeader() {
3233
Table table = new Table();
33-
try {
34-
table.startRow();
35-
fail("Expected IllegalStateException");
36-
} catch (IllegalStateException e) {
37-
assertThat(e.getMessage(), is("no headers added..."));
38-
}
34+
Exception e = expectThrows(IllegalStateException.class, () -> table.startRow());
35+
assertThat(e.getMessage(), is("no headers added..."));
3936
}
4037

4138
public void testFailOnEndHeadersWithoutStart() {
4239
Table table = new Table();
43-
try {
44-
table.endHeaders();
45-
fail("Expected IllegalStateException");
46-
} catch (IllegalStateException e) {
47-
assertThat(e.getMessage(), is("no headers added..."));
48-
}
49-
40+
Exception e = expectThrows(IllegalStateException.class, () -> table.endHeaders());
41+
assertThat(e.getMessage(), is("no headers added..."));
5042
}
5143

5244
public void testFailOnAddCellWithoutHeader() {
5345
Table table = new Table();
54-
try {
55-
table.addCell("error");
56-
fail("Expected IllegalStateException");
57-
} catch (IllegalStateException e) {
58-
assertThat(e.getMessage(), is("no block started..."));
59-
}
60-
46+
Exception e = expectThrows(IllegalStateException.class, () -> table.addCell("error"));
47+
assertThat(e.getMessage(), is("no block started..."));
6148
}
6249

6350
public void testFailOnAddCellWithoutRow() {
6451
Table table = this.getTableWithHeaders();
65-
try {
66-
table.addCell("error");
67-
fail("Expected IllegalStateException");
68-
} catch (IllegalStateException e) {
69-
assertThat(e.getMessage(), is("no block started..."));
70-
}
71-
52+
Exception e = expectThrows(IllegalStateException.class, () -> table.addCell("error"));
53+
assertThat(e.getMessage(), is("no block started..."));
7254
}
7355

7456
public void testFailOnEndRowWithoutStart() {
7557
Table table = this.getTableWithHeaders();
76-
try {
77-
table.endRow();
78-
fail("Expected IllegalStateException");
79-
} catch (IllegalStateException e) {
80-
assertThat(e.getMessage(), is("no row started..."));
81-
}
82-
58+
Exception e = expectThrows(IllegalStateException.class, () -> table.endRow());
59+
assertThat(e.getMessage(), is("no row started..."));
8360
}
8461

8562
public void testFailOnLessCellsThanDeclared() {
8663
Table table = this.getTableWithHeaders();
8764
table.startRow();
8865
table.addCell("foo");
89-
try {
90-
table.endRow(true);
91-
fail("Expected IllegalStateException");
92-
} catch (IllegalStateException e) {
93-
assertThat(e.getMessage(), is("mismatch on number of cells 1 in a row compared to header 2"));
94-
}
95-
66+
Exception e = expectThrows(IllegalStateException.class, () -> table.endRow());
67+
assertThat(e.getMessage(), is("mismatch on number of cells 1 in a row compared to header 2"));
9668
}
9769

9870
public void testOnLessCellsThanDeclaredUnchecked() {
@@ -107,13 +79,8 @@ public void testFailOnMoreCellsThanDeclared() {
10779
table.startRow();
10880
table.addCell("foo");
10981
table.addCell("bar");
110-
try {
111-
table.addCell("foobar");
112-
fail("Expected IllegalStateException");
113-
} catch (IllegalStateException e) {
114-
assertThat(e.getMessage(), is("can't add more cells to a row than the header"));
115-
}
116-
82+
Exception e = expectThrows(IllegalStateException.class, () -> table.addCell("foobar"));
83+
assertThat(e.getMessage(), is("can't add more cells to a row than the header"));
11784
}
11885

11986
public void testSimple() {

core/src/test/java/org/elasticsearch/common/geo/ShapeBuilderTests.java

+16-32
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@
1919

2020
package org.elasticsearch.common.geo;
2121

22-
import org.locationtech.spatial4j.exception.InvalidShapeException;
23-
import org.locationtech.spatial4j.shape.Circle;
24-
import org.locationtech.spatial4j.shape.Point;
25-
import org.locationtech.spatial4j.shape.Rectangle;
26-
import org.locationtech.spatial4j.shape.Shape;
27-
import org.locationtech.spatial4j.shape.impl.PointImpl;
2822
import com.vividsolutions.jts.geom.Coordinate;
2923
import com.vividsolutions.jts.geom.LineString;
3024
import com.vividsolutions.jts.geom.Polygon;
@@ -35,6 +29,12 @@
3529
import org.elasticsearch.common.geo.builders.ShapeBuilder;
3630
import org.elasticsearch.common.geo.builders.ShapeBuilders;
3731
import org.elasticsearch.test.ESTestCase;
32+
import org.locationtech.spatial4j.exception.InvalidShapeException;
33+
import org.locationtech.spatial4j.shape.Circle;
34+
import org.locationtech.spatial4j.shape.Point;
35+
import org.locationtech.spatial4j.shape.Rectangle;
36+
import org.locationtech.spatial4j.shape.Shape;
37+
import org.locationtech.spatial4j.shape.impl.PointImpl;
3838

3939
import static org.elasticsearch.test.hamcrest.ElasticsearchGeoAssertions.assertMultiLineString;
4040
import static org.elasticsearch.test.hamcrest.ElasticsearchGeoAssertions.assertMultiPolygon;
@@ -183,17 +183,13 @@ public void testMultiLineString() {
183183
}
184184

185185
public void testPolygonSelfIntersection() {
186-
try {
187-
ShapeBuilders.newPolygon(new CoordinatesBuilder()
186+
PolygonBuilder newPolygon = ShapeBuilders.newPolygon(new CoordinatesBuilder()
188187
.coordinate(-40.0, 50.0)
189188
.coordinate(40.0, 50.0)
190189
.coordinate(-40.0, -50.0)
191-
.coordinate(40.0, -50.0).close())
192-
.build();
193-
fail("Expected InvalidShapeException");
194-
} catch (InvalidShapeException e) {
195-
assertThat(e.getMessage(), containsString("Self-intersection at or near point (0.0"));
196-
}
190+
.coordinate(40.0, -50.0).close());
191+
Exception e = expectThrows(InvalidShapeException.class, () -> newPolygon.build());
192+
assertThat(e.getMessage(), containsString("Self-intersection at or near point (0.0"));
197193
}
198194

199195
public void testGeoCircle() {
@@ -550,12 +546,8 @@ public void testShapeWithInvalidTangentialHole() {
550546
.coordinate(179, -10)
551547
.coordinate(164, 0)
552548
));
553-
try {
554-
builder.close().build();
555-
fail("Expected InvalidShapeException");
556-
} catch (InvalidShapeException e) {
557-
assertThat(e.getMessage(), containsString("interior cannot share more than one point with the exterior"));
558-
}
549+
Exception e = expectThrows(InvalidShapeException.class, () -> builder.close().build());
550+
assertThat(e.getMessage(), containsString("interior cannot share more than one point with the exterior"));
559551
}
560552

561553
public void testBoundaryShapeWithTangentialHole() {
@@ -602,12 +594,8 @@ public void testBoundaryShapeWithInvalidTangentialHole() {
602594
.coordinate(176, -10)
603595
.coordinate(-177, 10)
604596
));
605-
try {
606-
builder.close().build();
607-
fail("Expected InvalidShapeException");
608-
} catch (InvalidShapeException e) {
609-
assertThat(e.getMessage(), containsString("interior cannot share more than one point with the exterior"));
610-
}
597+
Exception e = expectThrows(InvalidShapeException.class, () -> builder.close().build());
598+
assertThat(e.getMessage(), containsString("interior cannot share more than one point with the exterior"));
611599
}
612600

613601
/**
@@ -659,11 +647,7 @@ public void testInvalidShapeWithConsecutiveDuplicatePoints() {
659647
.coordinate(-176, 4)
660648
.coordinate(180, 0)
661649
);
662-
try {
663-
builder.close().build();
664-
fail("Expected InvalidShapeException");
665-
} catch (InvalidShapeException e) {
666-
assertThat(e.getMessage(), containsString("duplicate consecutive coordinates at: ("));
667-
}
650+
Exception e = expectThrows(InvalidShapeException.class, () -> builder.close().build());
651+
assertThat(e.getMessage(), containsString("duplicate consecutive coordinates at: ("));
668652
}
669653
}

0 commit comments

Comments
 (0)