You are here

Additional Capabilities

15 January, 2016 - 09:02
Available under Creative Commons-ShareAlike 4.0 International License. Download for free at

Note that any test method can call other methods if needed. This is useful when the testing procedure is complex and needs to be broken down into smaller pieces. Do not name these helper methods starting with "test" or they will be run separately as test cases! Using helper methods is also useful when certain types of tests are performed only under certain conditions. assertEquals(...), assertTrue(...) and fail(...) can be called from anywhere, but the DrJava will only highlight the line in the main test method that generated the error, i.e. the line that called the helper method. It will not highlight the line in the helper method where the actual call to assertEquals(...), assertTrue(...) or fail(...)was made.

Sometimes in complex testing scenarios, there is a significant amount of initialization work required to set up the system to be tested. This is often the case when testing a system of interdependent objects where the entire system must be set up before any single object can be tested. If the initialization code is the same for all the tests being conducted, the method protected void setup() can be declared and be used to execute the necessary initializations. To insure "clean" test runs, JUnit/DrJava re-instantiates the test class before running each test method. The setup() method, if it exists, is called before any test method is executed. This means that the test class cannot utilize any internal field values that one test method modifies and another test method expects to use as modifed. Likewise, in the event that after a test is run that significant amounts of common "clean-up" code is required, one can declare the method protected void tearDown(). This method runs after each test method and is useful for insuring, for instance, that files and network connections are properly closed and thus keep them from interfering with other tests.

The help system in DrJava has more detailed information on testing with JUnit, including how to create a test suite of multiple tests.