Selecting one TestCase

To look in more detail at one test case, TreeNodeTestCase, edit server/project.xml and make the following changes.

vi ${VOSPACE_BUILD}/server/project.xml

Locate the unit test includes section towards the bottom of the file.

    <build>
        <unitTest>
            <includes>

                ....
                ....
                <include>**/*TestCase.java</include>
                ....

            </includes>
        </unitTest>
    </build>

First, comment out the generic pattern '*TestCase.java' that matches all the test cases.

-               <include>**/*TestCase.java</include>
+               <!--include>**/*TestCase.java</include-->

Then add a new pattern that only matches the test you want to run, TreeNodeTestCase .

+               <include>**/TreeNodeTestCase.java</include>

The section should now look like this :

    <build>
        <unitTest>
            <includes>

                ....
                ....

                <!--include>**/*TestCase.java</include-->
                <include>**/TreeNodeTestCase.java</include>

            </includes>
        </unitTest>
    </build>

Save the file and then run the tests again.

    pushd ${VOSPACE_BUILD}/server
        ~/maven/maven-1.0.2/bin/maven test
    popd

This time, the build should only run one test case.

Selecting one test method within the TestCase

Ok, so we have restricted the build to only one test case, but a typical test case will contain several tests.

The nest step is to edit the test case to change it to only run one of the test methods.

    vi ${VOSPACE_BUILD}/server/src/junit/org/astrogrid/vospace/v02/server/node/TreeNodeTestCase.java

By default, JUnit matches and runs all of the methods in the test case whose name starts with 'test'.

    public void testSomething()
        {
        ....
        }

To exclude all of the test methods, do a global search and replace for the phrase 'void test' with 'void notest'.

-   public void testSomething()
+   public void notestSomething()
        {
        ....
        }
Now none of the test methods will match the pattern, so JUnit will ignore all of them.

Choose the test you do want to run, and change that method name back to 'test'.

-   public void notestRootFrogFrog()
+   public void testRootFrogFrog()
        {
        ....
        }

Run the build again and it should only run one test method.

    pushd ${VOSPACE_BUILD}/server
        ~/maven/maven-1.0.2/bin/maven test
    popd

Enabling DEBUG to see what is going on

We have restricted the build to only run one of the tests, but we don't get much clue as to what is going on inside.

    [junit] Running org.astrogrid.vospace.v02.server.node.TreeNodeTestCase
    [junit] 2009-01-09 15:55:12,493 VOSPACE  INFO main Interceptor [322][0][1][2][0]
    [junit] 2009-01-09 15:55:12,497 VOSPACE  WARN main Testing for known exception [org.astrogrid.vospace.v02.server.exception.DuplicateNodeException]
    [junit] 2009-01-09 15:55:12,519 VOSPACE  WARN main About to add a duplicate node
    [junit] 2009-01-09 15:55:12,523 VOSPACE  INFO main Interceptor [27][0][0][1][0]
    [junit] Tests run: 2, Failures: 0, Errors: 0, Time elapsed: 7.252 sec

To enable the verbose debug logging, edit the log4j.properties file

    vi ${VOSPACE_BUILD}/server/src/config/log4j.properties

Change the AstroGrid logging level from INFO to DEBUG.

    #
    # Configure the AstroGrid logger.
-   log4j.logger.org.astrogrid=INFO
+   log4j.logger.org.astrogrid=DEBUG

Run the build again and it should genaret a lot more debug for that one test.

    pushd ${VOSPACE_BUILD}/server
        ~/maven/maven-1.0.2/bin/maven test
    popd

    [junit] Running org.astrogrid.vospace.v02.server.node.TreeNodeTestCase
    [junit] 2009-01-09 16:04:49,239 VOSPACE DEBUG main HibernateTestBase.open()
    [junit] 2009-01-09 16:04:49,242 VOSPACE DEBUG main HibernateTestBase.hibernate()
    [junit] 2009-01-09 16:04:49,269 VOSPACE DEBUG main HibernateConfigImpl()
    [junit] 2009-01-09 16:04:49,270 VOSPACE DEBUG main HibernateConfigImpl.open()
    [junit] 2009-01-09 16:04:51,769 VOSPACE DEBUG main HibernateAwareImpl()

    ....
    
    [junit] 2009-01-09 16:04:55,929 VOSPACE DEBUG main HibernateTestBase.close()
    [junit] 2009-01-09 16:04:55,929 VOSPACE DEBUG main HibernateTestBase.hibernate()
    [junit] 2009-01-09 16:04:55,929 VOSPACE DEBUG main HibernateConfigImpl.close()
    [junit] 2009-01-09 16:04:55,929 VOSPACE DEBUG main Unbinding Session
    [junit] 2009-01-09 16:04:55,929 VOSPACE DEBUG main Closing Session
    [junit] Tests run: 2, Failures: 0, Errors: 0, Time elapsed: 7.459 sec

Making the test fail

The 'testRootFrogFrog' method we enabled check that the server will throw the right exception if we try to create a node with the same name as an existing one.

The first section of the test creates a root node and adds a tree node called 'frog'.

        session.begin();
        final Node root = session.createRootNode(
            Node.GENERATE_NAME
            );
        final Node frog = session.createTreeNode(
            root.ident(),
            "frog"
            );
        session.commit();

The next section of the test tries to create another tree node called 'frog'.

        session.begin();
            ....
                    session.createTreeNode(
                        root.ident(),
                        "frog"
                        );
            ....
        session.rollback();

This part is wrapped in some code that verifies that creating the duplicate node always throws a DuplicateNodeException.

        session.begin();
        assertThrows(
            DuplicateNodeException.class,
            new TestBlock(){
                public void run()
                    throws Exception
                    {
                    session.createTreeNode(
                        root.ident(),
                        "frog"
                        );
                    }
                }
            );
        session.rollback();

So if we change the second part of the test to create a node called 'toad', then we won't get the Exception and the test should fail.

    vi ${VOSPACE_BUILD}/server/src/junit/org/astrogrid/vospace/v02/server/node/TreeNodeTestCase.java

Change the second part of the test so that it creates 'toad' rather than 'frog'.

        session.begin();
            ....
                    session.createTreeNode(
                        root.ident(),
-                       "frog"
+                       "toad"
                        );
            ....
        session.rollback();

Run the build again and it should fail the test.

    pushd ${VOSPACE_BUILD}/server
        ~/maven/maven-1.0.2/bin/maven test
    popd

    [junit] Running org.astrogrid.vospace.v02.server.node.TreeNodeTestCase
    [junit] 2009-01-09 16:37:29,778 VOSPACE DEBUG main HibernateTestBase.open()
    [junit] 2009-01-09 16:37:29,781 VOSPACE DEBUG main HibernateTestBase.hibernate()
    ....
    [junit] 2009-01-09 16:37:36,468 VOSPACE ERROR main Assert throws [org.astrogrid.vospace.v02.server.exception.DuplicateNodeException]
    [junit] 2009-01-09 16:37:36,473 VOSPACE ERROR main FAIL : Test threw different Exception
    [junit] junit.framework.AssertionFailedError: Expected [org.astrogrid.vospace.v02.server.exception.DuplicateNodeException]
    ....
    [junit] 2009-01-09 16:37:36,505 VOSPACE DEBUG main Unbinding Session
    [junit] 2009-01-09 16:37:36,505 VOSPACE DEBUG main Closing Session
    [junit] Tests run: 2, Failures: 1, Errors: 0, Time elapsed: 7.49 sec
    [junit] [ERROR] TEST org.astrogrid.vospace.v02.server.node.TreeNodeTestCase FAILED

In this example we changed the test to make it break.

Normally we would be working on the server code, and running the tests to verify that we have't borken anything.

The test is there to make sure that whatever we do to the server code, it still throws a DuplicateNodeException if we try to create a node with the same name.

-- DaveMorris - 09 Jan 2009

Topic revision: r1 - 2009-01-09 - 16:44:27 - DaveMorris
 
AstroGrid Service Click here for the
AstroGrid Service Web
This is the AstroGrid
Development Wiki

This site is powered by the TWiki collaboration platformCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback