0% found this document useful (0 votes)
77 views3 pages

Junit - A Standard Unit Testing Tool For Java Programs: Testresult

The document summarizes JUnit, a standard unit testing framework for Java. It describes the key aspects of JUnit including its open source availability, core classes like TestResult and TestSuite, use of design patterns like Command and Template Method, how to write test cases by extending TestCase and using setUp(), tearDown(), and assert methods. It also compares differences between JUnit 3.x and 4.x versions, with 4.x using annotations like @Before, @After, @Test instead of overriding methods. Code examples are provided to illustrate how test cases are designed and executed with both versions.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views3 pages

Junit - A Standard Unit Testing Tool For Java Programs: Testresult

The document summarizes JUnit, a standard unit testing framework for Java. It describes the key aspects of JUnit including its open source availability, core classes like TestResult and TestSuite, use of design patterns like Command and Template Method, how to write test cases by extending TestCase and using setUp(), tearDown(), and assert methods. It also compares differences between JUnit 3.x and 4.x versions, with 4.x using annotations like @Before, @After, @Test instead of overriding methods. Code examples are provided to illustrate how test cases are designed and executed with both versions.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

JUnit a standard unit testing tool for Java programs Open source and documentation available at http://www.junit.

t.org [1,2] describe the Junit 3.x framework by using the following class diagram:
TestResult
<<Interface>>

Assert

Test *

TestCase setUp() runTest() tearDown()

TestSuite

The design of JUnit is based on the following patterns: o Command (test cases are encapsulated and manipulated as objects) o Composite (a TestSuite is a composite of TestCases) o Template Method (tests are based on a template sequence of operations: each test method is run between a call to setUp() and a call to tearDown()) To create a test-case class you have to define a subclass of TestCase. o Each test-case class may contain several test methods. o Each test method is run in a fresh instance of the test-case class. o The test-case class may override setUp() and tearDown(). setUp() and tearDown() provide control over each test fixture (the set of objects that are involved in a test). o Assert provides (static) methods for checking the test values. You can write and execute collections of tests assembled in test suites. The new versions JUnit 4.x use annotations (require JDK 5 to run): @Before, @After, @Test (for set up, tear down and test methods, respectively). References [1] Kent Beck. JUnit pocket guide. OReilly, 2004. [2] JUnit A Cooks Toor (available from http://www.junit.org)

Differences between JUnit 3.x and JUnit 4.x


// C.java // Application class under test. // Normally, you don't need to test // very simple methods like set() or get(). // This example is only considered // for the sake of simplicity. // You can find more complex examples at // http://www.junit.org public class C { private int i; private String s; public int getI() { return this.i; } public String getS() { return this.s; } public void setI(int ival) { this.i = ival; } public void setS(String sval) { this.s = sval; } } Test cases designed with JUnit 3.x // CTest1.java import junit.framework.TestCase; public class CTest1 extends TestCase { private C c; // Creates a test case and sets its // name to arg. // Since JUnit 3.8.1 you // don't need to override this // constructor in subclasses // of TestCase. public CTest(String arg) { super(arg); } public void setUp() { c = new C(); } // Each test method is run between // a call to setup() and a // call to tearDown(). // Each test method is run in a fresh // instance of the test-case class. public void testI() { int j = 10; c.setI(j); assertEquals(c.getI(),j); } public void testS() { String t = "str"; c.setS(t); assertEquals(c.getS(),t); } > java junit.textui.TestRunner CTest1 .. Time: 0.02 OK (2 tests) Test cases designed with JUnit 4.x // CTest1.java import static org.junit.Assert.assertEquals; import org.junit.Before; import org.junit.Test; public class CTest1 { private C c; @Before public void setUp() { c = new C(); } @Test public void testI() { int j = 10; c.setI(j); assertEquals(c.getI(),j); } @Test public void testS() { String t = "str"; c.setS(t); assertEquals(c.getS(),t); } > java org.junit.runner.JUnitCore CTest1 JUnit version 4.2 .. Time: 0.09 OK (2 tests)

// // // // //

} // // // //

Visualizing the execution of setup() and teardown() Test cases designed with JUnit 3.x Test cases designed with JUnit 4.x // CTest2.java // CTest2.java import junit.framework.TestCase; import static org.junit.Assert.assertEquals; import org.junit.Before; public class CTest2 extends TestCase { import org.junit.After; private C c; import org.junit.Test; public CTest2(String str) { super(str); } public void setUp() { System.out.println("setUp()"); c = new C(); } public void testI() { int j = 10; c.setI(j); assertEquals(c.getI(),j); } public void testS() { String t = "str"; c.setS(t); assertEquals(c.getS(),t); } public void tearDown() { System.out.println("tearDown()"); } } // // // // // // // // > java junit.textui.TestRunner CTest2 .setUp() tearDown() .setUp() tearDown() Time: 0.03 OK (2 tests) public class CTest2 { private C c; @Before public void setUp() { System.out.println("setUp()"); c = new C(); } @Test public void testI() { int j = 10; c.setI(j); assertEquals(c.getI(),j); } @Test public void testS() { String t = "str"; c.setS(t); assertEquals(c.getS(),t); } @After public void tearDown() { System.out.println("tearDown()"); } } // // // // // // // // // > java org.junit.runner.JUnitCore CTest2 JUnit version 4.2 .setUp() tearDown() .setUp() tearDown() Time: 0.11 OK (2 tests)

Additional information 1. 2. 3. 4. http://www.junit.org Kent Beck. JUnit pocket guide. OReilly, 2004. Gunjan Doshi. JUnit 4.0 in 10 minutes (available from [1]) Elliote Harold. An early look at JUnit 4 (available from [1])

You might also like