@Test(expected=...)
or
@Rule ExpectedException
support or TestNG's
@Test(expectedExceptions=...)
support@Deprecated
public abstract class AssertThrows
extends java.lang.Object
AssertThrows
is a simple method object that encapsulates the
'test-for-exception' scenario for unit testing. Intended for
use with JUnit or TestNG.
Given the following business class...
// the class under test public class Foo { public void someBusinessLogic(String name) { if (name == null) { throw new IllegalArgumentException("The 'name' argument is required"); } // rest of business logic here... } }
The test for the above bad argument path can be expressed using
AssertThrows
like so:
public class FooTest { public void testSomeBusinessLogicBadArgumentPath() { new AssertThrows(IllegalArgumentException.class) { public void test() { new Foo().someBusinessLogic(null); } }.runTest(); } }
This will result in the test passing if the Foo.someBusinessLogic(..)
method threw an IllegalArgumentException
; if it did not, the
test would fail with the following message:
"Must have thrown a [class java.lang.IllegalArgumentException]"
If the wrong type of Throwable
was thrown,
the test will also fail, this time with a message similar to the following:
"java.lang.AssertionError: Was expecting a [class java.lang.UnsupportedOperationException] to be thrown, but instead a [class java.lang.IllegalArgumentException] was thrown"
The test for the correct Throwable
respects polymorphism,
so you can test that any old Exception
is thrown like so:
public class FooTest { public void testSomeBusinessLogicBadArgumentPath() { // any Exception will do... new AssertThrows(Exception.class) { public void test() { new Foo().someBusinessLogic(null); } }.runTest(); } }
Modifier and Type | Field and Description |
---|---|
private java.lang.Throwable |
actualException
Deprecated.
|
private java.lang.Class<? extends java.lang.Throwable> |
expectedException
Deprecated.
|
private java.lang.String |
failureMessage
Deprecated.
|
Constructor and Description |
---|
AssertThrows(java.lang.Class<? extends java.lang.Throwable> expectedException)
Deprecated.
Create a new instance of the
AssertThrows class. |
AssertThrows(java.lang.Class<? extends java.lang.Throwable> expectedException,
java.lang.String failureMessage)
Deprecated.
Create a new instance of the
AssertThrows class. |
Modifier and Type | Method and Description |
---|---|
protected void |
checkExceptionExpectations(java.lang.Throwable actualException)
Deprecated.
Does the donkey work of checking (verifying) that the
Throwable that was thrown in the body of the test is
an instance of the getExpectedException() class (or an
instance of a subclass). |
protected java.lang.String |
createMessageForNoExceptionThrown()
Deprecated.
Creates the failure message used if the test fails
(i.e.
|
protected java.lang.String |
createMessageForWrongThrownExceptionType(java.lang.Throwable actualException)
Deprecated.
Creates the failure message used if the wrong type
of
Throwable is thrown in the body of the test. |
protected void |
doFail()
Deprecated.
Template method called when the test fails; i.e.
|
java.lang.Throwable |
getActualException()
Deprecated.
Expose the actual exception thrown from
test() , if any. |
protected java.lang.Class<? extends java.lang.Throwable> |
getExpectedException()
Deprecated.
Return the
Throwable expected to be thrown during
the execution of the surrounding test. |
protected java.lang.String |
getFailureMessage()
Deprecated.
Return the extra, contextual failure message that will be included
in the failure text if the text fails.
|
void |
runTest()
Deprecated.
The main template method that drives the running of the
test logic and the
checking of the
resulting (expected)
Throwable . |
void |
setFailureMessage(java.lang.String failureMessage)
Deprecated.
Set the extra, contextual failure message that will be included
in the failure text if the text fails.
|
abstract void |
test()
Deprecated.
Subclass must override this
abstract method and
provide the test logic. |
private final java.lang.Class<? extends java.lang.Throwable> expectedException
private java.lang.String failureMessage
private java.lang.Throwable actualException
public AssertThrows(java.lang.Class<? extends java.lang.Throwable> expectedException)
AssertThrows
class.expectedException
- the Throwable
expected to be
thrown during the execution of the surrounding testjava.lang.IllegalArgumentException
- if the supplied expectedException
is
null
; or if said argument is not a Throwable
-derived classpublic AssertThrows(java.lang.Class<? extends java.lang.Throwable> expectedException, java.lang.String failureMessage)
AssertThrows
class.expectedException
- the Throwable
expected to be
thrown during the execution of the surrounding testfailureMessage
- the extra, contextual failure message that will be
included in the failure text if the text fails (can be null
)java.lang.IllegalArgumentException
- if the supplied expectedException
is
null
; or if said argument is not a Throwable
-derived classprotected java.lang.Class<? extends java.lang.Throwable> getExpectedException()
Throwable
expected to be thrown during
the execution of the surrounding test.public void setFailureMessage(java.lang.String failureMessage)
protected java.lang.String getFailureMessage()
public abstract void test() throws java.lang.Throwable
abstract
method and
provide the test logic.java.lang.Throwable
- if an error occurs during the execution of the
aforementioned test logicpublic void runTest()
Throwable
.test()
,
doFail()
,
checkExceptionExpectations(Throwable)
protected void doFail()
Throwable
is not thrown.
The default implementation simply fails the test by throwing an
AssertionError
.
If you want to customize the failure message, consider overriding
createMessageForNoExceptionThrown()
, and / or supplying an
extra, contextual failure message via the appropriate constructor.
protected java.lang.String createMessageForNoExceptionThrown()
getFailureMessage()
protected void checkExceptionExpectations(java.lang.Throwable actualException)
Throwable
that was thrown in the body of the test is
an instance of the getExpectedException()
class (or an
instance of a subclass).
If you want to customize the failure message, consider overriding
createMessageForWrongThrownExceptionType(Throwable)
.
actualException
- the Throwable
that has been thrown
in the body of a test method (will never be null
)protected java.lang.String createMessageForWrongThrownExceptionType(java.lang.Throwable actualException)
Throwable
is thrown in the body of the test.actualException
- the actual exception thrownpublic final java.lang.Throwable getActualException()
test()
, if any.null
if none