package junit.learnnow; import java.io.IOException; import junit.framework.TestCase; public class MyClassTest extends TestCase { public static void setUpClass() throws Exception { // WARNING: Doesn't work in JUnit3 // Code executed before the first test method in the JUnit is run. System.out.println("setUpClass()"); } public static void tearDownClass() throws Exception { // WARNING: Doesn't work in JUnit3 // Code executed after the last test method in the JUnit is run. System.out.println("tearDownClass()"); } public void setUp() throws Exception { // Code executed before each test System.out.println("setUp()"); } public void tearDown() throws Exception { // Code executed after each test System.out.println("tearDown()"); } public void testPlay() { /* Warning: * For objects it uses the equals() method to look for equality! */ System.out.println("in testPlay()");; MyClass c1 = new MyClass(); MyClass c2 = c1; assertEquals(1, 2-1); //interger assertEquals(10.0, 10.0, .001); //double - using common delta value of .001 String myString = "JUnit Hello World"; assertEquals("JUnit Hello World", myString); //Better: Same as the above but giving a description of the test. assertEquals("Testing the creationg of myString","JUnit Hello World", myString); assertSame(c1, c2); //for Objects only! assertTrue(true); assertFalse(false); assertNull(null); assertNotNull(c1); //Uncomment next line and test! Look at the error. //assertEquals("Ooops", myString); //Used for stubbed out tests before you know what to test for. //fail("Not yet implemented"); } public void testMyMethodInt() { MyClass c1 = new MyClass(); assertEquals(0, c1.myMethodInt(-1)); assertEquals(0, c1.myMethodInt(0)); assertEquals(0, c1.myMethodInt(10)); assertEquals(1, c1.myMethodInt(11)); //Next line will error out. //assertEquals(1, c1.myMethodInt(0)); } public void testMyMethodDouble() { MyClass c1 = new MyClass(); assertEquals(10.0, c1.myMethodDouble(100.0), .000); assertEquals(10.0, c1.myMethodDouble(100.0), .001); //Common to add the .001 for decimal delta issues. } public void testMyMethodBoolean() { MyClass c1 = new MyClass(); assertEquals(true, c1.myMethodBoolean(true)); assertTrue(c1.myMethodBoolean(true)); assertFalse(c1.myMethodBoolean(false)); } public void testMyMethodString() { MyClass c1 = new MyClass(); //Test all the situations! assertEquals("You won!", c1.myMethodString("Hello World")); assertEquals("No", c1.myMethodString("Hello")); assertEquals(null, c1.myMethodString(null)); //Example using the assertNull & assertNotNull methods. assertNull(c1.myMethodString(null)); assertNotNull(c1.myMethodString("Hello World")); } public void testGoodHouseLoanRate() { MyClass myClassObj1 = new MyClass(); myClassObj1.setHouseLoanRate(3.0); assertTrue(myClassObj1.goodHouseLoanRate()); myClassObj1.setHouseLoanRate(4.0); assertFalse(myClassObj1.goodHouseLoanRate()); } public void testDisplayFile() { MyClass myClassObj1 = new MyClass(); String myInfo = null; try { myInfo = myClassObj1.displayFile("BooBoo.txt"); fail("File was supposed to not exist!"); } catch (IOException e) { //The string should be null because the file should not exit! assertNull(myInfo); //Note: If we expected the file to be there then we could test this way: //fail("File should have existed!\n" + e.getMessage()); } } }