In the Java community, extreme programming has become a norm, leading to the increased use of the JUnit
software testing tool by developers. According to
http://junit.sourceforge.net, JUnit is a simple, open source framework to write and run repeatable tests. For test-driven development to be
successful, it is essential to use JUnit effectively.
Tips for
Software Testing Using JUnit
Following are some of the best
practices for working on the JUnit automated testing framework:
Write tests beforehand
Test scripts should be written before you write the code for your particular application. Write the code as and when the automated tests fail. This will not only allow you to design a system that achieves its intended use, but would also
prevent you from over engineering or overbuilding the system.
Expose bugs before you fix
them
Whenever a bug is discovered, write the unit test to expose the bug. Only after you do this, should you go on to fixing the bug. This would ensure that the particular bug
does not occur again.
Write tests for breakable codes
Write tests only for those codes that could possibly break. Do not try and write test scripts for
everything.
Take decisions according to
testing results
In case fixing bugs is taking a lot of time, you need to write more tests in order to allow your application to grow easily. In case you find units that are difficult to test, you
know that the design of those needs improvement.
Test often
You need to test every time a code is changed. Test as frequently as possible to ensure there are
no code breaks.
Avoid creating overlapping
scenarios
Overlapping scenarios may cause
problems in testing and require manual intervention.
Avoid creating test cases with
side effects
Ensure that the data being generated by one test does not corrupt data being generated by another test. If this happens, you may have to manually delete data from the
database to run the test properly.
Let JUnit do the exception
handling
JUnit can catch exceptions automatically. So, avoid creating very complex test codes for detecting
exceptions. This will make your test codes easier to maintain.
Avoid using debug statements
Avoid statements like System.out.println( ). Having used debug statements, you may have to scan the output manually
to check whether the code is getting you the right results.
If used correctly, JUnit can help you develop robust tests and a sound application. However, in the absence of correct guidelines, you may be struggling to ensure a good
system design.