Presentation is loading. Please wait.

Presentation is loading. Please wait.

An Introduction to JUnit Greg Jackson June 2004. Software Quality Assurance & Testing 2 Contact Info Northrop Grumman Corp. 904.825.6162.

Similar presentations

Presentation on theme: "An Introduction to JUnit Greg Jackson June 2004. Software Quality Assurance & Testing 2 Contact Info Northrop Grumman Corp. 904.825.6162."— Presentation transcript:

1 An Introduction to JUnit Greg Jackson June 2004

2 Software Quality Assurance & Testing 2 Contact Info Northrop Grumman Corp. 904.825.6162 (work)

3 Software Quality Assurance & Testing 3 Why Test? Testing is an important part of large enterprise systems. Humans make mistakes Bugs cost time & money

4 Software Quality Assurance & Testing 4 Why Automated Testing? Humans hate testing. Testing will show when your code started working and when it breaks. Automated unit-level testing is an essential part of continuous integration. Instantly verify that re-factoring didn’t break code. Allows scheduled verification of code. Documented test results. Test often.

5 Software Quality Assurance & Testing 5 Automated Unit Test Unit tests help you to make sure that what is documented in the API of your classes is actually what you have implemented. Unit test can help you write your code or define what you want your code to do. They test for success, failures, and errors.

6 Software Quality Assurance & Testing 6 Cost Automated testing takes more time to develop then performing manual testing. ROI occurs during system maintenance.

7 Software Quality Assurance & Testing 7 Java Why learn how to test a Java application?

8 Software Quality Assurance & Testing 8 Companies Using Java Presence in Jacksonville Citigroup (8) Bank of America (24) Northrop Grumman (55) *Winn Dixie (162) *CSX (231) *Fidelity Information Services (262) Landstar *Headquarters in Jacksonville (2003 Fortune 500 Ranking)

9 Software Quality Assurance & Testing 9 Writing Testing Code Debugger  Simple  Change debug expressions without recompiling. Print to the standard output stream  System.out.println() Problems:  Require human judgment to analyze results.  Only execute on at a time.  Scroll blindness.

10 Software Quality Assurance & Testing 10 Writing Testing Code Solution: Automated Testing  Don’t require human judgment to interpret.  Easy to run many at the same time.

11 Software Quality Assurance & Testing 11 JUnit Framework JUnit is a framework for writing unit test for Java classes. Written by Erich Gamma and Kent Beck. Open Source  

12 Software Quality Assurance & Testing 12 Definitions Test Case – defines a fixture to run a related set of test. Typically every class you write should have a test case. Test Fixture – provides resources: primitive variables and objects that test need to run. Test Suite – a collection of related test cases.

13 Software Quality Assurance & Testing 13 Stack Example Push Pop Top Is Empty

14 Software Quality Assurance & Testing 14 Steps to Writing a Test Case 1.Subclass junit.framework.TestCase 2.Override the setUp() method to create fixture objects. 3.Define a number of test that return void and whose method name begins with test, such as testAdd(), testPut(), and testIterator(). 4.Override the tearDown() method to release resources that were part of fixture. 5.Group releated sets of test cases by defining a suite of tests.

15 Software Quality Assurance & Testing 15 JUnit TestCase Methods public class StackTest extends TestCase { public StackTest(String arg0) {…} protected void setUp() {…} protected void tearDown() {…} public void testPush() {…} public static Test suite() {…} public static void main(String[] args) {…} }

16 Software Quality Assurance & Testing 16 JUnit TestCase Class Define instance variables that store the state of the fixture. Initialize the fixture state by overriding setUp. Clean-up after a test by overriding tearDown Each test runs its own fixture so there can be no side effects among test runs.

17 Software Quality Assurance & Testing 17 MathTest public class MathTest extends TestCase{ protected double x; protected double y; }

18 Software Quality Assurance & Testing 18 MathTest.setUp() public class MathTest extends TestCase{ protected double x; protected double y; protected void setUp() { x = 2.0; y = 3.0; }

19 Software Quality Assurance & Testing 19 tearDown() public class MathTest extends TestCase{ protected double x; protected double y; protected void tearDown() { \\ simple test }

20 Software Quality Assurance & Testing 20 MathTest.testAdd() public class MathTest extends TestCase{ public void testAddXY() { double result = x + y; assertTrue(result == 5.0); } public void testAddXX() { double result = x + x; assertTrue(result == 4.0); }

21 Software Quality Assurance & Testing 21 Running the MathTest TestCase test = new MathTest(“testAddXY”);; TestCase test2 = new MathTest(“testAddXX”);;

22 Software Quality Assurance & Testing 22 JUnit TestSuite Class The test to run can be collected into a TestSuite. public static Test suite() { TestSuite suite = new TestSuite(); suite.addTest(new MathTest(“testAddXY”)); suite.addTest(new MathTest(“testAddXX”)); return suite; } public static Test suite() { return new TestSuite(MathTest.class); }

23 Software Quality Assurance & Testing 23 JUnit TestRunner Class TestRunner expects the name of a TestCase class as argument. If the class defines a static suite method it will be invoked and the returned test is run. Otherwise all the methods starting with “test” having no arguments are run. junit.textui.TestRunner junit.awtui.TestRunner junit.swingui.TestRunner public static void main(String[] args) {; }

24 Software Quality Assurance & Testing 24 assertEquals() assertEquals(boolean expected, boolean actual) –Asserts that two booleans are equal. –Only displays a message when an assert fails. Supports: boolean, byte, char, double, float, int, long, Object, short, String. assertEquals(String message, boolean expected, boolean actual) –Uses supplied message when assert fails.

25 Software Quality Assurance & Testing 25 assertFalse() / assertTrue() assertFalse(boolean condition) assertFalse(String message, boolean condition) - Asserts that a condition is false. assertTrue(boolean condition) assertTrue(String message, boolean condition) - Asserts that a condition is true.

26 Software Quality Assurance & Testing 26 assertNotNull() / assertNull() assertNotNull(java.lang.Object object) - Asserts that an object isn’t null. assertNull(java.lang.Object object) - Asserts that an object is null.

27 Software Quality Assurance & Testing 27 assertSame() / assertNotSame() assertSame(java.lang.Object expected, java.lang.Object actual) - Asserts that two objects refer to the same object. assertNotSame(java.lang.Object expected, java.lang.Object actual) - Asserts that two objects do not refer to the same object.

28 Software Quality Assurance & Testing 28 fail() fail() - Fails a test with no message. fail(java.lang.String message) - Fails a test with the given message.

29 Software Quality Assurance & Testing 29 Testing for Exceptions try { int x = 2 / 0; fail(“Should have thrown ArithmeticException”); } catch(ArithmeticException e){ // test passed }

30 Software Quality Assurance & Testing 30 Pitfalls No assert Unreasonable assert Console-Based Testing Unfocused Test Method Failure to Isolate Each Test Failure to Isolate Subject

31 Software Quality Assurance & Testing 31 No Assert Problem: Assume that invoking a method is a sufficient test. Assume that if not exceptions are thrown, everything must be OK. Solution: Introduce asserts. The intent of the API needs to be asserted to make sure the documented behavior is actually happening.

32 Software Quality Assurance & Testing 32 Unreasonable assert Problem: Tendency to assert everything that can be imagined. Asserting that the JVM is working. Solution: The intent of the API needs to be asserted to make sure the documented behavior is actually happening. Ask “What aspect of the API does this assert test?”, if no clear answer remove the assert.

33 Software Quality Assurance & Testing 33 Console-Based Testing Problem: Use of System.out.println Solution: System.out becomes assert Why is data printed? If it is to see what is there, often an assertNotNull can take its place. Visual comparison of values are converted to assertions.

34 Software Quality Assurance & Testing 34 Unfocused Test Method Problem: Test method unfocused. Difficult to determine what is being tested. Not using setUp and tearDown, instead put all code in test method. Solution: Keep is simple. Place each related group of asserts into a separate test method. Decompose the complex test. As a general rule, each test method should have one assert.

35 Software Quality Assurance & Testing 35 Failure to Isolate Each Test Problem: Test requires external script to run before test will succeed (ie: initialization of data) Order dependencies (ie: one test creates an object, a second test updates it, and a third test deletes the object) Not cleaning up database after test run. Solution: Use the setUp and tearDown methods. Test decorators perform larger setup only once (less resources). They extend TestDecorator or TestSetup. Take care of create and remove calls.

36 Software Quality Assurance & Testing 36 Failure to Isolate Subject Problem: Failure to isolate test subject from other classes they rely on. Subject test failure caused by code other than the test subject. Solution: Introduce mock objects to replace the objects that the test subject depends on. Mock objects provide the same API but yield hard-coded data and simplified functionality.

37 Software Quality Assurance & Testing 37 Questions on JUnit ?

38 Software Quality Assurance & Testing 38 Cactus Cactus is a simple test framework for unit testing server-side java code (Servlets, EJBs, Tag Ligs, Filters, …) Extends JUnit. Supports Mock Object or Container Testing.

39 Software Quality Assurance & Testing 39 HttpUnit Allows testing of a web application. Emulates the relevant portions of browser behavior, including submission, JavaScript, basic http authentications, cookies, and automatic page redirections, allowing Java test code to examine returned pages either as test, an XML DOM, or containers of forms, tables, and links. Combine with JUnit to write test to verify functionality of a web site.

40 Software Quality Assurance & Testing 40 Questions ?

41 Software Quality Assurance & Testing 41 References Jakarta Pitfalls: Time-Saving Solutions for Struts, Ant, JUnit, and Cactus (Java Open Source Library) Bill Dudney, Jonathan Lehr ISBN: 0-471-44915-6 Java Tools for Extreme Programming: Mastering Open Source Tools, Including Ant, JUnit, and Cactus Richard Hightower, Nicholas Lesiecki ISBN: 0-471-20708-X

Download ppt "An Introduction to JUnit Greg Jackson June 2004. Software Quality Assurance & Testing 2 Contact Info Northrop Grumman Corp. 904.825.6162."

Similar presentations

Ads by Google