1 / 10

Using JUnit

Using JUnit. How we can use JUnit for testing in Bandera By Todd Wallentine (tcw@cis.ksu.edu). Overview. What is JUnit? Example Usage Talking Points References. What is JUnit?. JUnit is a regression testing framework built by Erich Gamma and Kent Beck[1].

dash
Download Presentation

Using JUnit

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Using JUnit How we can use JUnit for testing in Bandera By Todd Wallentine (tcw@cis.ksu.edu)

  2. Overview • What is JUnit? • Example Usage • Talking Points • References

  3. What is JUnit? • JUnit is a regression testing framework built by Erich Gamma and Kent Beck[1]. • It can be used for regression testing as well as for simple Unit testing. • It will not work for regression testing of the GUI components. • Works very well to test methods!

  4. Example • We need to test a class called Math that performs simple • operations (add, subtract, multiply, and divide). It has four • method calls: • add(int a, int b) : int • subtract(int a, int b) : int • multiply(int a, int b) : int • divide(int a, int b) : int • To exercise this class, we will create a JUnit TestCase that • will perform simple tests. • The addition of 1 and 1 will be 2. • The subtraction of 3 from 5 will be 2. • The multiplication of 2 and 2 will be 4. • The division of 10 by 2 will be 5.

  5. Example (continued) To create a JUnit test, we will extend the class TestCase. public class MathTestCase extends TestCase { We will then create tests. Example for add(a, b): public void testAdd1() { int result = Math.add(1, 1); assertTrue(“1 + 1 should be 2.”, result == 2); } We will then instrument the TestCase so that all tests will be run. public static Test suite() { return(new TestSuite(MathTestCase.class)); }

  6. Example – Text Output • ..F..F..F..F • Time: 0.04 • There were 4 failures: • testAdd2(math.test.MathTestCase)junit.framework.AssertionFailedError: • 1 + 1 should equal 3. result = 2 • at java.lang.Throwable.<init>(Compiled Code) • at java.lang.Error.<init>(Compiled Code) • at junit.framework.AssertionFailedError.<init>(Compiled Code) • at math.test.MathTestCase.testAdd2(Compiled Code) • 2) testSubtract2(math.test.MathTestCase)junit.framework.AssertionFailedError: • 5 - 3 should equal 3. result = 2 • at java.lang.Throwable.<init>(Compiled Code) • at java.lang.Error.<init>(Compiled Code) • at junit.framework.AssertionFailedError.<init>(Compiled Code) • at math.test.MathTestCase.testSubtract2(Compiled Code) • … [divide and multiplication errors here] … • FAILURES!!! • Tests run: 8, Failures: 4, Errors: 0

  7. Example – GUI Output

  8. Example – MathTestCase code public void testAdd1() { int result = Math.add(1, 1); assertTrue("1 + 1 should equal 2. result = " + result, result == 2); } public void testAdd2() { int result = Math.add(1, 1); assertTrue("1 + 1 should equal 3. result = " + result, result == 3); } public void testSubtract1() { int result = Math.subtract(5, 3); assertTrue("5 - 3 should equal 2. result = " + result, result == 2); } public void testSubtract2() { int result = Math.subtract(5, 3); assertTrue("5 - 3 should equal 3. result = " + result, result == 3); }

  9. Talking Points • A good practice is to have a TestCase class for each class in a package. • A good practice is to have all TestCase classes in a subpackage, test, for each package. • A good practice is to have an all encompassing TestCase for the whole package to make regression testing easier. • JUnit will integrate into IDEs like VisualAge for Java. • A good practice is to add regression testing to the CVS submisison process so that no code gets submitted on accident that would break the regression tests.

  10. References • http://junit.org • http://junit.sourceforge.net/doc/testinfected/testing.htm • http://www.junit.org/junit/doc/vaj/vaj.htm

More Related