JUnit Don Braffitt Updated: 10-Jun-2011.
-
Upload
rudolph-joseph -
Category
Documents
-
view
213 -
download
0
Transcript of JUnit Don Braffitt Updated: 10-Jun-2011.
![Page 1: JUnit Don Braffitt Updated: 10-Jun-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062804/5697bf8c1a28abf838c8b92a/html5/thumbnails/1.jpg)
JUnitDon Braffitthttp://www.radford.edu/dbraffitt/Updated: 10-Jun-2011
![Page 2: JUnit Don Braffitt Updated: 10-Jun-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062804/5697bf8c1a28abf838c8b92a/html5/thumbnails/2.jpg)
JUnit – Pocket Guide
• JUnit Pocket Guide• By: Kent Beck• Publisher: O'Reilly Media, Inc.• Pub. Date: September 23, 2004• Print ISBN-13: 978-0-596-00743-0• Pages in Print Edition: 90
![Page 3: JUnit Don Braffitt Updated: 10-Jun-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062804/5697bf8c1a28abf838c8b92a/html5/thumbnails/3.jpg)
JUnit – Overview
• Created by Kent Beck and Erich Gamma• Java open source framework for test-driven
development• Appeals to programmers using an agile (iterative
and incremental) programming method such as Extreme Programming which advocates frequent releases in short development cycles
![Page 4: JUnit Don Braffitt Updated: 10-Jun-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062804/5697bf8c1a28abf838c8b92a/html5/thumbnails/4.jpg)
JUnit - Automating Tests
• Consider this starting point
• Now consider this initial testing effort
List fixture= new ArrayList( );// fixture should be emptyObject element= new Object( );fixture. add(element);// fixture should have one element
List fixture= new ArrayList( );System.out.println(fixture.size( ));Object element= new Object( );fixture. add(element);System.out.println(fixture.size( ));
![Page 5: JUnit Don Braffitt Updated: 10-Jun-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062804/5697bf8c1a28abf838c8b92a/html5/thumbnails/5.jpg)
JUnit – Automating Tests
• Now do output only if a failure is detected
• Now build a new method to handle the output
List fixture= new ArrayList( );System.out.println(fixture.size( ) == 0);Object element= new Object( );fixture. add(element);System.out.println(fixture.size( ) == 1);
void assertTrue(boolean condition) throws Exception { if (! condition) throw new Exception("Assertion failed");}
![Page 6: JUnit Don Braffitt Updated: 10-Jun-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062804/5697bf8c1a28abf838c8b92a/html5/thumbnails/6.jpg)
JUnit – Automating Tests
• Finally, use the new AssertTrue method
List fixture= new ArrayList( );assertTrue(fixture.size( ) == 0);Object element= new Object( );fixture. add(element);assertTrue(fixture.size( ) == 1);
![Page 7: JUnit Don Braffitt Updated: 10-Jun-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062804/5697bf8c1a28abf838c8b92a/html5/thumbnails/7.jpg)
JUnit - Why Test?
• JUnit does the following:• Runs tests automatically• Runs many tests together and summarizes the results• Provides a convenient place to collect the tests you’ve
written• Provides a convenient place for sharing the code used
to create the objects you are going to test• Compares actual results to expected results and
reports differences
![Page 8: JUnit Don Braffitt Updated: 10-Jun-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062804/5697bf8c1a28abf838c8b92a/html5/thumbnails/8.jpg)
JUnit – Why Test?
• Win-win-win programming practice• A win for me in the short term• A win for me in the long term• A win for my teammates and customers
![Page 9: JUnit Don Braffitt Updated: 10-Jun-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062804/5697bf8c1a28abf838c8b92a/html5/thumbnails/9.jpg)
JUnit – Why Test?
• Defect Cost Increase (DCI)• The sooner you test after the creation of an error, the
greater your chance of finding the error and the less it costs to find and fix the error
![Page 10: JUnit Don Braffitt Updated: 10-Jun-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062804/5697bf8c1a28abf838c8b92a/html5/thumbnails/10.jpg)
JUnit – Goals
• Simultaneously tests should be:• Easy to write. Test code should contain no extraneous
overhead.• Easy to learn to write. Because our target audience for JUnit is
programmers who are not usually professional testers, we would like to minimize the barriers to test writing.
• Quick to execute. Tests should run fast so we can run them hundreds or thousands of times a day.
• Easy to execute. The tests should run at the touch of a button and present their results in a clear and unambiguous format.
• Isolated. Tests should not affect each other. If the order in which the tests are run changes, the results shouldn’t change.
• Composable. We should be able to run any number or combination of tests together. This is a corollary of isolation.
![Page 11: JUnit Don Braffitt Updated: 10-Jun-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062804/5697bf8c1a28abf838c8b92a/html5/thumbnails/11.jpg)
JUnit – Goals
• There are two main clashes between these constraints:• Easy to write versus easy to learn to write. Tests do
not generally require all the flexibility of a programming language, especially not an object language.• Isolated versus quick to execute. If you want the
results of one test to have no effect on the results of another test, each test should create the full state of the world before it begins to execute and return the world to its original state when it finishes.
![Page 12: JUnit Don Braffitt Updated: 10-Jun-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062804/5697bf8c1a28abf838c8b92a/html5/thumbnails/12.jpg)
JUnit - Fixtures• setUp()• tearDown()• Move variable parts of setUp() to test methods• No convenient support for suite-level setup
![Page 13: JUnit Don Braffitt Updated: 10-Jun-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062804/5697bf8c1a28abf838c8b92a/html5/thumbnails/13.jpg)
JUnit - Testing Exceptions
![Page 14: JUnit Don Braffitt Updated: 10-Jun-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062804/5697bf8c1a28abf838c8b92a/html5/thumbnails/14.jpg)
JUnit Implementation
• Consider a test case class with two test methods
public class EmptyTest extends TestCase { List empty= new ArrayList( ); public void testSize( ) { assertEquals(0, empty.size( )); } public void testIsEmpty( ) { assertTrue(empty.isEmpty( )); }}
![Page 15: JUnit Don Braffitt Updated: 10-Jun-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062804/5697bf8c1a28abf838c8b92a/html5/thumbnails/15.jpg)
JUnit Implementation
• JUnit converts the test class into a TestSuite
![Page 16: JUnit Don Braffitt Updated: 10-Jun-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062804/5697bf8c1a28abf838c8b92a/html5/thumbnails/16.jpg)
JUnit Implementation
• When the TestSuite is run, it runs each of the EmptyTest in turn• Each runs its own setUp( ) method, creating a
fresh ArrayList for each test
![Page 17: JUnit Don Braffitt Updated: 10-Jun-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062804/5697bf8c1a28abf838c8b92a/html5/thumbnails/17.jpg)
JUnit Implementation
• To run the test method itself, JUnit uses reflection to find the method named in fName and invokes it• This trick is called Pluggable Selector in the
Smalltalk world• You can’t look at the code to decide whether a
function is invoked, you have to look at the data values at runtime.• Pluggable Selectors in JUnit make writing the
tests much simpler
![Page 18: JUnit Don Braffitt Updated: 10-Jun-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062804/5697bf8c1a28abf838c8b92a/html5/thumbnails/18.jpg)
JUnit Implementation
• Summary• One test-case class results in a two-level tree of
objects when the tests are run• Each test method works with its own copy of the
objects created by setUp( )• The result is tests that can run completely
independently
![Page 19: JUnit Don Braffitt Updated: 10-Jun-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062804/5697bf8c1a28abf838c8b92a/html5/thumbnails/19.jpg)
JUnit’s API
• Five main classes or interfaces• Assert - A collection of static methods for checking
actual values against expected values• Test - The interface of all objects that act like tests• TestCase - A single test• TestSuite - A collection of tests• TestResult - A summary of the results of running
one or more tests
![Page 20: JUnit Don Braffitt Updated: 10-Jun-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062804/5697bf8c1a28abf838c8b92a/html5/thumbnails/20.jpg)
JUnit - Test-First Programming
• Part of test-driven development• White-box tests for whole specification• Incremental tests as you gradually add specified
functionality• Tests are expected to fail since you don’t have
complete functionality to test
![Page 21: JUnit Don Braffitt Updated: 10-Jun-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062804/5697bf8c1a28abf838c8b92a/html5/thumbnails/21.jpg)
JUnit - Stubs
• Stubs don’t faithfully reproduce the behavior of the intended object• Always better to have at least one test case
failing while the stub is in place
![Page 22: JUnit Don Braffitt Updated: 10-Jun-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062804/5697bf8c1a28abf838c8b92a/html5/thumbnails/22.jpg)
JUnit - Other Uses for Tests
• Debugging Tests• Learning an API with Tests• Documenting Assumptions with Tests• Cross-Team Tests
![Page 23: JUnit Don Braffitt Updated: 10-Jun-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062804/5697bf8c1a28abf838c8b92a/html5/thumbnails/23.jpg)
Story of JUnit
• Kent Beck 1994• Smalltalk testing framework (3 classes 12
methods)• Eric Gamma 1997• With Kent Beck, built most of JUnit on a plane in 3
hours
![Page 24: JUnit Don Braffitt Updated: 10-Jun-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062804/5697bf8c1a28abf838c8b92a/html5/thumbnails/24.jpg)
Extending JUnit
• Add your own extensions• Subclass TestCase• Write new Assert classes• Write new Test Runner
• Pre-built extensions• JUnitPerf• HttpUnit• JWebUnit and many others
![Page 25: JUnit Don Braffitt Updated: 10-Jun-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062804/5697bf8c1a28abf838c8b92a/html5/thumbnails/25.jpg)
JUnit and Ant
![Page 26: JUnit Don Braffitt Updated: 10-Jun-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062804/5697bf8c1a28abf838c8b92a/html5/thumbnails/26.jpg)
Running JUnit Standalone
![Page 27: JUnit Don Braffitt Updated: 10-Jun-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062804/5697bf8c1a28abf838c8b92a/html5/thumbnails/27.jpg)
JUnit and IDEs
![Page 28: JUnit Don Braffitt Updated: 10-Jun-2011.](https://reader036.fdocuments.us/reader036/viewer/2022062804/5697bf8c1a28abf838c8b92a/html5/thumbnails/28.jpg)
JUnit - Test Infection
• Write lots of tests• Runs your tests often• Learn the design skills necessary to write tests
that are simple and run fast• Start sharing your skills with others