Annotating Your Classes

You can simply mark a method with the @Tested annotation to indicate that it should be tested.

Let's take a look at an easy example:


    package org.mycompany.myproject.services;

    public class MyService {
        ...
        @Tested
        public void performAction() {
            ...
        }
    }

We shall refer to this as the System Under Test (SUT) for the duration of this tutorial. JTestConnect uses the jtestconnect.xml configuration file specified in your Ant task to determine how the test for this method should look. Lets take another look at the one we used in our example.

<!-- Configures JTestConnect -->
<jtestconnect>

	<defaults>
		<property name="package" value=""/>
		<property name="classPrefix" value=""/>
		<property name="classSuffix" value="Test"/>
		<property name="methodPrefix" value="test"/>
		<property name="methodSuffix" value=""/>
	</defaults>
	
</jtestconnect>
        

This file defines a naming convention that describes how we name our tests. JTestConnect uses this files to generate expected test names. Let examine what each of these values actually mean:

With this information, JTestConnect will determine that we need a test class called: "MyServiceTest" with a method "testPerformAction". If this method is not found in your test code, JTestConnect will complain.

Testing Everything

If most of the methods in a SUT need to be tested, then we can add @Tested to the class instead.

    package org.mycompany.myproject.services;

    @Tested
    public class MyService {
        ...
        public void performActionA() {
            ...
        }

        public void performActionB() {
            ...
        }
    }

JTestConnect will generate test requirements for all the methods in the class. We still don't want to test all our getters and setters, so we can annotate those with "@NotTested" and they will be excluded from the requirements generation.

Multiple Tests For One Method

Sometimes, one test just ain't enough. For particularly important code we may require that multiple conditions be tested. Maybe a particular validation case needs to be tested, or perhaps we need to ensure that a method fails gracefully when an error occurs.

To create multiple requirements for a single method you can do the following:


    package org.mycompany.myproject.services;

    public class MyService {
        ...
        @Tested(tests={"positiveTest", "negativeTest"})
        public void veryImportantAction() {
            ...
        }
    }

This will generate 2 test requirements for this method. The method name prefixes and suffixes are not used to find the test method name in this case (as the name has been explicitly defined).

Sometimes, when you have rather a lot of tests, you may wish to put them into different test classes. This can be specified as follows:


    package org.mycompany.myproject.services;

    public class MyService {
        ...
        @Tested(testClass=org.mycompany.myproject.services.MyServiceTests1)
        public void myMethod1() {
            ...
        }

        @Tested(testClass=org.mycompany.myproject.services.MyServiceTests2)
        public void myMethod2() {
            ...
        }
    }

This also helps when working with tests that are in classes that simply don't correspond to the naming convention and where it is inconvenient to rename the test class.

That's it for now! Hopefully more features are on their way :-)