1 / 34

JUnit & Eclipse

DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING CONCORDIA UNIVERSITY. JUnit & Eclipse. revision 1.2 – Feb 2, 2009. by Emil Vassev & Joey Paquet. Feb 2, 2009. Outline. Testing Unit Testing Unit Testing Frameworks JUnit – the Java’s Unit Testing Framework Introduction Benefits

rosalyn
Download Presentation

JUnit & Eclipse

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING CONCORDIA UNIVERSITY JUnit & Eclipse revision 1.2 – Feb 2, 2009 by Emil Vassev & Joey Paquet JUnit & Eclipse Feb 2, 2009

  2. 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 JUnit & Eclipse

  3. Testing :: Definition • 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] JUnit & Eclipse

  4. Testing :: Definition • 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. JUnit & Eclipse

  5. Unit Testing :: Definition Defintion: A unit test is a piece of code written by a developer that exercisesa very small, specific area of functionalityapplied to one of the units of the codebeing tested. Usually a unit test exercises some particularmethod in a particular context. Example: adda large value to a sorted list, then confirm that this value appearsat 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. JUnit & Eclipse

  6. Unit Testing :: Benefits • 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. JUnit & Eclipse

  7. Unit Testing :: Benefits (cont.) • 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. JUnit & Eclipse

  8. Unit TestingFramework :: Rationale • 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. JUnit & Eclipse

  9. JUnit – the Java’s xUnit Testing Framework :: Introduction • In Java, the standard unit testing framework is known as JUnit. • Test Cases and Test Results are Java objects. • JUnit was created by ErichGamma and Kent Beck, two authors best known for Design Patterns and eXtremeProgramming, 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. JUnit & Eclipse

  10. JUnit – the Java’s Unit Testing Framework :: Key JUnit Notions • 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 singlebatch. JUnit & Eclipse

  11. JUnit – the Java’s Unit Testing Framework :: TestCase Class 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. JUnit & Eclipse

  12. JUnit – the Java’s Unit Testing Framework :: TestCase Class – SetUp • 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"); } JUnit & Eclipse

  13. JUnit – the Java’s Unit Testing Framework :: TestCase Class – TearDown • 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(); } JUnit & Eclipse

  14. JUnit – the Java’s Unit Testing Framework :: Basics - JUnit 3 • Create the class that youwant to test. • 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’. • Code the actual test cases. • Validate conditions and invariants using one of the several assert methods. import junit.framework.*; public class TestFailureextends TestCase { public void testSquareRootException() { try { SquareRoot.sqrt(-4, 1); fail("Should raise an exception"); } catch (Exception success) { … } } } Test Case Class Test Case Method Tested Class and Method Assertion Statement JUnit & Eclipse

  15. JUnit – the Java’s Unit Testing Framework :: Basics – JUnit 4 • 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); } } JUnit & Eclipse

  16. JUnit – the Java’s Unit Testing Framework :: Example – Tested Class JUnit & Eclipse

  17. JUnit – the Java’s Unit Testing Framework :: Example – Test Class JUnit & Eclipse

  18. JUnit – the Java’s Unit Testing Framework :: Assertion Statement Reference I List of different types of assertion statements that you can use to test your code. These assertions are taken from the 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) JUnit & Eclipse

  19. JUnit – the Java’s Unit Testing Framework :: Assertion Statement Reference II • 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) JUnit & Eclipse

  20. Unit Testing in Eclipse using JUnit :: Introduction • Eclipse comes with both JUnit and a plug-in forcreating 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. • Eclipsefacilitates the testing by generating automaticallystubs for testing class methods. JUnit & Eclipse

  21. Unit Testing in Eclipse using JUnit Put the test case class into the same package as the tested class. :: Adding a Test Case to the Project • 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] JUnit & Eclipse

  22. Unit Testing in Eclipse using JUnit :: Writing Test Cases This is a test case classfor 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. JUnit & Eclipse

  23. Unit Testing in Eclipse using JUnit :: Running the Test From Menu bar [Run  Run As  JUnit Test], or: JUnit & Eclipse

  24. Unit Testing in Eclipse using JUnit These two test case methods reported failures :: Test Result Analysis JUnit & Eclipse

  25. 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); JUnit & Eclipse

  26. Unit Testing in Eclipse using JUnit :: Test Result Analysis The following test fails because the SquareRoot.sqrt()method does notcompute 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))); JUnit & Eclipse

  27. Unit Testing in Eclipse using JUnit All the test case methods passed the test. :: Rerunning the Test JUnit & Eclipse

  28. 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 TestSuite is a simple way of running one program that, in turn, runs all test cases. JUnit & Eclipse

  29. 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 packagejunit.framework and extend the TestSuite class. JUnit & Eclipse

  30. 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 JUnit & Eclipse

  31. 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. JUnit & Eclipse

  32. 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. JUnit & Eclipse

  33. Unit Testing in Eclipse using JUnit :: Running All Tests Right click on the test suite class and select [Run As  JUnit Test] JUnit & Eclipse

  34. Resources • JUnit • JUnit FAQ • JUnit API • Eclipse • An early look at JUnit 4 • Pragmatic Unit Testing in Java with JUnit • http://newton.cs.concordia.ca/~paquet/wiki/index.php/Testing • http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks JUnit & Eclipse

More Related