Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: JUnit and Testcases



Ufff! Tough crowd.

Admirable goal to get users to submit bug reports with jUnit test cases included (when possible).

I added some comments from the jUnit docs to your example, which makes it hideously long.

- Craig

/**
File: TrivialTest.java -- a jUnit test fixture

A test fixture is useful if you have two or more tests for a
common set of objects. Using a test fixture avoids duplicating
the test code necessary to initialize and cleanup those common
objects for each test.

Note: Prior to compiling, download jUnit from www.junit.org
and install junit.jar in /Library/Java/Extensions/:

sudo cp /Users/me/Downloads/junit3.8.1/junit.jar /Library/Java/Extensions/

To run the test with the textual test runner used in main():

java directory.TrivialTest

from outside the package directory. For this example:

java TrivialTest

To run the test with the Swing GUI test runner:

java junit.swingui.TestRunner directory.TrivialTest

from outside the package directory. For this example:

java junit.swingui.TestRunner TrivialTest

A quick reference card for jUnit:

http://www.digilife.be/quickreferences/QRC/ JUnit%20Quick%20Reference.pdf
*/

//package directory // Not used in this example

import junit.framework.*;

/**
A jUnit test fixture must extend class TestCase
*/
public class TrivialTest extends TestCase
{
/**
The objects we're testing on
*/
// protected Object obj;
protected double myPi;

/**
Constructor -- required for old versions of jUnit
*/
public TrivialTest(String name)
{
super(name);
}

/**
Initialization -- set up testing scaffold
(create objects/variables to test on)
*/
protected void setUp()
{
// obj = new Object();
myPi = Math.PI;
}

/**
Cleanup -- destroy testing scaffold
(release any permanent resources allocated in setUp)
*/
protected void tearDown()
{
// obj = null;
myPi = 0d; // Dumb, usually close files and nullify references
}

/**
Write a suite() method that uses reflection to
dynamically create a test suite which contains
all the testSomething() methods.
*/
public static Test suite()
{
return new TestSuite(TrivialTest.class);
}

/**
Write a main() method to conveniently run
the test with the textual test runner.
*/
public static void main (String[] args)
{
junit.textui.TestRunner.run(suite());
}

/**
T e s t s -- Method names must begin with the prefix "test"
*/

// Test 1.
public void testRangeOfPI()
{
// assertTrue(String failureMessage, boolean condition);
assertTrue("\n\t-- Pi bigger than 3", myPi > 3);
assertTrue("\n\t-- Pi smaller than 4", myPi < 4);

// assertFalse (String failureMessage, boolean condition);
// assertEquals (String failureMessage, expected, actual);
// assertNull (String failureMessage, object);
// assertNotNull(String failureMessage, object);
// assertSame (String failureMessage, expected, actual);
// assertNotSame(String failureMessage, expected, actual);
// fail (String failureMessage);
// etc.
}

// Test 2.
public void testValueOfPI()
{
// Uncomment following line to deliberately blow test
// myPi = 3.151;

// Use delta difference forms for floating point comparisons!
// assertEquals(String failureMessage, expected, actual, delta);
assertEquals("\n\t-- Pi not even close!", 3.14d, myPi, .01d);
}

// Test 3. ...

/**
E n d o f t e s t s
*/
}
_______________________________________________
java-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/java-dev
Do not post admin requests to the list. They will be ignored.




Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.