13.07.2015 Views

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

13.6 Running Test Suites309The TestRunner will use introspecti<strong>on</strong> and reflecti<strong>on</strong> to dig informati<strong>on</strong>out of the AccountTest class. It will find all the public methods that beginwith test and have no parameters. It will execute setUp(), then <strong>on</strong>e of thetest methods, then tearDown(); then setUp(), then another test method,then tearDown(), and so <strong>on</strong>. Our example has <strong>on</strong>ly <strong>on</strong>e test method,testCreateSub(), so that will be the <strong>on</strong>e test method it runs.The result of running the test should look like this:$ java junit.textui.TestRunner net.multitool.core.AccountTest.Time: 0.071OK (1 test)$13.6RUNNING TEST SUITESQuite likely, you’ll want to run several tests, exercising the various classes thatmake up your applicati<strong>on</strong>. Let’s see an example of how to build such a suite oftests (Example 13.3).While not defined as an interface, the c<strong>on</strong>venti<strong>on</strong> is used by JUnitTestRunner classes that they will look for a public static method calledsuite() in any class that you ask a TestRunner to run. Your class, the <strong>on</strong>ethat will define the suite of tests, should return something that implements theTest interface. A TestSuite object is <strong>on</strong>e such object, and we can fill it withtests gleaned automatically by JUnit from the class names that we supply.We’ve also added a main() that invokes the text-based user interface forrunning these tests. That way you can invoke the tests from the command lineif you like.Here are the two commands to compile and execute the CoreTest suite,using the Swing GUI:$ javac test/net/multitool/core/CoreTest.java$ java junit.swingui.TestRunner net.multitool.core.CoreTestWhen the GUI runs, click <strong>on</strong> the Hierarchy tab and you can see the varioustests that make up the suite. Opening the folders will show the tests insideof suites (Figure 13.4).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!