Difference between revisions of "Creating Unit Tests"
From AgileApps Support Wiki
imported>Aeric |
imported>Aeric |
||
Line 28: | Line 28: | ||
:* If multiple assertions fail, all of the failure messages are reported. | :* 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. | :* If one or more assertions fail, and then an exception occurs, all of the messages are reported, along with the exception. | ||
:* The method | :* The test method (<tt>testSomeBehavior</tt>, above) must be public. If it isn't, an <tt>IllegalAccessException</tt> occurs when the <tt>@TestMethod</tt> annotation causes the Unit Test Framework to attempt execution. |
Revision as of 22:19, 9 August 2011
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:
- <syntaxhighlight lang="java" enclose="div">
/**
* 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);
} </syntaxhighlight>
- 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.
- The test method (testSomeBehavior, above) must be public. If it isn't, an IllegalAccessException occurs when the @TestMethod annotation causes the Unit Test Framework to attempt execution.