Title: JUnit and JFCUnit Frameworks
1J-Unit and JFCUnit Frameworks
2Agenda
3What is xUnit?
- An automated unit test framework
- Provides the Driver for unit(s)
- Provides automatic test runs
- Provides automatic result checks
- Available for multiple languages
- cppUnit
- sUnit
- Junit
-
4Junit Terms
- Failure Expected
- Error Unexpected (Exception)
- TestCase Collection of method tests
- Test Fixture Object Reuse for multiple tests
- TestSuite Collection of Test Cases
- TestRunner Interface
- awt, swing,text
5Example, Complex Class
public class Complex int real int
imaginary public Complex(int r, int i)
realr imaginaryi public Complex()
real0 imaginary0 public
boolean equals(Complex c) boolean result
false if ((realc.real)
(imaginaryc.imaginary)) resulttrue
return result public Complex add(Complex
c) Complex result new Complex(c.real()rea
l,c.imaginaryimaginary) return result
public int getReal() return real public
int getImaginary() return imaginary
6Using Junit (Create Fixture)
- public class ComplexTest extends TestCase
- Complex c1
- Complex c2
- protected void setUp()
- c1 new Complex(7,3)
- c2 new Complex(12,6)
-
- protected void tearDown()
-
7Using Junit (Add Test Cases)
- public void testAdd()
- Complex result c1.add(new Complex(5,3))
- assertEquals(result.getReal(),c2.getReal())
- assertEquals(result.getImaginary(),
- c2.get_Imaginary())
-
- public void testEqual()
- assertFalse(c2.equals(c1))
- assertTrue(c1.equals(new Complex(7,3)))
assertNull, assertNotNull, assertSame
8Using Junit (Make Suite)
- import junit.framework.TestCase
- import junit.framework.Test
- import junit.framework.TestSuite
- import junit.textui.TestRunner
- public static Test suite()
- TestSuite suite new TestSuite()
- suite.addTest(new ComplexTest(testAdd))
- suite.addTest(new ComplexTest(testEqual))
- return suite
9Using Junit (Batch Invoke and Constructor)
- public static void main(String args)
- junit.textui.TestRunner.run(suite())
-
- public ComplexTest(String s)
- super(s)
10The Graphical UI
11UI on Failure
12What Junit does not do
- Figure out your tests for you
- Calculate any coverage criteria
- Test GUIs
- Except extensions
- JFCUnit
- Jemmy
- Pounder
- Abbot
13Administrative
- Download latest version (3.8.1) from
- www.junit.org
- Setup classpath to junit.jar
- Include appropriate ui in main
- Import junit.framework. classes
- Already in class resources
- CS2335/java/junit
14JFCUnit
- Unit test extension to JUnit for Swing (JFC)
based UI. - Derive classes from JFCTestCase vs. TestCase
- Can obtain handles to GUI resources
- Can send events to GUI resources
- Takes over AWT thread with JFCTestHelper class
15First make base class
public class MyAppTest extends JFCTestCase
private JFCTestHelper helper private MyApp
public MyAppTest(String s) super(s)
public void setUp() helpernew
JFCTestHelper() MyAppnew MyApp(JFC
Test) public static void main(String
args) TestRunner.run(MyAppTest.class)
16Use helper class to manipulate
Finding stuff JButton exitButton ( JButton )
TestHelper.findNamedComponent(
"ExitButton", loginScreen, 0 )
assertNotNull( "Could not find the Exit button",
exitButton )
17Next Time
- More on Teams..
- Problem Team Members
- MBTI and effect on Development teams