Upload
solomon-adams
View
213
Download
1
Embed Size (px)
Citation preview
Intoduction to Unit Testing
Using JUnit to structure Unit Testing
SE-2030Dr. Rob Hasker
1
Based on material by Dr. Mark L. Hornick
How can you test your code?
SE-2030Dr. Rob Hasker
2
How can you test your app?
Run the app with inputs that should produce a known output, and verify the actual output
One problem with this approach may be that you can’t make your app accept “bad” inputs; thus you may not be able to force all possible if-then-else blocks of the app’s classes’ methods to execute
Write a separate “test” program that is designed to exercise the classes and methods of the “production” app
A problem with this might be gathering the results of the exercises and determining whether each one passed or failed.
SE-2030Dr. Rob Hasker
3
What is Unit Testing?
Creating special-purpose test code that exercises specific classes of your application is called Unit Testing
Such test code usually exercises one method at a time (or a small set) whenever possible.
The tests usually include exercising the methods in “boundary conditions” by force-feeding the methods “bad” arguments, such as nulls.
SE-2030Dr. Rob Hasker
4
What is JUnit?
JUnit is an open source Java testing framework used to write and run repeatable tests.
It is built in to Eclipse. JUnit provides a framework for executing tests
General form of tests: create data, check results Operations to check results: assertEquals, assertTrue,
assertFalse, etc. Minimal work needed by developer to set up, run
tests
SE-2030Dr. Rob Hasker
5
Demonstration
SE-2030Dr. Rob Hasker
6
Assertions in Junit
See http://junit.sourceforge.net/javadoc/ assertEquals assertTrue assertFalse assertNotSame assertNull assertArrayEquals fail
SE-2030Dr. Rob Hasker
7