JUnit Jan 14 2019 Test suites Obviously you have to test your code to get it working in the first place You can do ad hoc testing testing whatever occurs to you at the moment or You can build a test suite a thorough set of tests that can be run at any time Disadvantages of writing a test suite It s a lot of extra programming You don t have time to do all that extra work True but use of a good test framework can help quite a bit False Experiments repeatedly show that test suites reduce debugging time more than the amount spent building the test suite Advantages of having a test suite Your program will have many fewer bugs It will be a lot easier to maintain and modify your program This is a huge win for programs that unlike class assignments get actual use 2 Example Old way vs new way int max int a int b Test if a b void testMax return a assertEquals 7 max 3 7 else assertEquals 3 max 3 7 return b void testMax int x max 3 7 if x 7 System out println max 3 7 gives x x max 3 7 if x 3 System out println max 3 7 gives x public static void main String args new MyClass testMax 3 XP approach to testing In the Extreme Programming approach Tests are written before the code itself If code has no automated test case it is assumed not to work A test framework is used so that automated testing can be done after every small change to the code This may be as often as every 5 or 10 minutes If a bug is found after development a test is created to keep the bug from coming back Consequences Fewer bugs More maintainable code Continuous integration During development the program always works it may not do everything required but what it does it does right 4 JUnit JUnit is a framework for writing tests JUnit was written by Erich Gamma of Design Patterns fame and Kent Beck creator of XP methodology JUnit uses Java s reflection capabilities Java programs can examine their own code JUnit helps the programmer define and execute tests and test suites formalize requirements and clarify architecture write and debug code integrate code and always be ready to release a working version JUnit is not included in Sun s SDK but almost all IDEs include it 5 Terminology A test fixture sets up the data both objects and primitives that are needed to run tests Example If you are testing code that updates an employee record you need an employee record to test it on A unit test is a test of a single class A test case tests the response of a single method to a particular set of inputs A test suite is a collection of test cases A test runner is software that runs tests and reports results An integration test is a test of how well classes work together JUnit provides some limited support for integration tests 6 Once more in pictures test suite test runner another unit test test case for one method another test case another unit test another test case another test case another test case unit test for one class test case for one method another test case test fixture A unit test tests the methods in a single class A test case tests insofar as possible a single method You can have multiple test cases for a single method A test suite combines unit tests The test fixture provides software support for all this The test runner runs unit tests or an entire test suite Integration testing testing that it all works together is not well supported by JUnit 7 Writing a JUnit test class I Start by importing these JUnit 4 classes import org junit import static org junit Assert note static import Declare your test class in the usual way public class MyProgramTest Declare an instance of the class being tested You can declare other variables but don t give them initial values here public class MyProgramTest MyProgram program int someVariable 8 Writing a JUnit test class II Before public void setUp program new MyProgram someVariable 1000 Define a method or several methods to be executed before each test Initialize your variables in this method so that each test starts with a fresh set of values You can define one or more methods to be executed after each test Typically such methods release resources such as files Usually there is no need to bother with this method After public void tearDown 9 A simple example Suppose you have a class Arithmetic with methods int multiply int x int y and boolean isPositive int x import org junit import static org junit Assert public class ArithmeticTest Test public void testMultiply assertEquals 4 Arithmetic multiply 2 2 assertEquals 15 Arithmetic multiply 3 5 Test public void testIsPositive assertTrue Arithmetic isPositive 5 assertFalse Arithmetic isPositive 5 assertFalse Arithmetic isPositive 0 10 Assert methods I Within a test An assert method is a JUnit method that performs a test and throws an AssertionError if the test fails JUnit catches these Errors and shows you the result static void assertTrue boolean test static void assertTrue String message boolean test Call the method being tested and get the actual result Assert what the correct result should be with one of the assert methods These steps can be repeated as many times as necessary Throws an AssertionErrorif the test fails The optional message is included in the Error static void assertFalse boolean test static void assertFalse String message boolean test Throws an AssertionErrorif the test fails 11 Example Counter class For the sake of example we will create and test a trivial counter class We write the test methods before we write the code The constructor will create a counter and set it to zero The increment method will add one to the counter and return the new value The decrement method will subtract one from the counter and return the new value This has the advantages described earlier However we usually write the method stubs first and let the IDE generate the test method stubs Don t be alarmed if in this simple example the JUnit tests are more code than the class itself 12 JUnit tests for Counter public class CounterTest Counter counter1 declare a Counter here Before void setUp counter1 new Counter initialize the Counter here Test public void testIncrement assertTrue counter1 increment 1 assertTrue counter1 increment 2 Test public void testDecrement assertTrue counter1 decrement 1 Note that each test begins with a brand new counter This means you don t have to worry about the order in which the tests are run 13 The Counter class itself public class Counter int count 0 public int increment return count 1 public int decrement return count 1
View Full Document
Unlocking...