# Unit Test Examples

This appendix contains an example of a unit test header file listing and a unit test source file listing.

## Unit Test Header File Example

    /***************************************************************************
                              mymoneyexceptiontest.h
                              -------------------
        copyright            : (C) 2002 by Thomas Baumgart
        email                : ipwizard@users.sourceforge.net
     ***************************************************************************/

    /***************************************************************************
     *                                                                         *
     *   This program is free software; you can redistribute it and/or modify  *
     *   it under the terms of the GNU General Public License as published by  *
     *   the Free Software Foundation; either version 2 of the License, or     *
     *   (at your option) any later version.                                   *
     *                                                                         *
     ***************************************************************************/

    #ifndef __MYMONEYEXCEPTIONTEST_H__
    #define __MYMONEYEXCEPTIONTEST_H__

    #include <QtCore/QObject>

    #define KMM_MYMONEY_UNIT_TESTABLE friend class MyMoneyExceptionTest;

    #include "mymoneyutils.h"
    #include "mymoneyexception.h"

    class MyMoneyExceptionTest : public QObject
    {
      Q_OBJECT

    private slots:
      void init();
      void cleanup();

      void testDefaultConstructor();

      void testConstructor();

    };
    #endif

## Unit Test Source File Example

    /***************************************************************************
                              mymoneyexceptiontest.cpp
                              -------------------
        copyright            : (C) 2002 by Thomas Baumgart
        email                : ipwizard@users.sourceforge.net
     ***************************************************************************/

    /***************************************************************************
     *                                                                         *
     *   This program is free software; you can redistribute it and/or modify  *
     *   it under the terms of the GNU General Public License as published by  *
     *   the Free Software Foundation; either version 2 of the License, or     *
     *   (at your option) any later version.                                   *
     *                                                                         *
     ***************************************************************************/

    #include "mymoneyexceptiontest.h"

    #include <QtTest/QtTest>

    QTEST_MAIN(MyMoneyExceptionTest)

    void MyMoneyExceptionTest::init()
    {
    }

    void MyMoneyExceptionTest::cleanup()
    {
    }

    void MyMoneyExceptionTest::testDefaultConstructor()
    {
      MyMoneyException *e = new MYMONEYEXCEPTION("Message");
      QVERIFY(e->what() == "Message");
      QVERIFY(e->line() == __LINE__ - 2);
      QVERIFY(e->file() == __FILE__);
      delete e;
    }

    void MyMoneyExceptionTest::testConstructor()
    {
      MyMoneyException *e = new MyMoneyException("New message",
          "Joe's file", 1234);
      QVERIFY(e->what() == "New message");
      QVERIFY(e->line() == 1234);
      QVERIFY(e->file() == "Joe's file");
      delete e;
    }

    #include "mymoneyexceptiontest.moc"