JUnit Revisited Jan 14 2019 JUnit JUnit is a framework for writing unit tests A unit test is a test of a single class Unit testing is particularly important when software requirements change frequently A test case is a single test of a single method A test suite is a collection of test cases Code often has to be refactored to incorporate the changes Unit testing helps ensure that the refactored code continues to work 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 2 What JUnit does JUnit runs a suite of tests and reports results For each test in the test suite JUnit calls setUp JUnit calls one test method This method should create any objects you may need for testing The test method may comprise multiple test cases that is it may make multiple calls to the method you are testing In fact since it s your code the test method can do anything you want The setUp method ensures you entered the test method with a virgin set of objects what you do with them is up to you JUnit calls tearDown This method should remove any objects you created 3 Test classes in BlueJ The class to be tested The class to test it The menu you get when you right click the test class Use these to run tests Use these to create tests 4 Creating a test class in BlueJ If you have an existing class right click on it and choose Create Test Class If your class is named MyClass the new test class will be named MyClassTest To create the test class first just choose New Class and give the test class the name of a future class with Test appended Later after you create the class to be tested you can rightclick it and choose Create Test Class BlueJ will complain that the class already exists but it will also correctly associate the test class with the class to be tested 5 Creating the setUp method BlueJ has an Object Bench at the bottom of the main window You can right click on a test class and choose You can create objects on the Object Bench by right clicking on a class and choosing one of its new constructors Object Bench To Test Fixture Test Fixture To Object Bench or Since setUp is your code you can modify it any way you like such as creating new objects in it 6 Implementing the tearDown method In most cases the tearDown method doesn t need to do anything The next time you run setUp your objects will be replaced and the old objects will be available for garbage collection It doesn t hurt to set to null the objects you created in setUp Like the finally clause in a try catch finally statement tearDown is where you would release system resources such as streams that might not otherwise be released 7 Recording test cases An easy way to create a test method is to right click a compiled test class and choose Create Test Method Enter the name of the method you want to test you don t have to say test If you wish you can copy Test Fixture To Object Bench Use BlueJ to make calls to the method BlueJ will capitalize your method name and prefix it with test After each call BlueJ will tell you its result and ask you to type in the result you expected The result can be equal to the same as not the same as null not null or equal to double or float You can even create a new object to use as a result When you are done click the End button under Recording Review the results This is a new feature in BlueJ and sometimes it produces bad syntax A comma in your expected result will confuse BlueJ 1 3 0 8 The structure of a test method A test method doesn t return a result If the tests run correctly a test method does nothing If a test fails it throws an AssertionFailedError Hence a test method just calls some assertion method any of which may throw an AssertionFailedError The JUnit framework catches the error and deals with it you don t have to do anything 9 assertX methods static void assertTrue boolean test static void assertFalse boolean test assertEquals expected actual assertSame Object expected Object actual assertNotSame Object expected Object actual assertNull Object object assertNotNull Object object fail All the above may take an optional String message as the first argument for example static void assertTrue String message boolean test 10 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 Depending on the JUnit tool we use we may have to create the class first and we may have to populate it with stubs methods with empty bodies Don t be alarmed if in this simple example the JUnit tests are more code than the class itself 11 JUnit tests for Counter public class CounterTest extends junit framework TestCase Counter counter1 public CounterTest default constructor protected void setUp creates a simple test fixture counter1 new Counter protected void tearDown no resources to release public void testIncrement assertTrue counter1 increment 1 Note that each test begins assertTrue counter1 increment 2 with a brand new counter This means you don t public void testDecrement have to worry about the assertTrue counter1 decrement 1 order in which the tests are run 12 The Counter class itself public class Counter int count 0 public int increment return count public int decrement return count public int getCount return count Is JUnit testing overkill for this little class The Extreme Programming view is If it isn t tested assume it doesn t work You are not likely to have many classes this trivial in a real program so writing JUnit tests for those few trivial classes is no big deal Often even XP programmers don t bother writing tests for simple getter methods such as getCount We only used assertTrue in this example but there are additional assert methods 13 Viewing test results Unexpected errors and exceptions Failed tests If you run a single test and it is successful you just get a message in the status line 14 Problems with unit testing JUnit is designed to call methods and compare the results they return against expected results This ignores Programs that do work in response to GUI commands Methods that are used primary to produce output I think heavy use
View Full Document