JUnit is a simple framework towrite repeatable tests. It is an instance of the xUnit architecture for unittesting frameworks.
Using JUnit you cancheaply and incrementally build a test suite that will help you measure yourprogress, spot unintended side effects, and focus your development efforts.
The last step is how to run yourJUnit tests. You can do this several ways, including your command line, Eclipseor the JUnit Test Runner, but I like plain Ant. To run a Junit test with an Antscript, add this to your Ant script:
<junit printsummary="yes"haltonfailure="yes" showoutput="yes" >
<classpath>
<pathelementpath="${build}"/>
</classpath>
<batchtestfork="yes" todir="${reports}/raw/">
<formattertype="xml"/>
<filesetdir="${src}">
<includename="**/*Test*.java"/>
</fileset>
</batchtest>
</junit>
Ant will also do nice things like create nice HTML reports for you! I've linkedto a simple Ant script that will compile all the code in your "src"folder, run all the tests named "test*" and then create an HTMLreport in your "reports\html" folder. Just type "ant test".
Prerequisites :
In order to use this tutorial you will need tohave a Java compiler (http://java.sun.com/),Ant (http://ant.apache.org/),and JUnit (http://junit.org/)installed. You will also need JUnit.jar in your Ant's library folder(ANT_HOME/lib).
Example:
class Money {
private int fAmount;
private String fCurrency;
public Money(int amount, String currency) {
fAmount= amount;
fCurrency= currency;
}
public int amount() {
return fAmount;
}
public String currency() {
return fCurrency;
}
}
Adda New Method to the Class
• When you add two Moneys of thesame currency, the resulting Money has as its amount the sum of the other twoamounts.
public Money add(Money m) {
return new Money(amount()+m.amount(),currency());
}
How to Use JUnit to Write Test Case
• You implement a test in asubclass of TestCase. To test our Money implementation we therefore defineMoneyTest as a subclass of TestCase. In Java, classes are contained in packagesand we have to decide where to put MoneyTest. Our current practice is to putMoneyTest in the same package as the classes under test. In this way a testcase has access to the package private methods. We add a test methodtestSimpleAdd, that will exercise the simple version of Money.add() above. AJUnit test method is an ordinary method without any parameters.
The Test Case (Test Method)
public class MoneyTest extends TestCase {
//…
public void testSimpleAdd() {
Money m12CHF= new Money(12, "CHF");
// (1)
Money m14CHF= new Money(14, "CHF");
Money expected= new Money(26, "CHF");
Money result= m12CHF.add(m14CHF);
// (2)
Assert.assertTrue(expected.equals(result));
// (3)
}
}
Analysisof the Test Case
The testSimpleAdd() test caseconsists of:
1. Code which creates theobjects we will interact with during the test. This testing context is commonlyreferred to as a test's fixture. All we need for the testSimpleAdd testare some Money objects.
2. Code which exercises theobjects in the fixture.
3. Code which verifies the result.
Run the Test Case
The last step is how to run yourJUnit tests. You can do this several ways, including your command line, Eclipseor the JUnit Test Runner, but I like plain Ant. To run a Junit test with an Antscript, add this to your Ant script:
<junit printsummary="yes"haltonfailure="yes" showoutput="yes" >
<classpath>
<pathelementpath="${build}"/>
</classpath>
<batchtestfork="yes" todir="${reports}/raw/">
<formattertype="xml"/>
<filesetdir="${src}">
<includename="**/*Test*.java"/>
</fileset>
</batchtest>
</junit>
Ant will also do nice things like create nice HTML reports for you! I've linkedto a simple Ant script that will compile all the code in your "src"folder, run all the tests named "test*" and then create an HTMLreport in your "reports\html" folder. Just type "ant test".
0 Comments:
Post a Comment