Download presentation
Presentation is loading. Please wait.
Published byDiana Hall Modified over 9 years ago
1
JUnit & Eclipse1 DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING CONCORDIA UNIVERSITY Feb 2, 2009 revision 1.2 – Feb 2, 2009 by Emil Vassev & Joey Paquet
2
JUnit & Eclipse2 Outline Testing Unit Testing Unit Testing Frameworks JUnit – the Java’s Unit Testing Framework Introduction Benefits JUnit Notions Assertion Statement Reference Example Unit Testing in Eclipse using JUnit Introduction JUnit Test Cases JUnit Test Suits
3
JUnit & Eclipse3 Testing Software testing is meant to avoid software failure. A failure is caused by a fault in the code base. A symptom is an observable behavior of the system that enables us to observe a failure and possibly find its corresponding fault. The process of discovering what caused a failure is called fault identification. The process of ensuring that the failure does not happen again is called fault correction, or fault removal. Fault identification and fault correction is popularly called debugging. Software testing, in practice, is about identifying a certain possible system failure and design a test case that proves that this particular failure is not experienced by the software. “testing can reveal only the presence of faults, never their absence.” [Dijkstra] :: Definition
4
JUnit & Eclipse4 Testing There are many driving sources for software testing: Requirements-driven testing, Structure-driven testing, Statistics-driven testing, Risk-driven testing. There are many levels and kinds of software testing: Unit Testing, Integration Testing, Function Testing, Acceptance Testing, Installation Testing. At the day-to-day programming level, unit testing can easily be integrated in the programming effort by using a Unit Testing Framework. However, unit testing cannot be applied for higher-level testing purposes such as function testing or acceptance testing, which are system-level testing activities. :: Definition
5
JUnit & Eclipse5 Unit Testing Defintion: A unit test is a piece of code written by a developer that exercises a very small, specific area of functionality applied to one of the units of the code being tested. Usually a unit test exercises some particular method in a particular context. Example: add a large value to a sorted list, then confirm that this value appears at the end of the list. The goal of unit testing is to isolate important parts of the program and show that the individual parts are free of certain faults. :: Definition
6
JUnit & Eclipse6 Unit Testing Facilitates change: Unit testing allows the programmer to change or refactor code at a later date, and make sure the module still works correctly (i.e. regression testing). Simplifies integration: Unit testing helps to eliminate uncertainty in the units and can be used in a bottom-up integration testing style approach. Documentation: Unit testing provides a sort of living documentation of the specifications of the units of the system. Developers looking to learn what functionality is provided by a unit and how to use it can look at the unit tests to gain understanding of the unit’s API specifications. :: Benefits
7
JUnit & Eclipse7 Unit Testing Identifies defects early in the development cycle. Many small bugs ultimately leads to chaotic system behavior, which becomes increasingly difficult to work on. Successful (and meaningful) tests breed confidence. Makes sure that further changes do not introduce problems into previously correct code. Testing forces the programmers to read and analyze their code, thus removing defects through constant code verification. :: Benefits (cont.)
8
JUnit & Eclipse8 Unit Testing Framework For a large system, there can be thousands of unit tests, which can be tedious to maintain and execute. Automated tests support maintainability and extensibility along with efficiency. A xUnit Testing Framework lets a programmer associate Classes and Methods to corresponding Test Classes and Test Methods. Automation is achieved by automatically setting up a testing context, calling each test case, verifying their corresponding expected result, and reporting the status of all tests. Can be combined with the use of a Software Versioning Repository: prior to any commit being made, unit testing is re- applied to make sure that the committed code is still working properly. :: Rationale
9
JUnit & Eclipse9 JUnit – the Java’s xUnit Testing Framework In Java, the standard unit testing framework is known as JUnit. Test Cases and Test Results are Java objects. JUnit was created by Erich Gamma and Kent Beck, two authors best known for Design Patterns and eXtreme Programming, respectively. Using JUnit you can easily and incrementally build a test suite that will help you measure your progress, spot unintended side effects, and focus your development efforts. :: Introduction
10
JUnit & Eclipse10 JUnit – the Java’s Unit Testing Framework Tested Class – the class that is being tested. Tested Method – the method that is tested. Test Case – the testing of a class’s method against some specified conditions. Test Case Class – a class performing the test cases. Test Case Method – a Test Case Class’s method implementing a test case. Test Suite – a collection of test cases that can be tested in a single batch. :: Key JUnit Notions
11
JUnit & Eclipse11 JUnit – the Java’s Unit Testing Framework The class TestCase has four important methods – run(), setUp(), tearDown() and runTest(). TestCase.run() applies Template Method pattern public void run(){ setUp(); runTest(); tearDown(); } The Template Method pattern “defines the skeleton of an algorithm in an operation, deferring some steps to subclasses”. All Test Case classes need to be subclasses to the TestCase class. :: TestCase Class
12
JUnit & Eclipse12 JUnit – the Java’s Unit Testing Framework JUnit test runners automatically invoke the setUp() method before running each Test Class. This method typically initializes fields, turns on logging, resets environment variables, and so forth, i.e. it sets up a context for the test cases to be applied. protected void setUp() { System.out.println("Before testing"); } In JUnit 4, the initialization method no longer needs to be called setUp(). It just needs to be denoted with the @Before annotation. We can have multiple methods noted @Before, each running before testing. @Before protected void initialize() { System.out.println("Before testing"); } :: TestCase Class – SetUp
13
JUnit & Eclipse13 JUnit – the Java’s Unit Testing Framework If we need at the end of each test to do a cleanup operation, we can use JUnit’s tearDown() method. For example we can call the garbage collector there in case our tests consume large amount of memory. protected void tearDown() { System.out.println(“After testing"); System.gc(); } In JUnit 4, we can give it a more natural name and annotate it with @After. @After protected void disposeObjects () { System.out.println(“After testing"); System.gc(); } :: TestCase Class – TearDown
14
JUnit & Eclipse14 JUnit – the Java’s Unit Testing Framework 1. Create the class that you want to test. 2. Build the test class - with the needed imports and extensions for JUnit. Extend this class from junit.framework.TestCase. Name all the test methods with a prefix of ‘test’. 3. Code the actual test cases. Validate conditions and invariants using one of the several assert methods. import junit.framework.*; public class TestFailure extends TestCase { public void testSquareRootException() { try { SquareRoot.sqrt(-4, 1); fail("Should raise an exception"); } catch (Exception success) { … } } :: Basics - JUnit 3 Tested Class and Method Test Case Method Assertion Statement Test Case Class
15
JUnit & Eclipse15 JUnit – the Java’s Unit Testing Framework Tests are identified by an @Test annotation and we no longer need to prefix our test methods with “test”. This lets us follow the naming convention that best fits our application. import junit.framework.*; import org.junit.Test; public class TestAddition extends TestCase { private int x = 1; private int y = 1; @Test public void addition() { int z = x + y; assertEquals(2, z); } :: Basics – JUnit 4
16
JUnit & Eclipse16 JUnit – the Java’s Unit Testing Framework :: Example – Tested Class
17
JUnit & Eclipse17 JUnit – the Java’s Unit Testing Framework :: Example – Test Class
18
JUnit & Eclipse18 JUnit – the Java’s Unit Testing Framework List of different types of assertion statements that you can use to test your code. These assertions are taken from the JUnit API.JUnit API assertEquals(expected, actual) assertEquals(message, expected, actual) assertEquals(expected, actual, delta) - used on doubles or floats, where delta is the difference in precision. assertEquals(message, expected, actual, delta) - used on doubles or floats, where delta is the difference in precision. assertFalse(condition) assertFalse(message, condition) assertNotNull(object) assertNotNull(message, object) :: Assertion Statement Reference I
19
JUnit & Eclipse19 JUnit – the Java’s Unit Testing Framework assertNotSame(expected, actual) assertNotSame(message, expected, actual) assertNull(object) assertNull(message, object) assertSame(expected, actual) assertSame(message, expected, actual) assertTrue(condition) assertTrue(message, condition) fail() fail(message) failNotEquals(message, expected, actual) failNotSame(message, expected, actual) failSame(message) :: Assertion Statement Reference II
20
JUnit & Eclipse20 Unit Testing in Eclipse using JUnit Eclipse comes with both JUnit and a plug-in for creating and working with JUnit tests. Eclipse allows you to quickly create test case classes and test suite classes to write your test code in. With Eclipse, Test Driven Development (TDD), becomes very easy to organize and implement. Eclipse facilitates the testing by generating automatically stubs for testing class methods. :: Introduction
21
JUnit & Eclipse21 Unit Testing in Eclipse using JUnit Once the class we want to test, is created we can start with building the test cases. To create a test case do [File New JUnit Test Case] :: Adding a Test Case to the Project Put the test case class into the same package as the tested class.
22
JUnit & Eclipse22 Unit Testing in Eclipse using JUnit :: Writing Test Cases This is a test case class for testing SquareRoot class. The following test case methods test different aspects of the sqrt() method: The testSquareRootException() method demonstrates how to test if an exception is properly thrown. The testSquareRootPrecision() method tests against the precision of SquareRoot.sqrt(). The testSquareRootAlgorithm() method tests the square root algorithm.
23
JUnit & Eclipse23 Unit Testing in Eclipse using JUnit :: Running the Test From Menu bar [Run Run As JUnit Test], or:
24
JUnit & Eclipse24 Unit Testing in Eclipse using JUnit :: Test Result Analysis These two test case methods reported failures
25
JUnit & Eclipse25 Unit Testing in Eclipse using JUnit :: Test Result Analysis The following test fails because of insufficient precision: Abs(Math.sqrt(67) - SquareRoot.sqrt(67, 5)) > 0.000001 public void testSquareRootPrecision() throws Exception { Assert.assertEquals("SquareRoot precision less than 0.000001", Math.sqrt(67), SquareRoot.sqrt(67, 5), 0.000001); } In order to increase the precision, we increase the number of iterations from 5 to 6, to arrive at: Assert.assertEquals("SquareRoot precision less than 0.000001", Math.sqrt(67), SquareRoot.sqrt(67, 6), 0.000001);
26
JUnit & Eclipse26 Unit Testing in Eclipse using JUnit :: Test Result Analysis The following test fails because the SquareRoot.sqrt() method does not compute the exact square root of number 67. public void testSquareRootAlgorithm() throws Exception { Assert.assertTrue(67 == (SquareRoot.sqrt(67, 6) * SquareRoot.sqrt(67, 6))); } In order to fix this problem we increase the number of iterations from 6 to 7, to arrive at: Assert.assertTrue(67 == (SquareRoot.sqrt(67, 7) * SquareRoot.sqrt(67, 7)));
27
JUnit & Eclipse27 Unit Testing in Eclipse using JUnit :: Rerunning the Test All the test case methods passed the test.
28
JUnit & Eclipse28 Unit Testing in Eclipse using JUnit :: Test Suit - Introduction We have performed tests on only one class, i.e. we have tested methods under the consideration they belong to the same class. In large projects we have many classes with methods that should be tested. For testing multiple classes Eclipse and JUnit expose the concept of Test Suit. A Test Suit is a collection of test cases that can be tested in a single batch. A Test Suite is a simple way of running one program that, in turn, runs all test cases.
29
JUnit & Eclipse29 Unit Testing in Eclipse using JUnit :: Creating a Test Suit There are four ways to create a JUnit Test Suite Class. First, select the directory (usually unittests/) that you wish to create the test suite class in. Select [File New Other... Java JUnit JUnit Test Suite]. Select the arrow of the button in the upper left of the toolbar. Select [Other... Java JUnit JUnit Test Suite]. Right click on a package in the Package Explorer view in the Java Perspective, and select [Other... Java JUnit JUnit Test Suite]. You can create a normal Java class, but import the package junit.framework and extend the TestSuite class.
30
JUnit & Eclipse30 Unit Testing in Eclipse using JUnit :: Adding a New Class to be Tested This class is taken from the tutorial paper “JUnit Test Infected: Programmers Love Writing Tests”, by Kent Beck and Erich Gamma
31
JUnit & Eclipse31 Unit Testing in Eclipse using JUnit :: Adding a New TestCase Class This class is taken from the tutorial paper “JUnit Test Infected: Programmers Love Writing Tests”, by Kent Beck and Erich Gamma.
32
JUnit & Eclipse32 Unit Testing in Eclipse using JUnit :: Creating the Test Suite Class Creating a Test Suit is straight forward and you just have to follow the wizard. All the tests would be taken care of by JUnit.
33
JUnit & Eclipse33 Unit Testing in Eclipse using JUnit :: Running All Tests Right click on the test suite class and select [Run As JUnit Test]
34
JUnit & Eclipse34 JUnit JUnit JUnit FAQ JUnit FAQ JUnit API JUnit API Eclipse Eclipse An early look at JUnit 4 An early look at JUnit 4 Pragmatic Unit Testing in Java with JUnit Pragmatic Unit Testing in Java with JUnit http://newton.cs.concordia.ca/~paquet/wiki/index.php/Testing http://newton.cs.concordia.ca/~paquet/wiki/index.php/Testing http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks Resources
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.