Test Cases Made Easy with JUnit 4.5
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
0 Comments (click to add your comment)
Networking Solutions



Solid state disks (SSDs) made a splash in consumer technology, and now the technology has its eyes on the enterprise storage market. Download this eBook to see what SSDs can do for your infrastructure and review the pros and cons of this potentially game-changing storage technology.