Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unit testing Java programs Using JUnit

Similar presentations


Presentation on theme: "Unit testing Java programs Using JUnit"— Presentation transcript:

1 Unit testing Java programs Using JUnit
“If it isn't tested, it doesn’t work” Unit testing Java programs

2 Requirements for tests
Tests must be executable A test must clearly show whether it executed successfully or not The not-so-successfully part of the test must not be buried in a pile of test reports. Unit testing Java programs

3 Which methods should be tested
Test a method if you are not 100% sure that the method is correct. Methods that usually does not need testing Simple get and set methods However, you might call get and set methods in testing other (more complex) methods Simple toString methods Unit testing Java programs

4 Unit testing Java programs
Individual test cases How to write a test case Extends TestCase class MyTest extends TestCase { … } A test method must have the signature public void testXxx() throws ExceptionsAsYouLike Generally you would like one test case pr. Java class The unit to test is a class. Unit testing Java programs

5 Fixtures: setUp and tearDown
Sometimes you have 2 or more tests that must run on the same data. To ease this JUnit introduces the concept of a “fixture”. 2 methods inherited from TestCase meant to be overridden setUp() Executed before each individual test tearDown() Executed after each individual test Unit testing Java programs

6 Unit testing Java programs
Test suites You get a lot of test cases (1 pr. Java class). To several test cases at once you define a “test suite” Make an object of class TestSuite Add the individual test cases to the TestSuite Run the TestSuite Unit testing Java programs

7 Unit testing Java programs
Testing exceptions Testing an expected exception try { method(); fail(“Exception expected”); } catch (ExpectedException ex) { /* ignore */ } A test method must have the signature public void testXxx() throws Exception That means that you can throw any exception from a test method. However, if a test methods throws an exception it means that the test has not passed. Unit testing Java programs

8 Unit testing Java programs
NetBeans assistance NetBeans can assist you in making TestCases for individual Java class and in assembling the test cases into test suites. TestCase Tools → JUnit tests JUnit generates empty (in fact failing) tests for each public / protected method in a Java class. Fill you the empty tests and run the test. Test suites New → Test Suite JUnit generates a test suite of all the test cases in the current test package. Unit testing Java programs

9 Unit testing Java programs
Running tests JUnit test can be run in several ways With a graphical user interface junit.swingui.TestRunner Uses Swing classes for the GUI With a textual user interface Junit.textui.TextRunner NetBeans uses a textual user interface when you run a test case or suite. Unit testing Java programs

10 The internal structure of JUnit
JUnit uses the Reflection API Objects of class Class are submitted to the test suites. From an object Class JUnit finds all methods named testXxx() and executes those methods. Calling setUp() before testXxx() And calling tearDown() after testXxx() Unit testing Java programs

11 Composite design pattern in JUnit
JUnit uses the composite design pattern to organize individual test cases and test suites Unit testing Java programs

12 Unit testing Java programs
Testing in XP Testing is an important discipline in XP (Extreme Programming) XP idea: Create the test before the code to be tested Writing the test makes you thing about detailed design Test is an executable requirements Writing (and running) test will be a positive experience. We know when a class is done When all tests run Unit testing Java programs

13 Unit testing Java programs
References Beck & Gamma JUnit Cookbook, Kent Beck & Erich Gamma invented JUnit Martin Fowler Refactoring, Addison Wesley 2000 Chapter 4 Building Tests, page Extreme Programming, Code the Unit Test First Testing is an important discipline in XP (eXtreme Programming), which is another Kent Bech invention. Alex Garrett JUnit antipatterns Unit testing Java programs


Download ppt "Unit testing Java programs Using JUnit"

Similar presentations


Ads by Google