10-JUnit - Using JUnit in Eclipse 1, 2 by Christopher Batty...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Department of Computer Science Using JUnit in Eclipse University of Manitoba Tutorial 10 – Page 1 www.cs.umanitoba.ca/~eclipse Using JUnit in Eclipse 1, 2 by Christopher Batty Department of Computer Science, University of Manitoba, Winnipeg, Manitoba, Canada Last revised: October 30, 2003 Overview: In this document, we describe how to use Eclipse’s built-in JUnit tools to create unit tests for Java classes. Unit Testing Basics When developing software we need to run our code to verify that it actually works as intended. As the software gets larger and larger, it becomes more likely that introducing a new change will “break” the existing code. At the same time, verifying all of a program’s features by hand is a menial and time-consuming task. To reduce the need for manual testing, we can introduce unit tests which are small snippets of code designed to thoroughly exercise a particular function or class. With unit tests in place, when a change is made to the code we can simply run all the tests to ensure that nothing has been broken. An additional motivation for unit testing is to support test-driven development. Test- driven development involves writing unit tests first, with the actual code coming after. The reasoning is that in order to write complete, valid tests, you must have a solid understanding of how the new class should work, and in particular what its interface will be. Thus, the unit tests provide an executable definition of the class. When you move on to writing the actual code, you just have to fill in implementation, and once the tests are passed, the implementation is complete. In Java, the standard unit testing framework is known as JUnit. It was created by Erich Gamma and Kent Beck, two authors best known for design patterns and eXtreme Programming, respectively. Eclipse builds come with both JUnit and a plug-in for 1 This work was funded by an IBM Eclipse Innovation Grant. 2 © Christopher Batty and David Scuse
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Department of Computer Science Using JUnit in Eclipse University of Manitoba Tutorial 10 – Page 2 www.cs.umanitoba.ca/~eclipse creating and working with JUnit tests. As a demonstration, we will use these tools to test a simple class for representing fractions. Creating a Test Case Using Eclipse, import the sample project (JUnitDemo) provided with this tutorial. (A second project, JUnitDemoWithTests, that contains the result of adding JUnit tests is also included.) It contains just SimpleFraction.java, which has functionality for creating, modifying and simplifying a Fraction. The first thing we must do is import junit.jar, so we have access to the testing framework. Right-click on the project name, and choose Properties. In the tree on the left, select Java Build Path. Next, choose Add External JARs… and browse to find junit.jar. It will be located in <eclipsedir>\plugins\org.junit_<version number>\junit.jar. Once you successfully import junit.jar, close the Properties page. Now we can create a test for our SimpleFraction class.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 07/17/2009 for the course CSE CSE-106 taught by Professor Cse during the Spring '09 term at A.T. Still University.

Page1 / 9

10-JUnit - Using JUnit in Eclipse 1, 2 by Christopher Batty...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online