From abff485d68c9af90d8d505527dd03e2e458e40e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20B=C3=BCscher?= Date: Tue, 18 Oct 2016 09:26:40 +0200 Subject: [PATCH] Use Lucenes expectThrows() when testing exceptions Cleaning up a few remaining occurences of using junits ExpectedException rule in favor of using LuceneTestCase#expectThrows() which is more concise and versatile. --- .../forbidden/es-test-signatures.txt | 1 + .../search/sort/ScoreSortBuilderTests.java | 10 ++-------- .../search/sort/SortModeTests.java | 20 +++++-------------- 3 files changed, 8 insertions(+), 23 deletions(-) diff --git a/buildSrc/src/main/resources/forbidden/es-test-signatures.txt b/buildSrc/src/main/resources/forbidden/es-test-signatures.txt index 08e591e1cfa..d365acbfca3 100644 --- a/buildSrc/src/main/resources/forbidden/es-test-signatures.txt +++ b/buildSrc/src/main/resources/forbidden/es-test-signatures.txt @@ -25,3 +25,4 @@ org.apache.lucene.util.LuceneTestCase$Nightly @ We don't run nightly tests at th com.carrotsearch.randomizedtesting.annotations.Nightly @ We don't run nightly tests at this point! org.junit.Test @defaultMessage Just name your test method testFooBar +org.junit.rules.ExpectedException @ Use LuceneTestCase#expectThrows(Class, LuceneTestCase.ThrowingRunnable) instead diff --git a/core/src/test/java/org/elasticsearch/search/sort/ScoreSortBuilderTests.java b/core/src/test/java/org/elasticsearch/search/sort/ScoreSortBuilderTests.java index 7ed0c900151..bffa1326630 100644 --- a/core/src/test/java/org/elasticsearch/search/sort/ScoreSortBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/sort/ScoreSortBuilderTests.java @@ -26,8 +26,6 @@ import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.search.DocValueFormat; -import org.junit.Rule; -import org.junit.rules.ExpectedException; import java.io.IOException; @@ -49,16 +47,12 @@ public class ScoreSortBuilderTests extends AbstractSortTestCase new ScoreSortBuilder().order(null)); + assertEquals("sort order cannot be null.", e.getMessage()); } /** diff --git a/core/src/test/java/org/elasticsearch/search/sort/SortModeTests.java b/core/src/test/java/org/elasticsearch/search/sort/SortModeTests.java index 29deb6dd76d..e0329347dba 100644 --- a/core/src/test/java/org/elasticsearch/search/sort/SortModeTests.java +++ b/core/src/test/java/org/elasticsearch/search/sort/SortModeTests.java @@ -20,16 +20,11 @@ package org.elasticsearch.search.sort; import org.elasticsearch.test.ESTestCase; -import org.junit.Rule; -import org.junit.rules.ExpectedException; import java.util.Locale; public class SortModeTests extends ESTestCase { - @Rule - public ExpectedException exceptionRule = ExpectedException.none(); - public void testSortMode() { // we rely on these ordinals in serialization, so changing them breaks bwc. assertEquals(0, SortMode.MIN.ordinal()); @@ -50,16 +45,11 @@ public class SortModeTests extends ESTestCase { } } - public void testParseNull() { - exceptionRule.expect(NullPointerException.class); - exceptionRule.expectMessage("input string is null"); - SortMode.fromString(null); - } + public void testParsingFromStringExceptions() { + Exception e = expectThrows(NullPointerException.class, () -> SortMode.fromString(null)); + assertEquals("input string is null", e.getMessage()); - public void testIllegalArgument() { - exceptionRule.expect(IllegalArgumentException.class); - exceptionRule.expectMessage("Unknown SortMode [xyz]"); - SortMode.fromString("xyz"); + e = expectThrows(IllegalArgumentException.class, () -> SortMode.fromString("xyz")); + assertEquals("Unknown SortMode [xyz]", e.getMessage()); } - }