JUnit in Action SECOND EDITION PETAR TAHCHIEV FELIPE LEME VINCENT MASSOL GARY GREGORY ©2011 by Manning Publications Co. All rights reserved. Slides Prepared by: Dr. Samer Hanna Needed downloads to run this book’s tests 1) Java 2 Standard Edition JDK http://www.oracle.com/technetwork/java/javase/downloads/index.html Current version is JDK 6 update 22 2) Eclipse IDE http://www.eclipse.org/downloads/ Note: Eclipse documentation is available at: http://help.eclipse.org/helios/index.jsp PART 1 JUNIT ESSENTIALS Ch. 1 ■ JUnit jump-start Ch. 2 ■ Exploring core JUnit Ch. 3 ■ Mastering JUnit Ch. 4 ■ Software testing principles Ch1. JUnit jump-start This chapter covers ■ Exploring JUnit ■ Installing JUnit ■ Writing our first test ■ Running tests Introduction • JUnit (http://www.junit.org) is open source software • released under IBM’s Common Public License Version 1.0 and hosted on SourceForge • JUnit quickly became the de facto standard framework for developing unit tests in Java. • DEFINITION A unit test examines the behavior of a distinct unit of work. Within a Java application, the “distinct unit of work” is often (but not always) a single method. Cont. • By contrast, integration tests and acceptance tests examine how various components interact. • A unit of work is a task that isn’t directly dependent on the completion of any other task. Setting up JUnit • In order to use JUnit to write your application tests, you need to add the JUnit JAR file to your project’s compilation classpath and to your execution classpath. Follow these steps: • Download the JUnit distribution (junit-4.10 or newer) from http://sourceforge.net/projects/junit/ • Unzip the distribution zip file to a directory on your computer system • In this directory, unzipping will create a subdirectory for the JUnit distribution you downloaded (for example, C:\junit4.6) Adding junit-4.6.jar to our external jar files using Eclipse • To run all the examples in this book we should tell Eclipse where it can find the junit-4.6.jar (or later) jar file and this is done by the following steps: • Go to project menu • Choose properties • Choose Java Build Path • Choose Add External Jars • A Jar Selection window will be opened and you can browse to the folder where the junit-4.10.jar file is stored and select this file. Now you are ready to use JUnit Testing with JUnit Example The JUnit CalculatorTest program CalculatorTest details • At 1, we start by defining a test class. • The only restriction is that the class must be public; we can name it whatever we like. • It’s common practice to end the class name with Test. • At 2, we mark the method as a unit test method by adding the @Test annotation. • A best practice is to name test methods following the testXXX pattern. Cont. • JUnit doesn’t have method name restrictions. You can name your methods as you like; as long as they have the @Test annotation, JUnit will execute them. • At 3, we start the test by creating an instance of the Calculator class (the “object under test”). • At 4, as before, we execute the test by calling the method to test, passing it two known values. • At 5, the JUnit framework begins to shine! To check the result of the test, we call an assertEquals method, which we imported with a static import on the first line of the class assertEquals method • The Javadoc for the assertEquals method is as follows: /** * Asserts that two doubles or floats are equal to within a positive delta. */ static public void assertEquals( double expected, double actual, double delta) • In the previous example, we passed assertEquals these parameters: • expected = 60 • actual = result • delta = 0 • Because we passed the calculator the values 10 and 50, we tell assertEquals to expect the sum to be 60. (We pass 0 as the delta because we’re adding integers.) • When we called the calculator object, we tucked the return value into a local double named result. • Therefore, we pass that variable to assertEquals to compare against the expected value of 60. Cont. • If the actual value isn’t equal to the expected value, JUnit throws an unchecked exception, which causes the test to fail. • Most often, the delta parameter can be zero, and we can safely ignore it. It comes into play with calculations that aren’t always precise, which includes many floating point calculations. • The delta provides a range factor. If the actual value is within the range expected - delta and expected + delta, the test will pass. Summary • Every developer should perform some type of test to see if code works. • Developers who use automatic unit tests can repeat these tests on demand to ensure that new code works and doesn’t break existing tests. • Simple unit tests aren’t difficult to create without JUnit, but as tests are added and become more complex, writing and maintaining tests becomes more difficult. • JUnit is a unit testing framework that makes it easier to create, run, and revise unit tests. Summary (Cont.) • In this chapter, we scratched the surface of JUnit by stepping through a simple test. • JUnit has much more to offer. • In chapter 2 we take a closer look at the JUnit framework classes (different annotations and assertion mechanisms) and how they work together to make unit testing efficient and effective.