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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

13.5 Writing Test Cases303It prints a period for each test that it runs. (Yes, there are 119 periodsthere. Go ahead; count them if you must.) The command-line versi<strong>on</strong> is usefulfor incorporating JUnit tests into shell scripts (e.g., for testing nightly builds,e-mailing the results) and is used by ant when it invokes JUnit.13.5WRITING TEST CASESWriting a test case for your own Java code c<strong>on</strong>sists, at its simplest, of writing anew class for each class that you want to test. But this class that you create isbuilt in a special way so that the test harness of JUnit can execute it. That is,the test case class that you create should meet certain naming c<strong>on</strong>venti<strong>on</strong>s, sothat the JUnit test runners can find what they need in order to run your tests.More specifically, your test cases will extend the JUnit class TestCase.Now, TestCase is an abstract class, meaning there are parts that you have tofill in (i.e., methods that you must write) to make it a working class. Moreover,TestCase implements (in the Java sense of the word) the Test interface. Canyou begin to see how the TestCase class is a framework? It defines the roughoutline of how the test cases will look so that a comm<strong>on</strong> test runner can runany test case, no matter who wrote it.Let’s look at a simple example, to see what such a test case looks like.Example 13.2 shows <strong>on</strong>e for testing our Account class.Example 13.2 Simple test casepackage net.multitool.core;import java.util.*;import net.multitool.util.*;import junit.framework.*;// needed by our class// needed by our class// needed by JUnit/*** for JUnit testing of Account.java*/public classAccountTestextends TestCase{// our test instrumentati<strong>on</strong>:Account base;

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

Saved successfully!

Ooh no, something went wrong!