Testing Java Classes with JUnit

J.E.D.I 3. Clean up the code. Now that the code is working correctly, take a step back and refactor the codes to remove any duplication or any other problem that was introduced to get the test running. As developers, you know that the first pass at writing a piece of code is usually not the best implementation. Do not be afraid to refactor your code. If you break your code, the unit test will let you know immediately.

6.5.2 Testing Java Classes with JUnit

JUnit is a program used to perform unit testing of softwares. It is used for writing and organizing test cases for Java programs. Test cases could be written without the help of JUnit, but JUnit tests, aside from being easier to code, gives the programmer flexibility in grouping small tests allowing simultaneous running of tests and easier program debugging. This section discusses how JUnit is used within the Java Studio TM Enterprise 8. Unit Testing with JUnit Unit testing with JUnit can be accomplished by doing the following: 1. Build a subclass of the class TestCase. 2. Create a method named setUp if some things have to be set before testing. 3. Write as many test methods as you deem necessary. 4. Create a method named tearDown if some things have to be cleared after testing. To run several tests at once, just create a test suite. JUnit provides an object, Software Engineering 288 public class AthleteStatistics{ private Athlete a; private Statistics stats; constructor with Athlete parameter public AthleteStatisticsAthlete theAthlete{ place here code of initializing statistics of an athlete. a = theAthlete; stats = new Statistics; } public void addShootingAverageString dateOfGame, int shots, int attempts{ place here code of adding statistics for the game. } public float getGameShootingAverageString dateOfGame{ place computation here. } public float getSeasonShootingAverage{ place computation here. } } Text 16 Fraction of AthleteStatistics Codes J.E.D.I TestSuite , which runs multiple test cases and test suites together. Using JUnit in Sun Java™ Stud io Enterprise 8 Figure 6.8 Sample Code to be Tested Creating tests in Sun Java™ Studio Enterprise 8 is very easy. Using the sample shown in Figure 6.8 as the class to be tested, create a test by selecting Tools JUnit Tests → → Create Tests from the toolbar. Make sure that the class that you want to test is highlighted in the file’s node. Alternately, tests for a class can also be created by right- clicking the file’s node and choosing Tools JUnit Tests Create Tests → → from the contextual menu. Software Engineering 289 J.E.D.I Figure 6.9 JUnit Create Test Dialog Box In the Create Tests dialog box, select the Code Generation options the user requires for the tests. The IDE will generate a template with the specified options. For this case, everything is unchecked except the Public option. The class name for the test can also be renamed from the default in this dialog box Figure 6.9. JUnit settings can be changed in JUnit Module Settings in the Options window or the Create Tests dialog box. Clicking OK will generate a skeleton test case from the sample code’s to be tested structure Figure 6.10. Methods can be written using test expressions from the Assert class Class ju- nit.framework.Assert . Figure 6.11 shows the test methods written to test the sample code in Figure 1. To run the test, choose Run Test “project-name” → from the toolbar. The test output will then be generated by the IDE. Software Engineering 290 J.E.D.I Figure 6.10 IDE Generated Test Failed test cases are specified in the output box followed by the reason for their failure. For the sample code’s case, testSampleMethod1 failed because the expected string was different from the obtained string. The second test case testSampleMethod2 did not fail, therefore, no error messages were displayed. The test suite can also be edited to add additional test cases outside the file. Other test suites can also be added in the test suite. Other methods like setUp and tearDown can also be included in the test case. These methods are often called test fixtures since they are fixed or set before or after the tests. Software Engineering 291 J.E.D.I Figure 6.11 JUnit Test Case Using assertEquals Samples of the program is shown in Text 16 and Text 14. Software Engineering 292 J.E.D.I Software Engineering 293 SampleClass.java Created on November 17, 2005, 4:03 PM To change this template, choose Tools | Options and locate the template under the Source Creation and Management node. Right-click the template and choose Open. You can then make changes to the template in the Source Editor. author Jaclyn Cua public class SampleClass { public String SampleMethod1int i { return The number is: + i + ; } public int SampleMethod2int x, int y { return x+y; } } Text 17 SampleClass.java J.E.D.I Software Engineering 294 import junit.framework.; SampleClassTest.java JUnit based test Created on November 17, 2005, 4:06 PM author Jaclyn Cua public class SampleClassTest extends TestCase { public SampleClassTestString testName { supertestName; } public static Test suite { TestSuite suite = new TestSuiteSampleClassTest.class; return suite; } public void testSampleMethod1 { SampleClass sample = new SampleClass; String obtained = sample.SampleMethod1100; String expected = I got + 100 + ; assertEqualsSampleClass.SampleMethod1100, expected, obtained; } public void testSampleMethod2 { SampleClass sample = new SampleClass; int obtained = sample.SampleMethod2100,1; int expected = 101; assertEqualsSampleClass.SampleMethod2100,1, expected, obtained; } } Text 18 SampleClassTest.java J.E.D.I

6.6 Testing the System