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.
This commit is contained in:
Christoph Büscher 2016-10-18 09:26:40 +02:00
parent 8133c5e85d
commit abff485d68
3 changed files with 8 additions and 23 deletions

View File

@ -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! 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.Test @defaultMessage Just name your test method testFooBar
org.junit.rules.ExpectedException @ Use LuceneTestCase#expectThrows(Class<T>, LuceneTestCase.ThrowingRunnable) instead

View File

@ -26,8 +26,6 @@ import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.index.query.QueryParseContext;
import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.DocValueFormat;
import org.junit.Rule;
import org.junit.rules.ExpectedException;
import java.io.IOException; import java.io.IOException;
@ -49,16 +47,12 @@ public class ScoreSortBuilderTests extends AbstractSortTestCase<ScoreSortBuilder
return result; return result;
} }
@Rule
public ExpectedException exceptionRule = ExpectedException.none();
/** /**
* test passing null to {@link ScoreSortBuilder#order(SortOrder)} is illegal * test passing null to {@link ScoreSortBuilder#order(SortOrder)} is illegal
*/ */
public void testIllegalOrder() { public void testIllegalOrder() {
exceptionRule.expect(NullPointerException.class); Exception e = expectThrows(NullPointerException.class, () -> new ScoreSortBuilder().order(null));
exceptionRule.expectMessage("sort order cannot be null."); assertEquals("sort order cannot be null.", e.getMessage());
new ScoreSortBuilder().order(null);
} }
/** /**

View File

@ -20,16 +20,11 @@
package org.elasticsearch.search.sort; package org.elasticsearch.search.sort;
import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.ESTestCase;
import org.junit.Rule;
import org.junit.rules.ExpectedException;
import java.util.Locale; import java.util.Locale;
public class SortModeTests extends ESTestCase { public class SortModeTests extends ESTestCase {
@Rule
public ExpectedException exceptionRule = ExpectedException.none();
public void testSortMode() { public void testSortMode() {
// we rely on these ordinals in serialization, so changing them breaks bwc. // we rely on these ordinals in serialization, so changing them breaks bwc.
assertEquals(0, SortMode.MIN.ordinal()); assertEquals(0, SortMode.MIN.ordinal());
@ -50,16 +45,11 @@ public class SortModeTests extends ESTestCase {
} }
} }
public void testParseNull() { public void testParsingFromStringExceptions() {
exceptionRule.expect(NullPointerException.class); Exception e = expectThrows(NullPointerException.class, () -> SortMode.fromString(null));
exceptionRule.expectMessage("input string is null"); assertEquals("input string is null", e.getMessage());
SortMode.fromString(null);
}
public void testIllegalArgument() { e = expectThrows(IllegalArgumentException.class, () -> SortMode.fromString("xyz"));
exceptionRule.expect(IllegalArgumentException.class); assertEquals("Unknown SortMode [xyz]", e.getMessage());
exceptionRule.expectMessage("Unknown SortMode [xyz]");
SortMode.fromString("xyz");
} }
} }