Skip to content

Commit

Permalink
Delete expectThrows(). Change assertThrows() to return the caught exc…
Browse files Browse the repository at this point in the history
…eption.

JUnit 5 decided to make the same change in the Assertions class, so this
change makes the JUnit4 API consistent.
  • Loading branch information
kcooney committed Nov 30, 2016
1 parent 638e824 commit 2fd6312
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 39 deletions.
17 changes: 1 addition & 16 deletions src/main/java/org/junit/Assert.java
Original file line number Diff line number Diff line change
Expand Up @@ -966,21 +966,6 @@ public static <T> void assertThat(String reason, T actual,
MatcherAssert.assertThat(reason, actual, matcher);
}

/**
* Asserts that {@code runnable} throws an exception of type {@code expectedThrowable} when
* executed. If it does not throw an exception, an {@link AssertionError} is thrown. If it
* throws the wrong type of exception, an {@code AssertionError} is thrown describing the
* mismatch; the exception that was actually thrown can be obtained by calling {@link
* AssertionError#getCause}.
*
* @param expectedThrowable the expected type of the exception
* @param runnable a function that is expected to throw an exception when executed
* @since 4.13
*/
public static void assertThrows(Class<? extends Throwable> expectedThrowable, ThrowingRunnable runnable) {
expectThrows(expectedThrowable, runnable);
}

/**
* Asserts that {@code runnable} throws an exception of type {@code expectedThrowable} when
* executed. If it does, the exception object is returned. If it does not throw an exception, an
Expand All @@ -993,7 +978,7 @@ public static void assertThrows(Class<? extends Throwable> expectedThrowable, Th
* @return the exception thrown by {@code runnable}
* @since 4.13
*/
public static <T extends Throwable> T expectThrows(Class<T> expectedThrowable, ThrowingRunnable runnable) {
public static <T extends Throwable> T assertThrows(Class<T> expectedThrowable, ThrowingRunnable runnable) {
try {
runnable.run();
} catch (Throwable actualThrown) {
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/org/junit/rules/ExpectedException.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@
* {@link org.junit.Assert#assertThrows(java.lang.Class, org.junit.function.ThrowingRunnable)
* Assert.assertThrows}
* is often a better choice since it allows you to express exactly where you
* expect the exception to be thrown. Use
* {@link org.junit.Assert#expectThrows(java.lang.Class,
* org.junit.function.ThrowingRunnable) expectThrows}
* if you need to assert something about the thrown exception.
* expect the exception to be thrown.
*
* <h3>Usage</h3>
*
Expand Down
38 changes: 19 additions & 19 deletions src/test/java/org/junit/tests/assertion/AssertionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.expectThrows;
import static org.junit.Assert.fail;

import java.io.IOException;
Expand Down Expand Up @@ -797,14 +797,14 @@ public void assertNotEqualsIgnoresFloatDeltaOnNaN() {
}

@Test(expected = AssertionError.class)
public void expectThrowsRequiresAnExceptionToBeThrown() {
expectThrows(Throwable.class, nonThrowingRunnable());
public void assertThrowsRequiresAnExceptionToBeThrown() {
assertThrows(Throwable.class, nonThrowingRunnable());
}

@Test
public void expectThrowsIncludesAnInformativeDefaultMessage() {
public void assertThrowsIncludesAnInformativeDefaultMessage() {
try {
expectThrows(Throwable.class, nonThrowingRunnable());
assertThrows(Throwable.class, nonThrowingRunnable());
} catch (AssertionError ex) {
assertEquals("expected java.lang.Throwable to be thrown, but nothing was thrown", ex.getMessage());
return;
Expand All @@ -813,27 +813,27 @@ public void expectThrowsIncludesAnInformativeDefaultMessage() {
}

@Test
public void expectThrowsReturnsTheSameObjectThrown() {
public void assertThrowsReturnsTheSameObjectThrown() {
NullPointerException npe = new NullPointerException();

Throwable throwable = expectThrows(Throwable.class, throwingRunnable(npe));
Throwable throwable = assertThrows(Throwable.class, throwingRunnable(npe));

assertSame(npe, throwable);
}

@Test(expected = AssertionError.class)
public void expectThrowsDetectsTypeMismatchesViaExplicitTypeHint() {
public void assertThrowsDetectsTypeMismatchesViaExplicitTypeHint() {
NullPointerException npe = new NullPointerException();

expectThrows(IOException.class, throwingRunnable(npe));
assertThrows(IOException.class, throwingRunnable(npe));
}

@Test
public void expectThrowsWrapsAndPropagatesUnexpectedExceptions() {
public void assertThrowsWrapsAndPropagatesUnexpectedExceptions() {
NullPointerException npe = new NullPointerException("inner-message");

try {
expectThrows(IOException.class, throwingRunnable(npe));
assertThrows(IOException.class, throwingRunnable(npe));
} catch (AssertionError ex) {
assertSame(npe, ex.getCause());
assertEquals("inner-message", ex.getCause().getMessage());
Expand All @@ -843,11 +843,11 @@ public void expectThrowsWrapsAndPropagatesUnexpectedExceptions() {
}

@Test
public void expectThrowsSuppliesACoherentErrorMessageUponTypeMismatch() {
public void assertThrowsSuppliesACoherentErrorMessageUponTypeMismatch() {
NullPointerException npe = new NullPointerException();

try {
expectThrows(IOException.class, throwingRunnable(npe));
assertThrows(IOException.class, throwingRunnable(npe));
} catch (AssertionError error) {
assertEquals("unexpected exception type thrown; expected:<java.io.IOException> but was:<java.lang.NullPointerException>",
error.getMessage());
Expand All @@ -858,11 +858,11 @@ public void expectThrowsSuppliesACoherentErrorMessageUponTypeMismatch() {
}

@Test
public void expectThrowsUsesCanonicalNameUponTypeMismatch() {
public void assertThrowsUsesCanonicalNameUponTypeMismatch() {
NullPointerException npe = new NullPointerException();

try {
expectThrows(NestedException.class, throwingRunnable(npe));
assertThrows(NestedException.class, throwingRunnable(npe));
} catch (AssertionError error) {
assertEquals(
"unexpected exception type thrown; expected:<org.junit.tests.assertion.AssertionTest.NestedException>"
Expand All @@ -875,12 +875,12 @@ public void expectThrowsUsesCanonicalNameUponTypeMismatch() {
}

@Test
public void expectThrowsUsesNameUponTypeMismatchWithAnonymousClass() {
public void assertThrowsUsesNameUponTypeMismatchWithAnonymousClass() {
NullPointerException npe = new NullPointerException() {
};

try {
expectThrows(IOException.class, throwingRunnable(npe));
assertThrows(IOException.class, throwingRunnable(npe));
} catch (AssertionError error) {
assertEquals(
"unexpected exception type thrown; expected:<java.io.IOException>"
Expand All @@ -893,9 +893,9 @@ public void expectThrowsUsesNameUponTypeMismatchWithAnonymousClass() {
}

@Test
public void expectThrowsUsesCanonicalNameWhenRequiredExceptionNotThrown() {
public void assertThrowsUsesCanonicalNameWhenRequiredExceptionNotThrown() {
try {
expectThrows(NestedException.class, nonThrowingRunnable());
assertThrows(NestedException.class, nonThrowingRunnable());
} catch (AssertionError error) {
assertEquals(
"expected org.junit.tests.assertion.AssertionTest.NestedException to be thrown,"
Expand Down

0 comments on commit 2fd6312

Please sign in to comment.