JUnit for beginners.

JUnit is an open source framework designed for the purpose of writing and running tests in the Java programming language. JUnit, has been important in the evolution of test-driven development.
JUnit , making it possible to write and test source code quickly and easily. JUnit allows the developer to incrementally build test suites to measure progress and detect unintended side effects. Tests can be run continuously. Results are provided immediately. JUnit shows test progress in a bar that is normally green but turns red when a test fails. An ongoing list of unsuccessful tests appears in a space near the bottom of the display window. Multiple tests can be run concurrently. No subjective human judgments or interpretations of test results are required. The simplicity of JUnit makes it possible for the software developer to easily correct bugs as they are found.

Few reasons to know why to use?


1) JUnit tests allow you to write code faster while increasing quality.2) JUnit is elegantly simple and writing Junit tests is inexpensive. 3) JUnit tests check their own results and provide immediate feedback. 4) JUnit tests can be composed into a hierarchy of test suites. 5) It increase the stabilty of software.

You can download a latest version of JUnit.

STEP1 : Writing a Test Case


import junit.framework.TestCase;
public class ShoppingCartTest extends TestCase {
private ShoppingCart cart;
private Product book1;
protected void setUp() {
cart = new ShoppingCart();
book1 = new Product("Pragmatic Unit Testing", 29.95);
cart.addItem(book1);
}
protected void tearDown() {
// release objects under test here, if necessary
}
public void testEmpty() {
cart.empty();
assertEquals(0, cart.getItemCount());
}
public void testAddItem() {
Product book2 = new Product("Pragmatic Project Automation", 29.95);
cart.addItem(book2);
double expectedBalance = book1.getPrice() + book2.getPrice();
assertEquals(expectedBalance, cart.getBalance(), 0.0);
assertEquals(2, cart.getItemCount());
}
public void testRemoveItem() throws ProductNotFoundException {
cart.removeItem(book1);
assertEquals(0, cart.getItemCount());
}
public void testRemoveItemNotInCart() {
try {
Product book3 = new Product("Pragmatic Version Control", 29.95);
cart.removeItem(book3);
fail("Should raise a ProductNotFoundException");
} catch(ProductNotFoundException expected) {
}
}
}


STEP 2: Write a Test Suite

we'll write a test suite that includes several test cases. The test suite will allow us to run all of its test cases in one fell swoop.
To write a test suite, follow these steps:
Write a Java class that defines a static suite() factory method that creates a TestSuite containing all the tests.
Optionally define a main() method that runs the TestSuite in batch mode.
The following is an example test suite:


import junit.framework.Test;
import junit.framework.TestSuite;
public class EcommerceTestSuite {
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTestSuite(ShoppingCartTest.class);
suite.addTest(CreditCardTestSuite.suite());
return suite;
}
/**
* Runs the test suite using the textual runner.
*/
public static void main(String[] args) {
junit.textui.TestRunner.run(suite());
}
}



You can Download Complete source code.

I hope you got an idea how to run and why to use?

I will update it further very soon,....

No comments:

Post a Comment

Share your thoughts on this post, Suggestions, Comments.Welcome!