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



I looked for a main before posting. Apparently not carefully enough.


You certainly aren't alone if you think the example that I posted was a bit longer than absolutely necessary; I simply grabbed one of the last testcases that I pulled out of a bug report and transformed into a JUnit test. More than one person has asked for a simpler example showing the flavor of a JUnit test. So...

Try the following:

1) Download JUnit, grab "junit.jar" and put it in your classpath.
(See www.junit.org or junit.sourceforge.net)
(I just stick junit.jar in /Library/Java/Extensions/ because I use it so much)
2) Cut and paste the file below into someplace convenient like /tmp/TrivialTest.java
3) cd /tmp/
4) javac TrivialTest.java
5) java TrivialTest

To see what a failure looks like, change the sense of one of the asserts so that it says myPi > 4.




/*
* File : TrivialTest.java
*/

import java.lang.Math;
import junit.framework.*;


public class TrivialTest extends TestCase {
protected double myPi;

protected void setUp() {
myPi = Math.PI;
}

public void testPI() {
assertTrue("Pi bigger than 3", myPi > 3);
assertTrue("Pi smaller than 4", myPi < 4);
}

// Boilerplate below so it can be run from the command line

public static Test suite() {
return new TestSuite(TrivialTest.class);
}

public static void main (String[] args) {
junit.textui.TestRunner.run(suite());
}
}
_______________________________________________
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.


References: 
 >JUnit and Testcases (From: Michael McDougall <email@hidden>)
 >Re: JUnit and Testcases (From: Timothy Wall <email@hidden>)



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.