JUnit, RevisitedJUnitWhat JUnit doesTest classes in BlueJCreating a test class in BlueJCreating the setUp() methodImplementing the tearDown() methodRecording test casesThe structure of a test methodassertX methodsExample: Counter classJUnit tests for CounterThe Counter class itselfViewing test resultsProblems with unit testingThe GUI problemThe printing problemThe EndJan 14, 2019JUnit, Revisited2JUnitJUnit is a framework for writing unit testsA unit test is a test of a single classA test case is a single test of a single methodA test suite is a collection of test casesUnit testing is particularly important when software requirements change frequentlyCode often has to be refactored to incorporate the changesUnit testing helps ensure that the refactored code continues to workJUnit helps the programmer:define and execute tests and test suitesformalize requirements and clarify architecturewrite and debug codeintegrate code and always be ready to release a working version3What JUnit doesJUnit runs a suite of tests and reports resultsFor each test in the test suite:JUnit calls setUp()This method should create any objects you may need for testingJUnit calls one test methodThe test method may comprise multiple test cases; that is, it may make multiple calls to the method you are testingIn fact, since it’s your code, the test method can do anything you wantThe setUp() method ensures you entered the test method with a virgin set of objects; what you do with them is up to youJUnit calls tearDown()This method should remove any objects you created4Test classes in BlueJThe class tobe testedThe classto test itThe menu youget when youright-click thetest classUse theseto createtestsUse theseto run tests5Creating a test class in BlueJIf you have an existing class, right-click on it and choose Create Test ClassIf your class is named MyClass, the new test class will be named MyClassTestTo create the test class first, just choose New Class... and give the test class the name of a future class, with ‘Test’ appendedLater, after you create the class to be tested,you can right-click it and choose Create Test ClassBlueJ will complain that the class already exists, but it will also correctly associate the test class with the class to be tested6Creating the setUp() methodBlueJ has an Object Bench (at the bottom of the main window)You can create objects on the Object Bench by right-clicking on a class and choosing one of its new constructorsYou can right-click on a test class and choose:Object Bench To Test Fixture orTest Fixture To Object BenchSince setUp() is your code, you can modify it any way you like (such as creating new objects in it)7Implementing the tearDown() methodIn most cases, the tearDown() method doesn’t need to do anythingThe next time you run setUp(), your objects will be replaced, and the old objects will be available for garbage collectionIt 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 released8Recording test casesAn 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”BlueJ will capitalize your method name and prefix it with testIf you wish, you can copy Test Fixture To Object BenchUse BlueJ to make calls to the methodAfter each call, BlueJ will tell you its result, and ask you to type in the result you expectedThe 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 resultWhen you are done click the End button (under Recording)Review the results!This is a new feature in BlueJ, and sometimes it produces bad syntaxA comma in your expected result will confuse BlueJ 1.3.09The structure of a test methodA test method doesn’t return a resultIf the tests run correctly, a test method does nothingIf a test fails, it throws an AssertionFailedErrorHence, a test method just calls some assertion method, any of which may throw an AssertionFailedErrorThe JUnit framework catches the error and deals with it; you don’t have to do anything10assertX methodsstatic void assertTrue(boolean test)static void assertFalse(boolean te st)assertEquals(expected, actual )assertSame(Object,expected, Object,actual)assertNotSame(Object,expected, Object,actual)assertNull(Object,object)assertNotNull(Object,obje ct) fail()All the above may take an optional String message as the first argument, for example,static void assertTrue(String message, boolean test)11Example: Counter classFor the sake of example, we will create and test a trivial “counter” classThe constructor will create a counter and set it to zeroThe increment method will add one to the counter and return the new valueThe decrement method will subtract one from the counter and return the new valueWe write the test methods before we write the codeThis has the advantages described earlierDepending 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 itself12JUnit 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); assertTrue(counter1.increment() == 2); } public void testDecrement() { assertTrue(counter1.decrement() == -1); }}Note that each test begins with a brand new counterThis means you don’t have to worry about the order in which the tests are run13The Counter class itselfpublic 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 Full Document