Presentation is loading. Please wait.

Presentation is loading. Please wait.

JUnit Tatiana Totskaya. Main parts of the presentation  Unit Testing  JUnit – Main Concepts  JUnit Primer  Unit Testing in Eclipse Using JUnit.

Similar presentations


Presentation on theme: "JUnit Tatiana Totskaya. Main parts of the presentation  Unit Testing  JUnit – Main Concepts  JUnit Primer  Unit Testing in Eclipse Using JUnit."— Presentation transcript:

1 JUnit Tatiana Totskaya

2 Main parts of the presentation  Unit Testing  JUnit – Main Concepts  JUnit Primer  Unit Testing in Eclipse Using JUnit

3 Unit Testing  In computer programming, unit testing is a procedure used to validate that individual units of source code are working properly.

4 JUnit – Main Concepts  JUnit is a unit testing framework for the Java programming language.  JUnit has been ported to other languages, including: PHP (PHPUnit) C# (NUnit) Python (PyUnit) Fortran (fUnit) Perl (Test::Class and Test::Unit) C++ (CPPUnit) And more recently, owing to the development of rich client frameworks such as AJAX, a port of JUnit has been developed for JavaScript (JSUnit).

5 JUnit Primer  Why Use JUnit?  Step 1: Install JUnit  Step 2: Write a Test Case  Step 3: Write a Test Suite  Step 4: Run the Tests  Step 5: Organize the Tests  Testing Idioms

6 JUnit Primer Why Use JUnit? JUnit tests allow you to write code faster while increasing quality. JUnit is elegantly simple. JUnit tests check their own results and provide immediate feedback. JUnit tests can be composed into a hierarchy of test suites. Writing JUnit tests is inexpensive. JUnit tests increase the stability of software. JUnit tests are developer tests. JUnit tests are written in Java. JUnit is free!

7 JUnit Primer Step 1: Install JUnit 1.First, download the latest version of JUnit, referred to below as junit.zip. 2.Then install JUnit on your platform of choice: Windows To install JUnit on Windows, follow these steps: 1.Unzip the junit.zip distribution file to a directory referred to as %JUNIT_HOME%. 2.Add JUnit to the classpath: set CLASSPATH=%JUNIT_HOME%\junit.jar Unix (bash) To install JUnit on Unix, follow these steps: 1.Unzip the junit.zip distribution file to a directory referred to as $JUNIT_HOME. 2.Add JUnit to the classpath: export CLASSPATH=$JUNIT_HOME/junit.jar 3.Test the installation by using either the textual or graphical test runner to run the sample tests distributed with JUnit. To use the textual test runner, type: java junit.textui.TestRunner junit.samples.AllTests To use the graphical test runner, type: java junit.swingui.TestRunner junit.samples.AllTests

8 JUnit Primer Step 2: Write a Test Case To write a test case, follow these steps: 1.Define a subclass of TestCase. 2.Override the setUp() method to initialize object(s) under test. 3.Optionally override the tearDown() method to release object(s) under test. 4.Define one or more public testXXX() methods that exercise the object(s) under test and assert expected results.

9 JUnit Primer Step 3: Write a Test Suite To write a test suite, follow these steps: 1.Write a Java class that defines a static suite() factory method that creates a TestSuite containing all the tests. 2.Optionally define a main() method that runs the TestSuite in batch mode.

10 JUnit Primer Step 4: Run the Tests To run the test case using the textual user interface, use: java junit.textui.TestRunner ShoppingCartTest To run the test case using the graphical user interface, use: java junit.swingui.TestRunner ShoppingCartTest

11 JUnit Primer Step 5: Organize the Tests Here's the recommended way to organize tests: 1.Create test cases in the same package as the code under test. For example, the com.mydotcom.ecommerce package would contain all the application-level classes as well as the test cases for those components. 2.To avoid combining application and testing code in your source directories, create a mirrored directory structure aligned with the package structure that contains the test code. 3.For each Java package in your application, define a TestSuite class that contains all the tests for validating the code in the package. 4.Define similar TestSuite classes that create higher-level and lower-level test suites in the other packages (and sub-packages) of the application. 5.Make sure your build process includes the compilation of all tests. This helps to ensure that your tests are always up-to-date with the latest code and keeps the tests fresh.

12 JUnit Primer Testing Idioms Keep the following things in mind when writing JUnit tests: The software does well those things that the tests check. Test a little, code a little, test a little, code a little... Make sure all tests always run at 100%. Run all the tests in the system at least once per day (or night). Write tests for the areas of code with the highest probability of breakage. Write tests that have the highest possible return on your testing investment. If you find yourself debugging using System.out.println(), write a test to automatically check the result instead. When a bug is reported, write a test to expose the bug. The next time someone asks you for help debugging, help them write a test. Write unit tests before writing the code and only write new code when a test is failing.

13 Unit Testing in Eclipse Using JUnit Introduction to JUnit Eclipse v. 3.1comes with JUnit built into the Workbench. 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. The class that you will want to test is created first so that Eclipse will be able to find that class under test when you build the test case class. The test cases are built with the needed imports and extensions for JUnit to run. Once the test case class is built the actual test cases are then coded in by the programmer. The creation of test suites in Eclipse is even easier. When you create a test suite, Eclipse will name it for you and will specify all of the test cases in the scope of the project that it can find. The code to run the test suite and to add all of the specified test cases you've created, is added to the test suite for you. Reminder of JUnit Naming Conventions: Test Case Class: Named [classname]Test.java, where classname is the name of the class that is being tested. Test Case Method: Named test[methodname], where methodname is the name of the method that is tested. Test Suite: Default name for Eclipse is AllTests.java

14 Unit Testing in Eclipse Using JUnit File Structure Before Adding fit.jar and junit.jar to the Build Path File Structure After Adding fit.jar and junit.jar to the Build Path It is considered a best practice in testing, to separate the test case code from the application code. It is also a good idea to separate your JUnit and FIT tests as well. Here is an example file structure that helps with this:

15 Unit Testing in Eclipse Using JUnit Putting junit.jar on the Build Path 1.Right click on the project and select Properties. 2.In the left column select Java Build Path. 3.To the right select the Libraries tab. 4.Click the Add External JARs... button. You should start off in the Eclipse install directory, otherwise search through the file system until you find the Eclipse install directory. 5.Under the Eclipse install directory select plugins/org.junit_3.8.1/junit.jar. Click OK. Note: The Eclipse install directory is usually under the C:/ on most computers in the VenIII lab. This is where it is suggested that you install Eclipse on the Eclipse web site. OR 1.Download junit.jar from here, or from www.junit.org. Add junit.jar to your lib/ directory in your project.junit.jarwww.junit.org 2.Right click on the project and select Properties. 3.In the left column select Java Build Path. 4.To the right select the Libraries tab. 5.Click the Add JARs... button. 6.Select your project, and go down through the tree until you find junit.jar under the lib folder. 7.Select junit.jar and Click OK. Note: After you add junit.jar to the build path, it will no longer be displayed in the lib/ directory in the Package Explorer view of Eclipse. The jar file is still located in the lib/ directory when you explore it from outside of Eclipse, but since it is now on the build path inside of Eclipse, it has its own listing in the project.

16 Unit Testing in Eclipse Using JUnit Creating a Test Class There are five ways to create a JUnit Test Case Class. First, select the directory (usually unittests/) that you wish to create the test case class in. 1.Select File > New > JUnit Test Case 2.Select the arrow of the button in the upper left of the toolbar. Select JUnit Test Case, 3.Right click on a package in the Package Explorer view in the Java Perspective, and select JUnitTestCase, or 4.Click on the arrow of the icon in the toolbar. Select JUnit Test Case. 5.You can create a normal Java class as shown in the Eclipse tutorial, but include junit.framework.TestCase as the super class of the test class you are creating.

17 Unit Testing in Eclipse Using JUnit Check to make sure that you are creating the TestCase in the proper package. Give the test case a name. 1.Use the Browse button to search for a different super class. The default super class is junit.framework.TestCase. 2.Check which method stubs you would like to create. You can create a main method, setUp(), tearDown(), or a constructor(), but all of these are optional. A constructor is only run when the test case class is first instantiated, but the setUp() and tearDown() methods are run before and after, respectively, each test case is run. 3.You can browse the application that you are creating for a class that you wish to test, or this could be left blank if you will generate the class while creating while creating the test.

18 Unit Testing in Eclipse Using JUnit If you selected a "Class Under Test" you can click the Next button, otherwise click Finish. You will be able to select which methods in the class under test that you want to write test cases for. The method signatures will be created for you. Click Finish. The new test case class will be open in the editor.

19 Unit Testing in Eclipse Using JUnit import junit.framework.TestCase; public class SampleTest extends TestCase { private java.util.List emptyList; /** * Sets up the test fixture. * (Called before every test case method.) */ protected void setUp() { emptyList = new java.util.ArrayList(); } /** * Tears down the test fixture. * (Called after every test case method.) */ protected void tearDown() { emptyList = null; } public void testSomeBehavior() { assertEquals("Empty list should have 0 elements", 0, emptyList.size()); } public void testForException() { try { Object o = emptyList.get(0); fail("Should raise an IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException success) { } }

20 Unit Testing in Eclipse Using JUnit Creating a Test Suite A TestSuite is a simple way of running one program that, in turn, runs all test cases at one time. 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. 1.Select File > New > Other... > Java > JUnit > JUnit Test Suite. 3.1.2 Select the arrow of the button in the upper left of the toolbar. Select Other... > Java > JUnit > JUnit Test Suite, 2.Right click on a package in the Package Explorer view in the Java Perspective, and select Other... > Java > JUnit > JUnit Test Suite, or 3.You can create a normal Java class as shown in the Eclipse tutorial, but include junit.framework.TestSuite as the super class of the test class you are creating.

21 Unit Testing in Eclipse Using JUnit Check to make sure that you are creating the TestSuite in the proper package. Give the test suite a name. The default name is AllTests.java 1.Use the Browse button to search for a source folder, and the package. 2.Select which test classes you would like to include in the test suite. 3.If you would like to create a main class, check the box by public static void main(String [] args). When this box is checked the next checkbox is highlighted. This is where you can chose which JUnit user interface to use. There is a GUI UI and a command line (or Text) UI. If you do not check either of these checkboxes, when you run the test suite, the GUI UI is automatically used. 4.Click Finish. The new test suite class will be open in the editor.

22 Unit Testing in Eclipse Using JUnit import junit.framework.Test; import junit.framework.TestSuite; public class SampleTestSuite { public static Test suite() { TestSuite suite = new TestSuite("Sample Tests"); // Add one entry for each test class // or test suite. suite.addTestSuite(SampleTest.class); // For a master test suite, use this pattern. // (Note that here, it's recursive!) suite.addTest(AnotherTestSuite.suite()); return suite; }

23 Unit Testing in Eclipse Using JUnit Running JUnit Test Cases There are three ways to run JUnit Test Cases or Test Suites. 1.You can right click on the test case class or test suite class and select Run As > JUnit Test. 2.You can select a test case or suite and click the arrow on the icon or select Run from the toolbar, and select Run As > JUnit Test. 3.You can select a test case or suite and click the arrow on the icon or select Run from the toolbar, and select Run... From here you will create a new JUnit test configuration, and name it. You can choose to run a single test case, or run all test cases in a project or folder.

24 Unit Testing in Eclipse Using JUnit Assertion Statement Reference This is a list of the different types of assertion statements that are used to test your code. Any Java data type or object can be used in the statement. 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)  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)

25 JUnit Thank You! Any questions?


Download ppt "JUnit Tatiana Totskaya. Main parts of the presentation  Unit Testing  JUnit – Main Concepts  JUnit Primer  Unit Testing in Eclipse Using JUnit."

Similar presentations


Ads by Google