An Introduction to JUnit Greg Jackson June 2004

  • Slides: 41
Download presentation
An Introduction to JUnit Greg Jackson June 2004 Software Quality Assurance & Testing

An Introduction to JUnit Greg Jackson June 2004 Software Quality Assurance & Testing

Contact Info • Northrop Grumman Corp. • Greg. Jackson@ngc. com • 904. 825. 6162

Contact Info • Northrop Grumman Corp. • Greg. Jackson@ngc. com • 904. 825. 6162 (work) Software Quality Assurance & Testing 2

Why Test? • Testing is an important part of large enterprise systems. • Humans

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

Why Automated Testing? • Humans hate testing. • Testing will show when your code

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. Software Quality Assurance & Testing 4

Automated Unit Test • Unit tests help you to make sure that what is

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. Software Quality Assurance & Testing 5

Cost • Automated testing takes more time to develop then performing manual testing. •

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

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

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

Companies Using Java • Presence in Jacksonville • Citigroup (8) • Bank of America

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) Software Quality Assurance & Testing 8

Writing Testing Code • Debugger § Simple § Change debug expressions without recompiling. •

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. Software Quality Assurance & Testing 9

Writing Testing Code • Solution: Automated Testing § Don’t require human judgment to interpret.

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

JUnit Framework • JUnit is a framework for writing unit test for Java classes.

JUnit Framework • JUnit is a framework for writing unit test for Java classes. • Written by Erich Gamma and Kent Beck. • Open Source § http: //www. junit. org § http: //sourceforge. net/projects/junit/ Software Quality Assurance & Testing 11

Definitions • Test Case – defines a fixture to run a related set of

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. Software Quality Assurance & Testing 12

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

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

Steps to Writing a Test Case 1. Subclass junit. framework. Test. Case 2. Override

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

JUnit Test. Case Methods public class Stack. Test extends Test. Case { public Stack.

JUnit Test. Case Methods public class Stack. Test extends Test. Case { public Stack. Test(String arg 0) {…} protected void set. Up() {…} protected void tear. Down() {…} public void test. Push() {…} public static Test suite() {…} public static void main(String[] args) {…} } Software Quality Assurance & Testing 15

JUnit Test. Case Class • Define instance variables that store the state of the

JUnit Test. Case Class • Define instance variables that store the state of the fixture. • Initialize the fixture state by overriding set. Up. • Clean-up after a test by overriding tear. Down • Each test runs its own fixture so there can be no side effects among test runs. Software Quality Assurance & Testing 16

Math. Test public class Math. Test extends Test. Case{ protected double x; protected double

Math. Test public class Math. Test extends Test. Case{ protected double x; protected double y; } Software Quality Assurance & Testing 17

Math. Test. set. Up() public class Math. Test extends Test. Case{ protected double x;

Math. Test. set. Up() public class Math. Test extends Test. Case{ protected double x; protected double y; protected void set. Up() { x = 2. 0; y = 3. 0; } } Software Quality Assurance & Testing 18

tear. Down() public class Math. Test extends Test. Case{ protected double x; protected double

tear. Down() public class Math. Test extends Test. Case{ protected double x; protected double y; protected void tear. Down() { \ simple test } } Software Quality Assurance & Testing 19

Math. Test. test. Add() public class Math. Test extends Test. Case{ public void test.

Math. Test. test. Add() public class Math. Test extends Test. Case{ public void test. Add. XY() { double result = x + y; assert. True(result == 5. 0); } public void test. Add. XX() { double result = x + x; assert. True(result == 4. 0); } } Software Quality Assurance & Testing 20

Running the Math. Test. Case test = new Math. Test(“test. Add. XY”); test. run();

Running the Math. Test. Case test = new Math. Test(“test. Add. XY”); test. run(); Test. Case test 2 = new Math. Test(“test. Add. XX”); test 2. run(); Software Quality Assurance & Testing 21

JUnit Test. Suite Class • The test to run can be collected into a

JUnit Test. Suite Class • The test to run can be collected into a Test. Suite. public static Test suite() { Test. Suite suite = new Test. Suite(); suite. add. Test(new Math. Test(“test. Add. XY”)); suite. add. Test(new Math. Test(“test. Add. XX”)); return suite; } public static Test suite() { return new Test. Suite(Math. Test. class); } Software Quality Assurance & Testing 22

JUnit Test. Runner Class • Test. Runner expects the name of a Test. Case

JUnit Test. Runner Class • Test. Runner expects the name of a Test. Case 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. Test. Runner • junit. awtui. Test. Runner • junit. swingui. Test. Runner public static void main(String[] args) { Test. Runner. run(Math. Test. class); } Software Quality Assurance & Testing 23

assert. Equals() assert. Equals(boolean expected, boolean actual) – Asserts that two booleans are equal.

assert. Equals() assert. Equals(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. assert. Equals(String message, boolean expected, boolean actual) – Uses supplied message when assert fails. Software Quality Assurance & Testing 24

assert. False() / assert. True() assert. False(boolean condition) assert. False(String message, boolean condition) -

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

assert. Not. Null() / assert. Null() assert. Not. Null(java. lang. Object object) - Asserts

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

assert. Same() / assert. Not. Same() assert. Same(java. lang. Object expected, java. lang. Object

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

fail() - Fails a test with no message. fail(java. lang. String message) - Fails

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

Testing for Exceptions try { int x = 2 / 0; fail(“Should have thrown

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

Pitfalls • • • No assert Unreasonable assert Console-Based Testing Unfocused Test Method Failure

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

No Assert • Problem: • Assume that invoking a method is a sufficient test.

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. Software Quality Assurance & Testing 31

Unreasonable assert • Problem: • Tendency to assert everything that can be imagined. •

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. Software Quality Assurance & Testing 32

Console-Based Testing • Problem: • Use of System. out. println • Solution: • System.

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 assert. Not. Null can take its place. • Visual comparison of values are converted to assertions. Software Quality Assurance & Testing 33

Unfocused Test Method • Problem: • Test method unfocused. • Difficult to determine what

Unfocused Test Method • Problem: • Test method unfocused. • Difficult to determine what is being tested. • Not using set. Up and tear. Down, 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. Software Quality Assurance & Testing 34

Failure to Isolate Each Test • Problem: • Test requires external script to run

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 set. Up and tear. Down methods. • Test decorators perform larger setup only once (less resources). They extend Test. Decorator or Test. Setup. Take care of create and remove calls. Software Quality Assurance & Testing 35

Failure to Isolate Subject • Problem: • Failure to isolate test subject from other

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. Software Quality Assurance & Testing 36

Questions on JUnit ? Software Quality Assurance & Testing 37

Questions on JUnit ? Software Quality Assurance & Testing 37

Cactus • Cactus is a simple test framework for unit testing server-side java code

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. • http: //jakarta. apache. org/cactus Software Quality Assurance & Testing 38

Http. Unit • Allows testing of a web application. • Emulates the relevant portions

Http. Unit • Allows testing of a web application. • Emulates the relevant portions of browser behavior, including submission, Java. Script, 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. Software Quality Assurance & Testing 39

Questions ? Software Quality Assurance & Testing 40

Questions ? Software Quality Assurance & Testing 40

References Jakarta Pitfalls: Time-Saving Solutions for Struts, Ant, JUnit, and Cactus (Java Open Source

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 Software Quality Assurance & Testing 41