CppUnit project page | FAQ

Public Types | Public Member Functions | Private Member Functions | List of all members
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. More...
void | runTest ()
| Checks that the expected exception is thrown by the decorated test. is thrown. More...
Public Member Functions inherited from TestCaseDecorator
| TestCaseDecorator (TestCase *test)
| ~TestCaseDecorator ()
std::string | getName () const
| Returns the test name. More...
void | setUp ()
| Set up context before running a test. More...
void | tearDown ()
| Clean up after the test run. More...
void | runTest ()
| FIXME: this should probably be pure virtual. More...
Public Member Functions inherited from TestCase
| TestCase (const std::string &name)
| Constructs a test case. More...
| TestCase ()
| Constructs a test case for a suite. More...
| ~TestCase ()
| Destructs a test case. More...
virtual void | run (TestResult *result)
| Run the test and catch any exceptions that are triggered by it. More...
std::string | getName () const
| Returns the name of the test case. More...
virtual void | runTest ()
| FIXME: this should probably be pure virtual. More...
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 void | run (TestResult *result)=0
| Run the test, collecting results. More...
virtual int | countTestCases () const =0
| Return the number of test cases invoked by run(). More...
virtual int | getChildTestCount () const =0
| Returns the number of direct child of the test. More...
virtual Test * | getChildTestAt (int index) const
| Returns the child test of the specified index. More...
virtual std::string | getName () const =0
| Returns the test name. More...
virtual bool | findTestPath (const std::string &testName, TestPath &testPath) const
| Finds the test with the specified name and its parents test. More...
virtual bool | findTestPath (const Test *test, TestPath &testPath) const
| Finds the specified test and its parents test. More...
virtual Test * | findTest (const std::string &testName) const
| Finds the test with the specified name in the hierarchy. More...
virtual TestPath | resolveTestPath (const std::string &testPath) const
| Resolved the specified test path with this test acting as 'root'. More...
Public Member Functions inherited from TestFixture
virtual | ~TestFixture ()
virtual void | setUp ()
| Set up context before running a test. More...
virtual void | tearDown ()
| Clean up after the test run. More...

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

## Additional Inherited Members
Protected Member Functions inherited from Test
virtual void | checkIsValidIndex (int index) const
virtual Test * | doGetChildTestAt (int index) const =0
| Returns the child test of the specified valid index. More...
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
Expected exception test case decorator.
Definition: ExceptionTestCaseDecorator.h:45
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 & |  | ) |
| inline private virtual

Called when the exception is caught.

Should be overriden to check the exception.

## ◆ runTest()

template<class ExpectedException >

void ExceptionTestCaseDecorator< ExpectedException >::runTest | ( |  | ) |
| inline virtual

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