Skip to content

Commit

Permalink
Rename assertThrows() to expectThrows(), to be consistent wth Expecte…
Browse files Browse the repository at this point in the history
…dException.
  • Loading branch information
kcooney committed Dec 14, 2016
1 parent e90b5cd commit 2fb2db1
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/junit/Assert.java
Original file line number Diff line number Diff line change
Expand Up @@ -967,7 +967,7 @@ public static <T> void assertThat(String reason, T actual,
}

/**
* Asserts that {@code runnable} throws an exception of type {@code expectedThrowable} when
* Checks 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
* {@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
Expand All @@ -978,7 +978,7 @@ public static <T> void assertThat(String reason, T actual,
* @return the exception thrown by {@code runnable}
* @since 4.13
*/
public static <T extends Throwable> T assertThrows(Class<T> expectedThrowable, ThrowingRunnable runnable) {
public static <T extends Throwable> T expectThrows(Class<T> expectedThrowable, ThrowingRunnable runnable) {
try {
runnable.run();
} catch (Throwable actualThrown) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/junit/function/ThrowingRunnable.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.junit.function;

/**
* This interface facilitates the use of assertThrows from Java 8. It allows method references
* to void methods (that declare checked exceptions) to be passed directly into assertThrows
* This interface facilitates the use of {@code expectThrows} from Java 8. It allows method references
* to void methods (that declare checked exceptions) to be passed directly into {@code expectThrows}.
* without wrapping. It is not meant to be implemented directly.
*
* @since 4.13
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/junit/rules/ErrorCollector.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.junit.rules;

import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.expectThrows;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -117,7 +117,7 @@ public <T> T checkSucceeds(Callable<T> callable) {
*/
public void checkThrows(Class<? extends Throwable> expectedThrowable, ThrowingRunnable runnable) {
try {
assertThrows(expectedThrowable, runnable);
expectThrows(expectedThrowable, runnable);
} catch (AssertionError e) {
addError(e);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/junit/rules/ExpectedException.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
/**
* The {@code ExpectedException} rule allows you to verify that your code
* throws a specific exception. Note that, starting with Java 8,
* {@link org.junit.Assert#assertThrows(java.lang.Class, org.junit.function.ThrowingRunnable)
* Assert.assertThrows}
* {@link org.junit.Assert#expectThrows(java.lang.Class, org.junit.function.ThrowingRunnable)
* Assert.expectThrows}
* is often a better choice since it allows you to express exactly where you
* expect the exception to be thrown.
*
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 assertThrowsRequiresAnExceptionToBeThrown() {
assertThrows(Throwable.class, nonThrowingRunnable());
public void expectThrowsRequiresAnExceptionToBeThrown() {
expectThrows(Throwable.class, nonThrowingRunnable());
}

@Test
public void assertThrowsIncludesAnInformativeDefaultMessage() {
public void expectThrowsIncludesAnInformativeDefaultMessage() {
try {
assertThrows(Throwable.class, nonThrowingRunnable());
expectThrows(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 assertThrowsIncludesAnInformativeDefaultMessage() {
}

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

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

assertSame(npe, throwable);
}

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

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

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

try {
assertThrows(IOException.class, throwingRunnable(npe));
expectThrows(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 assertThrowsWrapsAndPropagatesUnexpectedExceptions() {
}

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

try {
assertThrows(IOException.class, throwingRunnable(npe));
expectThrows(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 assertThrowsSuppliesACoherentErrorMessageUponTypeMismatch() {
}

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

try {
assertThrows(NestedException.class, throwingRunnable(npe));
expectThrows(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 assertThrowsUsesCanonicalNameUponTypeMismatch() {
}

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

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

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

0 comments on commit 2fb2db1

Please sign in to comment.