AgileApps Support Wiki Pre Release

Creating Unit Tests

From AgileApps Support Wiki
Revision as of 04:05, 12 February 2011 by imported>Aeric
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Any method in a Java class can be a test method, as long as it is tagged with the @TestMethod annotation. Within the test method, use assert statements like this one to compare expected results to actual results: RunTest.assertEquals(expected, actual).

Here's a template for a test method:

/**
 * javadoc comment
 */
@TestMethod
public void testSomeBehavior() throws Exception
{
    String expect = "It's working!";         
    String actual = someBehavior();          // Invoke the method you're testing

    RunTest.assertEquals(expect, actual);
}

Thumbsup.gif

Tip: Give your test methods meaningful names that tell what the test was trying to do. That way, when you're reading a report that identifies a failure, the name will tell you a lot. For example: testTwoPlusTwoEqualsFour.

Considerations
  • A single test method can contain multiple assertions.
  • Each successful assertion adds to the success count and the count of total tests.
  • A test method may contain no assertions at all. In that case, it runs to completion, but the test is not counted as a success.
  • A test may fail either because an exception occurs, or because an assertion fails.
  • In either case, the message is recorded. (For an exception, a stack trace is also recorded.)
  • Whether an assertion succeeds or fails, the method continues running. It is only interrupted by an exception.
  • If multiple assertions fail, all of the failure messages are reported.
  • If one or more assertions fail, and then an exception occurs, all of the messages are reported, along with the exception.