CppUnit - The Unit Testing Library

CppUnit project page | FAQ

ExceptionTestCaseDecorator< ExpectedException > Class Template Reference

Expected exception test case decorator. More...

`#include <ExceptionTestCaseDecorator.h>`

Inheritance diagram for ExceptionTestCaseDecorator< ExpectedException >:
[legend]
Collaboration diagram for ExceptionTestCaseDecorator< ExpectedException >:
[legend]

## Public Types
typedef ExpectedException | ExpectedExceptionType

## Public Member Functions
| ExceptionTestCaseDecorator (TestCase *test)
| Decorates the specified test.
void | runTest ()
| Checks that the expected exception is thrown by the decorated test. is thrown.
Public Member Functions inherited from TestCaseDecorator
| TestCaseDecorator (TestCase *test)
| ~TestCaseDecorator ()
std::string | getName () const
| Returns the name of the test case.
void | setUp ()
| Set up context before running a test.
void | tearDown ()
| Clean up after the test run.
void | runTest ()
| FIXME: this should probably be pure virtual.
Public Member Functions inherited from TestCase
| TestCase (const std::string &name)
| Constructs a test case.
| TestCase ()
| Constructs a test case for a suite.
| ~TestCase ()
| Destructs a test case.
virtual void | run (TestResult *result)
| Run the test and catch any exceptions that are triggered by it.
Public Member Functions inherited from TestLeaf
int | countTestCases () const
int | getChildTestCount () const
Test * | doGetChildTestAt (int index) const
Public Member Functions inherited from Test
virtual | ~Test ()
virtual Test * | getChildTestAt (int index) const
| Returns the child test of the specified index.
virtual bool | findTestPath (const std::string &testName, TestPath &testPath) const
| Finds the test with the specified name and its parents test.
virtual bool | findTestPath (const Test *test, TestPath &testPath) const
| Finds the specified test and its parents test.
virtual Test * | findTest (const std::string &testName) const
| Finds the test with the specified name in the hierarchy.
virtual TestPath | resolveTestPath (const std::string &testPath) const
| Resolved the specified test path with this test acting as 'root'.
Public Member Functions inherited from TestFixture
virtual | ~TestFixture ()

## Private Member Functions
virtual void | checkException (ExpectedExceptionType &)
| Called when the exception is caught.

## Additional Inherited Members
Protected Member Functions inherited from Test
virtual void | checkIsValidIndex (int index) const
Protected Attributes inherited from TestCaseDecorator
TestCase * | m_test

## Detailed Description

template<class ExpectedException>
class ExceptionTestCaseDecorator< ExpectedException >

Expected exception test case decorator.

A decorator used to assert that a specific test case should throw an exception of a given type.

You should use this class only if you need to check the exception object state (that a specific cause is set for example). If you don't need to do that, you might consider using CPPUNIT_TEST_EXCEPTION() instead.

Intended use is to subclass and override checkException(). Example:

class NetworkErrorTestCaseDecorator :
public ExceptionTestCaseDecorator<NetworkError>
{
public:
NetworkErrorTestCaseDecorator( NetworkError::Cause expectedCause )
: m_expectedCause( expectedCause )
{
}
private:
void checkException( ExpectedExceptionType &e )
{
CPPUNIT_ASSERT_EQUAL( m_expectedCause, e.getCause() );
}
NetworkError::Cause m_expectedCause;
};
ExceptionTestCaseDecorator::ExceptionTestCaseDecorator
ExceptionTestCaseDecorator(TestCase *test)
Decorates the specified test.
Definition ExceptionTestCaseDecorator.h:52
ExceptionTestCaseDecorator::checkException
virtual void checkException(ExpectedExceptionType &)
Called when the exception is caught.
Definition ExceptionTestCaseDecorator.h:95
ExceptionTestCaseDecorator::ExpectedExceptionType
ExpectedException ExpectedExceptionType
Definition ExceptionTestCaseDecorator.h:47
CPPUNIT_ASSERT_EQUAL
#define CPPUNIT_ASSERT_EQUAL(expected, actual)
Asserts that two values are equals.
Definition TestAssert.h:332

## Member Typedef Documentation

## ◆ ExpectedExceptionType

template<class ExpectedException>

typedef ExpectedException ExceptionTestCaseDecorator< ExpectedException >::ExpectedExceptionType

## Constructor & Destructor Documentation

## ◆ ExceptionTestCaseDecorator()

template<class ExpectedException>

ExceptionTestCaseDecorator< ExpectedException >::ExceptionTestCaseDecorator | ( | TestCase * | test | ) |
| inline

Decorates the specified test.

Parameters

  test | TestCase to decorate. Assumes ownership of the test.

## Member Function Documentation

## ◆ checkException()

template<class ExpectedException>

virtual void ExceptionTestCaseDecorator< ExpectedException >::checkException | ( | ExpectedExceptionType & |  | ) |
| inlineprivatevirtual

Called when the exception is caught.

Should be overriden to check the exception.

## ◆ runTest()

template<class ExpectedException>

void ExceptionTestCaseDecorator< ExpectedException >::runTest | ( |  | ) |
| inlinevirtual

Checks that the expected exception is thrown by the decorated test. is thrown.

Calls the decorated test runTest() and checks that an exception of type ExpectedException is thrown. Call checkException() passing the exception that was caught so that some assertions can be made if needed.

Reimplemented from TestCase.

The documentation for this class was generated from the following file:

- ExceptionTestCaseDecorator.h

| Send comments to: CppUnit Developers