/** * */ package junit.learnnow; import static org.junit.Assert.*; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; /** * @author Michael Thomas * */ public class MyClassTest { /** * @throws java.lang.Exception */ @BeforeClass public static void setUpBeforeClass() throws Exception { System.out.println("setUpClass() - run once at class start up."); } /** * @throws java.lang.Exception */ @AfterClass public static void tearDownAfterClass() throws Exception { System.out.println("tearDownClass() - run once when class has finished."); } /** * @throws java.lang.Exception */ @Before public void setUp() throws Exception { System.out.println("setUp() - runs before each @test method."); } /** * @throws java.lang.Exception */ @After public void tearDown() throws Exception { System.out.println("tearDown() - runs after each @test method."); } @Test public void test() { //Goal is to eventually call all test methods. System.out.println("running test()"); //fail("Not yet implemented"); } @Test public void testMyMethodInt() { System.out.println("running testMyMethodInt()"); MyClass c1 = new MyClass(); assertEquals(0, c1.myMethodInt(0)); assertEquals(0, c1.myMethodInt(10)); assertEquals(1, c1.myMethodInt(11)); assertEquals(1, c1.myMethodInt(100)); assertEquals(0, c1.myMethodInt(-1)); } @Test public void testMyMethodDouble() { System.out.println("running testMyMethodDouble()"); MyClass c1 = new MyClass(); //Common to add the .001 for decimal delta issues. assertEquals(10.0, c1.myMethodDouble(100.0), .000); assertEquals(10.0, c1.myMethodDouble(100.0), .001); } @Test public void testMyMethodBoolean() { System.out.println("running testMyMethodBoolean()"); MyClass c1 = new MyClass(); assertEquals(true, c1.myMethodBoolean(true)); assertTrue(c1.myMethodBoolean(true)); assertFalse(c1.myMethodBoolean(false)); } @Test public void testMyMethodString() { System.out.println("running 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")); } @Test public void testGoodHouseLoanRate(){ System.out.println("running testGoodHouseLoanRate()"); MyClass c1 = new MyClass(); c1.setHouseLoanRate(3.0); assertTrue(c1.goodHouseLoanRate()); c1.setHouseLoanRate(4.0); assertFalse(c1.goodHouseLoanRate()); } }