diff --git a/assertthrows/README.txt b/assertthrows/README.txt
index 1194ed8..df1f779 100644
--- a/assertthrows/README.txt
+++ b/assertthrows/README.txt
@@ -3,3 +3,6 @@ Welcome to JUnit Contrib AssertThrows
=====================================
Testing for Expected Exceptions
+
+For details, see:
+src/site/resources/index.html
diff --git a/assertthrows/src/main/java/org/junit/contrib/assertthrows/AssertThrows.java b/assertthrows/src/main/java/org/junit/contrib/assertthrows/AssertThrows.java
index d9c9008..cf7ed08 100644
--- a/assertthrows/src/main/java/org/junit/contrib/assertthrows/AssertThrows.java
+++ b/assertthrows/src/main/java/org/junit/contrib/assertthrows/AssertThrows.java
@@ -16,11 +16,7 @@
*/
package org.junit.contrib.assertthrows;
-import java.lang.reflect.InvocationHandler;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
import org.junit.contrib.assertthrows.proxy.ProxyFactory;
-import org.junit.contrib.assertthrows.proxy.ReflectionUtils;
import org.junit.contrib.assertthrows.verify.ExceptionVerifier;
import org.junit.contrib.assertthrows.verify.ResultVerifier;
@@ -32,46 +28,55 @@
public abstract class AssertThrows {
private final ResultVerifier verifier;
+ private Throwable lastThrown;
/**
- * Create a new AssertThrows object, and call the test method to verify the
- * expected exception is thrown.
- *
- * @param expectedExceptionClass the expected exception class
+ * Create a new AssertThrows
object, and call the test method
+ * to verify an exception or error is thrown.
+ *
+ * For the test to pass, the test()
method must throw any kind
+ * of Exception
or Error
(
+ * AssertionError
, StackOverflowError
, and so on).
*/
- public AssertThrows(Class extends Exception> expectedExceptionClass) {
- this(new ExceptionVerifier(expectedExceptionClass, null));
+ public AssertThrows() {
+ this(new ExceptionVerifier());
}
/**
- * Create a new AssertThrows object, and call the test method to verify the
- * expected exception is thrown.
+ * Create a new AssertThrows
object, and call the test method
+ * to verify the expected exception is thrown.
+ *
+ * For the test to pass, the class of the thrown exception must match the
+ * expected exception, or it must be a subclass of the expected exception.
*
- * @param expectedException the expected exception
+ * @param expectedExceptionClass the expected exception class (must not be
+ * null)
*/
- public AssertThrows(Exception expectedException) {
- this(expectedException == null ?
- new ExceptionVerifier(null, null) :
- new ExceptionVerifier(
- expectedException.getClass(),
- expectedException.getMessage()));
+ public AssertThrows(Class extends Exception> expectedExceptionClass) {
+ this(new ExceptionVerifier(expectedExceptionClass));
}
/**
- * Create a new AssertThrows object, and call the test method to verify an
- * exception or error is thrown. That means for a successful result, the
- * test() method must throw any kind of Exception or Error (AssertionError,
- * StackOverflowError, and so on).
+ * Create a new AssertThrows
object, and call the test method
+ * to verify the expected exception is thrown.
+ *
+ * For the test to pass, the exception class must match exactly, and the message
+ * must match exactly. If the message of the expected exception is null, then
+ * the message of the thrown exception must also be null.
+ *
+ * @param expectedException the expected exception (must not be null)
*/
- public AssertThrows() {
- this(new ExceptionVerifier(null, null));
+ public AssertThrows(Exception expectedException) {
+ this(new ExceptionVerifier(expectedException));
}
/**
- * Create a new AssertThrows object, and call the test method as many times
- * as the result verifier requests. It is usually not required to use this
- * constructor, except to extend this facility to do something new, such as
- * repeat a test until it works.
+ * Create a new AssertThrows
object, and call the test method
+ * as many times as the result verifier requests.
+ *
+ * This constructor is usually not required within unit tests, except to
+ * extend this facility to do something new, such as repeat a test until it
+ * works.
*/
protected AssertThrows(ResultVerifier verifier) {
this.verifier = verifier;
@@ -84,61 +89,69 @@ protected AssertThrows(ResultVerifier verifier) {
*/
protected void verify() {
while (true) {
+ lastThrown = null;
try {
test();
// can't call verifier.verify here, because it can
// throw an exception itself (which must not be caught)
} catch (Throwable e) {
- if (verifier.verify(null, e, null)) {
- continue;
- }
- break;
+ lastThrown = e;
}
- if (verifier.verify(null, null, null)) {
- continue;
+ if (!verifier.verify(null, lastThrown, null)) {
+ return;
}
- break;
}
}
/**
- * Verify that the next method call on the object throws an exception.
+ * Verify that the next method call on the returned object throws an exception.
+ *
+ * For the test to pass, the method must throw any kind
+ * of Exception
or Error
(
+ * AssertionError
, StackOverflowError
, and so on).
*
* @param the class of the object
- * @param obj the object to wrap
+ * @param obj the object to wrap (must not be null)
* @return a proxy for the object
*/
public static T assertThrows(T obj) {
- return createVerifyingProxy(new ExceptionVerifier(null, null), obj);
+ return ExceptionVerifier.createVerifyingProxy(
+ new ExceptionVerifier(), obj);
}
/**
- * Verify that the next method call on the object throws the expected
+ * Verify that the next method call on the returned object throws the expected
* exception.
+ *
+ * For the test to pass, the class of the thrown exception must match the
+ * expected exception, or it must be a subclass of the expected exception.
*
* @param the class of the object
- * @param expectedExceptionClass the expected exception class to be thrown
- * @param obj the object to wrap
+ * @param expectedExceptionClass the expected exception class (must not be null)
+ * @param obj the object to wrap (must not be null)
* @return a proxy for the object
*/
public static T assertThrows(Class extends Exception> expectedExceptionClass, T obj) {
- return createVerifyingProxy(new ExceptionVerifier(expectedExceptionClass, null), obj);
+ return ExceptionVerifier.createVerifyingProxy(
+ new ExceptionVerifier(expectedExceptionClass), obj);
}
/**
* Verify that the next method call on the object throws the expected
* exception.
+ *
+ * For the test to pass, the exception class must match exactly, and the message
+ * must match exactly. If the message of the expected exception is null, then
+ * the message of the thrown exception must also be null.
*
* @param the class of the object
- * @param expectedException the expected exception
- * @param obj the object to wrap
+ * @param expectedException the expected exception (must not be null)
+ * @param obj the object to wrap (must not be null)
* @return a proxy for the object
*/
public static T assertThrows(Exception expectedException, T obj) {
- return createVerifyingProxy(expectedException == null ?
- new ExceptionVerifier(null, null) :
- new ExceptionVerifier(expectedException.getClass(), expectedException.getMessage()),
- obj);
+ return ExceptionVerifier.createVerifyingProxy(
+ new ExceptionVerifier(expectedException), obj);
}
/**
@@ -153,48 +166,20 @@ public static void useClassProxy(Class> c) {
}
/**
- * Create a verifying proxy for the given object. It is usually not required
- * to use this method in a test directly, except to extend this facility to
- * do something new, such as repeat a test until it works.
+ * The test method that is called.
*
- * @param the class of the object
- * @param verifier the result verifier to call after each method call
- * @param obj the object to wrap
- * @return a proxy for the object
- * @throws IllegalArgumentException if it was not possible to create a proxy
- * for the passed object
+ * @throws Exception the expected exception
*/
- protected static T createVerifyingProxy(final ResultVerifier verifier, final T obj) {
- InvocationHandler handler = new InvocationHandler() {
- private Exception called = new Exception("No method was called on " + obj);
- public void finalize() {
- if (called != null) {
- called.printStackTrace(System.err);
- }
- }
- public Object invoke(Object proxy, Method method, Object[] args) throws Exception {
- try {
- called = null;
- Object ret;
- do {
- ret = method.invoke(obj, args);
- } while (verifier.verify(ret, null, method, args));
- return ret;
- } catch (InvocationTargetException e) {
- verifier.verify(null, e.getTargetException(), method, args);
- return ReflectionUtils.getDefaultValue(method.getReturnType());
- }
- }
- };
- ProxyFactory factory = ProxyFactory.getFactory(obj.getClass());
- return factory.createProxy(obj, handler);
- }
+ public abstract void test() throws Exception;
/**
- * The test method that is called.
+ * Get the last exception or error (if any) that was thrown by the method
+ * test()
.
*
- * @throws Exception the expected exception
+ * @return the last thrown exception or error, or null
*/
- public abstract void test() throws Exception;
+ public Throwable getLastThrown() {
+ return lastThrown;
+ }
}
diff --git a/assertthrows/src/main/java/org/junit/contrib/assertthrows/proxy/CglibProxyFactory.java b/assertthrows/src/main/java/org/junit/contrib/assertthrows/proxy/CglibProxyFactory.java
index e94538e..bd7a498 100644
--- a/assertthrows/src/main/java/org/junit/contrib/assertthrows/proxy/CglibProxyFactory.java
+++ b/assertthrows/src/main/java/org/junit/contrib/assertthrows/proxy/CglibProxyFactory.java
@@ -147,7 +147,7 @@ public String getClassName(String prefix,
}
/**
- * An tool to create new objects, if possible without calling any constructors.
+ * A tool to create new objects, if possible without calling any constructors.
*/
interface ObjectCreator {
Object newInstance(Class> c) throws Exception;
diff --git a/assertthrows/src/main/java/org/junit/contrib/assertthrows/proxy/Compiler.java b/assertthrows/src/main/java/org/junit/contrib/assertthrows/proxy/Compiler.java
index 49ad200..ff3f95a 100644
--- a/assertthrows/src/main/java/org/junit/contrib/assertthrows/proxy/Compiler.java
+++ b/assertthrows/src/main/java/org/junit/contrib/assertthrows/proxy/Compiler.java
@@ -78,14 +78,12 @@ public class Compiler {
/**
* Set the source code for the specified class.
- * This will reset all compiled classes.
*
* @param className the class name
* @param source the source code
*/
public void setSource(String className, String source) {
sources.put(className, source);
- compiled.clear();
}
/**
diff --git a/assertthrows/src/main/java/org/junit/contrib/assertthrows/verify/ExceptionVerifier.java b/assertthrows/src/main/java/org/junit/contrib/assertthrows/verify/ExceptionVerifier.java
index 8b815d1..c9ed0e2 100644
--- a/assertthrows/src/main/java/org/junit/contrib/assertthrows/verify/ExceptionVerifier.java
+++ b/assertthrows/src/main/java/org/junit/contrib/assertthrows/verify/ExceptionVerifier.java
@@ -16,7 +16,11 @@
*/
package org.junit.contrib.assertthrows.verify;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
+import org.junit.contrib.assertthrows.proxy.ProxyFactory;
+import org.junit.contrib.assertthrows.proxy.ReflectionUtils;
/**
* A result verifier that checks against an expected exception class.
@@ -26,21 +30,90 @@
public class ExceptionVerifier implements ResultVerifier {
private final Class extends Exception> expectedExceptionClass;
- private final String expectedMessage;
+ private final Exception expectedException;
/**
- * Create a new verifier that checks against the given class (or any of it's
- * subclasses), or doesn't check the exception class at all (if the passed
- * class is null).
+ * Create a new verifier that only checks if an exception or error was thrown.
+ */
+ public ExceptionVerifier() {
+ this.expectedExceptionClass = null;
+ this.expectedException = null;
+ }
+
+ /**
+ * Create a new verifier that checks against a given exception class, or any
+ * of it's subclasses.
*
- * @param expectedExceptionClass the expected exception base class, or null
- * @param expectedMessage the expected message, or null
+ * @param expectedExceptionClass the expected exception base class (may not
+ * be null)
*/
- public ExceptionVerifier(
- Class extends Exception> expectedExceptionClass,
- String expectedMessage) {
+ public ExceptionVerifier(Class extends Exception> expectedExceptionClass) {
+ if (expectedExceptionClass == null) {
+ throw new NullPointerException("The passed exception class is null");
+ }
this.expectedExceptionClass = expectedExceptionClass;
- this.expectedMessage = expectedMessage;
+ this.expectedException = null;
+ }
+
+ /**
+ * Create a new verifier that checks against a given exception class for an
+ * exact match, plus additionally checks the message.
+ *
+ * @param expectedException the expected exception (may not be null)
+ */
+ public ExceptionVerifier(Exception expectedException) {
+ if (expectedException == null) {
+ throw new NullPointerException("The passed exception is null");
+ }
+ this.expectedExceptionClass = expectedException.getClass();
+ if (expectedExceptionClass == null) {
+ // overcareful
+ throw new NullPointerException("The passed exception class is null");
+ }
+ this.expectedException = expectedException;
+ }
+
+ /**
+ * Create a verifying proxy for the given object. It is usually not required
+ * to use this method in a test directly, except to extend this facility to
+ * do something new, such as repeat a test until it works.
+ *
+ * @param the class of the object
+ * @param verifier the result verifier to call after each method call
+ * @param obj the object to wrap
+ * @return a proxy for the object
+ * @throws IllegalArgumentException if it was not possible to create a proxy
+ * for the passed object
+ */
+ public static T createVerifyingProxy(final ResultVerifier verifier, final T obj) {
+ if (obj == null) {
+ throw new NullPointerException("The passed object is null");
+ }
+ InvocationHandler handler = new InvocationHandler() {
+ private Exception called = new Exception("No method was called on " + obj);
+ public void finalize() {
+ if (called != null) {
+ called.printStackTrace(System.err);
+ }
+ }
+ public Object invoke(Object proxy, Method method, Object[] args) throws Exception {
+ called = null;
+ while (true) {
+ Object ret = null;
+ Throwable thrown = null;
+ try {
+ ret = method.invoke(obj, args);
+ } catch (InvocationTargetException e) {
+ thrown = e.getTargetException();
+ }
+ if (!verifier.verify(ret, thrown, method, args)) {
+ return ReflectionUtils.getDefaultValue(method.getReturnType());
+ }
+ }
+ }
+ };
+ ProxyFactory factory = ProxyFactory.getFactory(obj.getClass());
+ return factory.createProxy(obj, handler);
}
public boolean verify(Object returnValue, Throwable t, Method m, Object... args) {
@@ -51,25 +124,35 @@ public boolean verify(Object returnValue, Throwable t, Method m, Object... args)
return false;
} else if (expectedExceptionClass.isAssignableFrom(t.getClass())) {
// matching expected class
- if (expectedMessage == null) {
- // don't verify the message
+ if (expectedException == null) {
+ // don't verify the exception itself
return false;
}
- if (expectedMessage.equals(t.getMessage())) {
- return false;
+ // the exception class must match exactly
+ if (expectedException.getClass().equals(t.getClass())) {
+ String expectedMsg = expectedException.getMessage();
+ String msg = t.getMessage();
+ if (expectedMsg == null) {
+ if (msg == null) {
+ // both messages are null
+ return false;
+ }
+ } else if (expectedMsg.equals(msg)) {
+ // messages match
+ return false;
+ }
+ String message = "Expected exception message <" + expectedMsg +
+ ">, but got <" + msg + ">";
+ throw buildAssertionError(message, t);
}
- throw new AssertionError(
- "Expected message:\n" + expectedMessage + "\n" +
- "but was: " + t.getMessage());
}
}
- String expected;
+ String type;
if (expectedExceptionClass == null) {
- expected = "Expected an exception to be thrown,\n";
+ type = "";
} else {
- expected = "Expected an exception of type\n" +
- expectedExceptionClass.getSimpleName() +
- " to be thrown,\n";
+ type = " of type\n" +
+ expectedExceptionClass.getSimpleName();
}
String but;
if (m == null) {
@@ -89,11 +172,18 @@ public boolean verify(Object returnValue, Throwable t, Method m, Object... args)
t.getClass().getSimpleName() +
" (see in the 'Caused by' for the exception that was thrown)";
}
- AssertionError ae = new AssertionError(expected + but + result);
- if (t != null) {
- ae.initCause(t);
+ String message =
+ "Expected an exception" + type + " to be thrown,\n" +
+ but + result;
+ throw buildAssertionError(message, t);
+ }
+
+ private AssertionError buildAssertionError(String message, Throwable got) {
+ AssertionError ae = new AssertionError(message);
+ if (got != null) {
+ ae.initCause(got);
}
- throw ae;
+ return ae;
}
/**
diff --git a/assertthrows/src/site/resources/index.html b/assertthrows/src/site/resources/index.html
index 65fc01a..dbef6f2 100644
--- a/assertthrows/src/site/resources/index.html
+++ b/assertthrows/src/site/resources/index.html
@@ -30,6 +30,12 @@
+
+
+This is an alpha release. The package names, API, and features may change in a future release.
+
+
+
Contents
- The Problem
@@ -180,6 +186,16 @@
Anonymous Class
}};
+If you need to verify the exception message in more detail
+(for example, match against a pattern, or verify the cause),
+you can get the last exception or error:
+
+
+Throwable t = new AssertThrows() { public void test() {
+ Integer.parseInt("x");
+}}.getThrowable();
+
+
Please note you need to add the following import statement:
diff --git a/assertthrows/src/test/java/org/junit/contrib/assertthrows/AnonymousClassTest.java b/assertthrows/src/test/java/org/junit/contrib/assertthrows/AnonymousClassTest.java
index da553c6..00a63b1 100644
--- a/assertthrows/src/test/java/org/junit/contrib/assertthrows/AnonymousClassTest.java
+++ b/assertthrows/src/test/java/org/junit/contrib/assertthrows/AnonymousClassTest.java
@@ -105,6 +105,53 @@ public void test() {
assertEquals(IndexOutOfBoundsException.class.getName(),
e.getCause().getClass().getName());
}
+ try {
+ new AssertThrows(new IndexOutOfBoundsException()) {
+ public void test() {
+ list.get(0);
+ }
+ };
+ fail();
+ } catch (AssertionError e) {
+ assertEquals("Expected exception message , but got ",
+ e.getMessage());
+ assertEquals(IndexOutOfBoundsException.class, e.getCause().getClass());
+ }
+ try {
+ new AssertThrows(new Exception("Index: 0, Size: 0")) {
+ public void test() {
+ list.get(0);
+ }
+ };
+ fail();
+ } catch (AssertionError e) {
+ assertEquals("Expected an exception of type\n" +
+ "Exception to be thrown,\n" +
+ "but the method threw an exception of type\n" +
+ "IndexOutOfBoundsException " +
+ "(see in the 'Caused by' for the exception that was thrown)",
+ e.getMessage());
+ assertEquals(IndexOutOfBoundsException.class, e.getCause().getClass());
+ }
+ }
+
+ @Test
+ public void testWrongUsage() {
+ final List list = new ArrayList();
+ new AssertThrows(new NullPointerException(
+ "The passed exception class is null")) {
+ public void test() {
+ new AssertThrows((Class) null) { public void test() {
+ list.get(0);
+ }};
+ }};
+ new AssertThrows(new NullPointerException(
+ "The passed exception is null")) {
+ public void test() {
+ new AssertThrows((Exception) null) { public void test() {
+ list.get(0);
+ }};
+ }};
}
}
diff --git a/assertthrows/src/test/java/org/junit/contrib/assertthrows/DocCodeSamplesTest.java b/assertthrows/src/test/java/org/junit/contrib/assertthrows/DocCodeSamplesTest.java
index 23379d2..5c87939 100644
--- a/assertthrows/src/test/java/org/junit/contrib/assertthrows/DocCodeSamplesTest.java
+++ b/assertthrows/src/test/java/org/junit/contrib/assertthrows/DocCodeSamplesTest.java
@@ -19,6 +19,7 @@
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
+import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.junit.contrib.assertthrows.AssertThrows.assertThrows;
@@ -75,6 +76,10 @@ public void testAnonymousClass() {
public void test() {
Integer.parseInt("x");
}};
+ Throwable t = new AssertThrows() { public void test() {
+ Integer.parseInt("x");
+ }}.getLastThrown();
+ assertEquals("For input string: \"x\"", t.getMessage());
}
}
diff --git a/assertthrows/src/test/java/org/junit/contrib/assertthrows/ExtensionTest.java b/assertthrows/src/test/java/org/junit/contrib/assertthrows/ExtensionTest.java
index 93bf414..0ef0f8c 100644
--- a/assertthrows/src/test/java/org/junit/contrib/assertthrows/ExtensionTest.java
+++ b/assertthrows/src/test/java/org/junit/contrib/assertthrows/ExtensionTest.java
@@ -21,8 +21,8 @@
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
import org.junit.contrib.assertthrows.proxy.ProxyFactory;
+import org.junit.contrib.assertthrows.verify.ExceptionVerifier;
import org.junit.contrib.assertthrows.verify.ResultVerifier;
/**
@@ -37,33 +37,37 @@ public class ExtensionTest {
@Test
public void testAnonymousClass() {
- new AssertEventuallyWorks() { public void test() {
+ new AssertEventuallySucceeds() { public void test() {
assertTrue(random.nextDouble() > 0.9);
}};
- try {
- new AssertEventuallyWorks() { public void test() {
+
+ Throwable e = new AssertThrows() { public void test() {
+ new AssertEventuallySucceeds() { public void test() {
assertTrue(random.nextInt(10) == 10);
}};
- fail();
- } catch (AssertionError e) {
- assertEquals("Verification failed after 100 tries", e.getMessage());
- }
+ }}.getLastThrown();
+ assertEquals("Verification failed after 100 tries", e.getMessage());
}
@Test
public void testProxy() {
ProxyFactory.useClassProxyFactory(Random.class);
assertEventuallyEquals(10, random).nextInt(20);
- try {
+ assertEventuallySucceeds(this).failWithProbability(0.9);
+
+ Throwable e;
+ e = new AssertThrows() { public void test() {
assertEventuallyEquals(20, random).nextInt(10);
- fail();
- } catch (AssertionError e) {
- assertEquals("Verification failed after 100 tries", e.getMessage());
- }
+ }}.getLastThrown();
+ assertEquals("Verification failed after 100 tries", e.getMessage());
+ e = new AssertThrows() { public void test() {
+ assertEventuallySucceeds(ExtensionTest.this).failWithProbability(1.0);
+ }}.getLastThrown();
+ assertEquals("Verification failed after 100 tries", e.getMessage());
}
- private T assertEventuallyEquals(final Object expected, T obj) {
- return AssertThrows.createVerifyingProxy(new ResultVerifier() {
+ T assertEventuallyEquals(final Object expected, T obj) {
+ return ExceptionVerifier.createVerifyingProxy(new ResultVerifier() {
private int count;
@@ -84,39 +88,50 @@ public boolean verify(Object returnValue, Throwable t, Method m, Object... args)
}, obj);
}
+ T assertEventuallySucceeds(T obj) {
+ return ExceptionVerifier.createVerifyingProxy(
+ new SucceedsEventuallyVerifier(), obj);
+ }
+
+ public void failWithProbability(double probability) {
+ if (random.nextDouble() < probability) {
+ throw new IllegalStateException();
+ }
+ }
+
}
/**
* Verify that a given test is successful after at most 100 tries.
*/
-abstract class AssertEventuallyWorks extends AssertThrows {
-
- static final int MAX_TRIES = 100;
+abstract class AssertEventuallySucceeds extends AssertThrows {
- AssertEventuallyWorks() {
- super(new WorksEventuallyVerifier());
+ AssertEventuallySucceeds() {
+ super(new SucceedsEventuallyVerifier());
}
- /**
- * Verifies that a no exception was thrown after at most 100 tries.
- */
- static class WorksEventuallyVerifier implements ResultVerifier {
+}
- private int count;
+/**
+ * Verifies that a no exception was thrown after at most 100 tries.
+ */
+class SucceedsEventuallyVerifier implements ResultVerifier {
- public boolean verify(Object returnValue, Throwable t, Method m, Object... args) {
- if (t == null) {
- return false;
- }
- if (count++ < 100) {
- return true;
- }
- AssertionError ae = new AssertionError(
- "Verification failed after " + MAX_TRIES + " tries");
- ae.initCause(t);
- throw ae;
- }
+ static final int MAX_TRIES = 100;
+
+ private int count;
+ public boolean verify(Object returnValue, Throwable t, Method m, Object... args) {
+ if (t == null) {
+ return false;
+ }
+ if (count++ < 100) {
+ return true;
+ }
+ AssertionError ae = new AssertionError(
+ "Verification failed after " + MAX_TRIES + " tries");
+ ae.initCause(t);
+ throw ae;
}
-}
\ No newline at end of file
+}
diff --git a/assertthrows/src/test/java/org/junit/contrib/assertthrows/ProxyTest.java b/assertthrows/src/test/java/org/junit/contrib/assertthrows/ProxyTest.java
index 5e48771..2df4e24 100644
--- a/assertthrows/src/test/java/org/junit/contrib/assertthrows/ProxyTest.java
+++ b/assertthrows/src/test/java/org/junit/contrib/assertthrows/ProxyTest.java
@@ -32,7 +32,7 @@ public class ProxyTest {
@Test
public void testExpectedException() {
- final List list = new ArrayList();
+ List list = new ArrayList();
assertThrows(list).get(0);
}
@@ -40,44 +40,94 @@ public void testExpectedException() {
public void testExpectedExceptionClass() {
final List list = new ArrayList();
assertThrows(IndexOutOfBoundsException.class, list).get(0);
+ assertThrows(Exception.class, list).get(0);
+ }
+
+ @Test
+ public void testExpectedExceptionAsObject() {
+ final List list = new ArrayList();
+ assertThrows(
+ new IndexOutOfBoundsException("Index: 0, Size: 0"), list).
+ get(0);
}
@Test
public void testDetectNoExceptionWasThrown() {
final List list = new ArrayList();
- try {
+ Throwable e;
+
+ e = new AssertThrows() { public void test() {
assertThrows(list).size();
- } catch (AssertionError e) {
- assertEquals("Expected an exception to be thrown,\n" +
- "but the method size() returned 0",
- e.getMessage());
- assertNull(e.getCause());
- }
- try {
+ }}.getLastThrown();
+ assertEquals("Expected an exception to be thrown,\n" +
+ "but the method size() returned 0",
+ e.getMessage());
+ assertNull(e.getCause());
+
+ e = new AssertThrows() { public void test() {
assertThrows(NullPointerException.class, list).size();
- } catch (AssertionError e) {
- assertEquals("Expected an exception of type\n" +
- "NullPointerException to be thrown,\n" +
- "but the method size() returned 0",
- e.getMessage());
- assertNull(e.getCause());
- }
+ }}.getLastThrown();
+ assertEquals("Expected an exception of type\n" +
+ "NullPointerException to be thrown,\n" +
+ "but the method size() returned 0",
+ e.getMessage());
+ assertNull(e.getCause());
}
@Test
public void testWrongException() {
final List list = new ArrayList();
- try {
+ Throwable e;
+
+ e = new AssertThrows() { public void test() {
assertThrows(NullPointerException.class, list).get(0);
- } catch (AssertionError e) {
- assertEquals("Expected an exception of type\n" +
- "NullPointerException to be thrown,\n" +
- "but the method get(0) threw an exception of type\n" +
- "IndexOutOfBoundsException " +
- "(see in the 'Caused by' for the exception that was thrown)",
- e.getMessage());
- assertEquals(IndexOutOfBoundsException.class, e.getCause().getClass());
- }
+ }}.getLastThrown();
+ assertEquals("Expected an exception of type\n" +
+ "NullPointerException to be thrown,\n" +
+ "but the method get(0) threw an exception of type\n" +
+ "IndexOutOfBoundsException " +
+ "(see in the 'Caused by' for the exception that was thrown)",
+ e.getMessage());
+ assertEquals(IndexOutOfBoundsException.class, e.getCause().getClass());
+
+ e = new AssertThrows() { public void test() {
+ assertThrows(new IndexOutOfBoundsException(), list).get(0);
+ }}.getLastThrown();
+ assertEquals("Expected exception message , but got ",
+ e.getMessage());
+ assertEquals(IndexOutOfBoundsException.class, e.getCause().getClass());
+
+ e = new AssertThrows() { public void test() {
+ assertThrows(new Exception("Index: 0, Size: 0"), list).get(0);
+ }}.getLastThrown();
+ assertEquals("Expected an exception of type\n" +
+ "Exception to be thrown,\n" +
+ "but the method get(0) threw an exception of type\n" +
+ "IndexOutOfBoundsException " +
+ "(see in the 'Caused by' for the exception that was thrown)",
+ e.getMessage());
+ assertEquals(IndexOutOfBoundsException.class, e.getCause().getClass());
+ }
+
+ @Test
+ public void testWrongUsage() {
+ final List list = new ArrayList();
+ Throwable e;
+
+ e = new AssertThrows() { public void test() {
+ assertThrows((List) null).get(0);
+ }}.getLastThrown();
+ assertEquals("The passed object is null", e.getMessage());
+
+ e = new AssertThrows() { public void test() {
+ assertThrows((Class) null, list).get(0);
+ }}.getLastThrown();
+ assertEquals("The passed exception class is null", e.getMessage());
+
+ e = new AssertThrows() { public void test() {
+ assertThrows((Exception) null, list).get(0);
+ }}.getLastThrown();
+ assertEquals("The passed exception is null", e.getMessage());
}
@Test