Download JUnit: A Java Testing Framework for Test-Driven Development and more Slides Java Programming in PDF only on Docsity!
JUnit for Test Driven Development
What is Test Driven Development
& JUnit?
- Test driven development (TDD) is one of the most successful
productivity enhancing techniques used by many in eXtreme
Programming. TDD has three step: write test, write code &
refactor.
- JUnit is an open source Java testing framework used in TDD.
JUnit was originally written by Erich Gamma and Kent Beck. It
is an instance of the xUnit architecture for unit testing
frameworks.
Desgin of JUnit (2)
- The test class can use the setUp() and tearDown() methods
to initialize and release common objects under test, referred
to as the test fixture.
- TestCase instances can be composed into TestSuites that
can automatically invoke all the testXXX() in each TestCase
instance. TestSuite can also have other TestSuite instances
and this allows the test to run automatically and provide test
status.
How to install JUnit?
• First, download the latest version of JUnit from
http://www.junit.org (Note: Eclipse should
already have JUnit installed.)
• For installing JUnit on Windows:
- Unzip the junit.zip to %JUNIT_HOME%
- Add JUnit to classpath:
- set CLASSPATH=%JUNIT_HOME%\junit.jar
• For installing JUnit on UNIX:
How to write JUnit Test Case?
• To write a test case follows these steps:
– Define a subclass of TestCase.
– Overide the setUp() method to initialize objects
under test.
– Optionally overide tearDown() method to release
object under test.
– Define one or more public testXXX() methods
that exercise the objects under test.
Sample Test Case
import junit.framework.TestCase;
public class ShoppingCartTest extends TestCase {
private ShoppingCart cart; private Product book1; // Sets up the test fixture. //Called before every testcase method. protected void setUp() { cart = new ShoppingCart();
Sample Test Case (3)
public void testEmpty() { cart.empty(); assertEquals(0, cart.getItemCount()); }
...
};
How to write JUnit Test Suite?
• A TestSuite comprises of various TestCase
instances.
• To write a test suite follow these steps:
– Create a class that defines a static suite() factory
method which creates TestSuites for all tests.
– Optionally define a main() method that runs the
TestSuite in batch mode.
Sample Test Suite (2)
// Another example test suite of tests. suite.addTest(CreditCardTestSuite.suite());
// Add more tests here return suite;
}
//Runs the test suite using the textual runner.
public static void main(String[] args) {
junit.textui.TestRunner.run(suite());
}
How to Run JUnit tests?
- JUnit provides textual and graphical interface (GI) to run tests.
Both interface indicates how many tests were run, errors or
failures and test status, “OK” for textual and green bar in GI.
- For running tests from textual interface: java
junit.textui.TestRunner ShoppingCartTest
- For running tests from graphical interface: java
junit.swingui.TestRunner ShoppingCartTest
Why use JUnit?
• Its free!
• It is simple and elegant to use.
• It is easy and inexpensive to write tests using
the JUnit testing framework.
• JUnit tests checks their own result and provide
quick visual feedback.
• Tests can be composed into TestSuites.
• It is integrated into IDE’s like Eclipse and
NetBeans.