dcsimg
September 21, 2018
Hot Topics:

Test Cases Made Easy with JUnit 4.5

  • November 12, 2008
  • By David Thurmond
  • Send Email »
  • More Articles »

Writing a Test Case

Listing 2 shows a set of JUnit 4.5 test cases for NumberCruncher.java. Listing 2.1 shows some of the equivalent test cases using JUnit 3.8.

Listing 2: NumberCruncherTestCase45.java

package com.dlt.developer.junit;

import com.dlt.developer.numbers.NumberCruncher;

import org.junit.*;

import static org.junit.Assert.*;

public class NumberCruncherTestCase45 {

   @BeforeClass
   public static void init() {
        // Do one-time setup for all test cases
   }    // init()

   @Before
   public void doSetup() {
        // Do setup before each test case
   }    // setUp()

   @After
   public void doTearDown() {
        // Do tear down after each test case
   }    // tearDown()

   @AfterClass
   public static void destroy() {
        // Do tear-down after all test cases are finished
   }    // destroy()

   @Test
   public void testIsPrime() {
      NumberCruncher nc = new NumberCruncher();
      assertTrue("17 is a prime number",
                 nc.isPrime(new Integer(17)));
      assertFalse("16 is not a prime number",
                  nc.isPrime(new Integer(16)));
   }    // testIsPrime()

   @Test
   public void testFactorial() {
      NumberCruncher nc = new NumberCruncher();
      assertEquals(new Integer(120), nc.factorial(new Integer(5)));
   }    // testFactorial()

   @Test (expected = IllegalArgumentException.class)
   public void testFactorialInvalid() {
      NumberCruncher nc = new NumberCruncher();
      nc.factorial(new Integer(-4));
   }    // testFactorialInvalid()

   @Test (timeout = 5000)
   public void testDoForever() {
      NumberCruncher nc = new NumberCruncher();
      assertEquals(new Integer(0), nc.doForever());
   }    // testDoForever()

   @Test
   public void testGetFactors() {
      NumberCruncher nc = new NumberCruncher();
      Object[] f1 = nc.getFactors(new Integer(32));
      Object[] f2 = nc.getFactors(new Integer(32));
      assertEquals("Factors are equal", f1, f2);
   }    // testGetFactors()

   @Ignore ("Not ready yet")
   public void testSomethingElse() {
      NumberCruncher nc = new NumberCruncher();
   }    // testSomethingElse()

} // NumberCruncherTestCase45

Listing 2.1: NumberCruncherTestCase38.java

package com.dlt.developer.junit;

import com.dlt.developer.numbers.NumberCruncher;
   import junit.framework.TestCase;

public class NumberCruncherTestCase38 extends TestCase {


   public void setup() {
        // Do setup before each test case
   }    // setUp()

   public void tearDown() {
        // Do tear down after each test case
   }    // tearDown()


   public void testIsPrime() {
      NumberCruncher nc = new NumberCruncher();
      assertTrue("17 is a prime number",
                  nc.isPrime(new Integer(17)));
         assertFalse("16 is not a prime number",
                     nc.isPrime(new Integer(16)));
   }    // testIsPrime()

   public void testFactorial() {
      NumberCruncher nc = new NumberCruncher();
      assertEquals(new Integer(120), nc.factorial(new Integer(5)));
   }    // testFactorial()

   public void testFactorialInvalid() {
      NumberCruncher nc = new NumberCruncher();
      boolean gotException = false;
      try {
         nc.factorial(new Integer(-4));
      } catch (IllegalArgumentException ex) {
         gotException = true;
      }    // catch
      assertTrue("Factorial Validation", gotException);
   }    // testFactorialInvalid()

   public void testGetFactors() {
      NumberCruncher nc = new NumberCruncher();
      Object[] f1 = nc.getFactors(new Integer(32));
      Object[] f2 = nc.getFactors(new Integer(32));
      assertTrue("Factor Arrays Equal Length",
                 f1.length == f2.length);
      for (int i = 0; i < f1.length; i++) {
         assertTrue("Array Element " + i + " Is Equal",
                    f1[i].equals(f2[i]));
      }    // for i
   }    // testGetFactors()

}    // NumberCruncherTestCase38




Page 2 of 5



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

By submitting your information, you agree that developer.com may send you developer offers via email, phone and text message, as well as email offers about other products and services that developer believes may be of interest to you. developer will process your information in accordance with the Quinstreet Privacy Policy.

Sitemap

Thanks for your registration, follow us on our social networks to keep up-to-date