Przykład:
"Calculator.java"
public class Calculator
{
public double add(double number1, double number2)
{
return number1 + number1;
}
public double mul(double number1, double number2)
{
return number1 * number2;
}
}
import junit.framework.TestCase;
public class TestCalculator extends TestCase
{
public void testAdd()
{
Calculator calculator = new Calculator();
double result = calculator.add(10, 50);
assertEquals(60.0, result);
}
public void testMul()
{
Calculator calculator = new Calculator();
double result = calculator.mul(10, 17);
assertEquals(170.0, result);
}
}
Przykład:
public class Counter {
protected int number;
protected boolean countingIsStoped;
public Counter()
{
countingIsStoped = false;
number = 0;
}
public int getNumber()
{
return number;
}
public void next()
{
if(!countingIsStoped)
number++;
}
public void stopCounting()
{
countingIsStoped = true;
}
}
"TestCounter.java"
import junit.framework.TestCase;
import junit.framework.Test;
import junit.framework.TestSuite;
public class TestCounter extends TestCase {
public TestCounter(String method)
{
super(method);
}
public void testNext()
{
Counter counter = new Counter();
counter.next();
int prev = counter.getNumber();
counter.next();
assertEquals(prev + 1, counter.getNumber());
}
public void testStopCounting()
{
Counter counter = new Counter();
int prev = counter.getNumber();
counter.stopCounting();
assertEquals(prev, counter.getNumber());
}
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTest(new TestCounter("testNext"));
suite.addTest(new TestCounter("testStopCounting"));
return suite;
}
}
import junit.framework.TestCase;
import junit.framework.Test;
import junit.framework.TestSuite;
public class MyTestSuite extends TestCase {
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTestSuite(TestCounter.class);
//suite.addTest(TestCounter.suite());
suite.addTest(new TestCalculator("testAdd"));
suite.addTest(new TestCalculator("testMul"));
return suite;
}
}
1. oneTimeSetUp() 2. setUp() 3. testMethod1() 4. tearDown() 5. setUp() 6. testMethod2() 7. tearDown() 8. oneTimeTearDown()
public class TestDB extends TestCase {
private Connection dbConn;
protected void setUp() {
dbConn = new Connection("oracle", 1521, "fred", "foobar");
dbConn.connect();
}
protected void tearDown() {
dbConn.disconnect();
dbConn = null;
}
public void testAccountAccess() { // Uses dbConn
xxx xxx xxxxxx xxx xxxxxxxxx;
xx xxx xxx xxxx x xx xxxx;
}
public void testEmployeeAccess() { // Uses dbConn
xxx xxx xxxxxx xxx xxxxxxxxx;
xxxx x x xx xxx xx xxxx;
}
}
<junit printsummary="yes" haltonerror="yes" haltonfailure="no" fork="yes" showoutput="no">
<formatter type="plain" usefile="no"/>
<formatter type="xml"/>
<test name="MyTestSuite" todir="${report.dir}" />
<classpath>
<pathelement location="${buil.dir}"/>
</classpath>
</junit>
<junitreport todir="${report.dir}">
<fileset dir="${report.dir}">
<include name="TEST-*.xml"/>
</fileset>
<report todir="${report.dir}/html"/>
</junitreport>