Java Tutorial/JUnit/TestCase

Материал из Java эксперт
Версия от 17:44, 31 мая 2010; (обсуждение)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

extends TestCase

import junit.framework.*;
public class TestSimple extends TestCase {
  public TestSimple(String name) {
    super(name);
  }
  public void testAdd() {
    assertEquals(2, 1+1);
  }
}





Init class being tested in test case method

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
public class TestClassTwo extends TestCase {
  public TestClassTwo(String method) {
    super(method);
  }
  public void testLongRunner() {
    TSP tsp = new TSP();
    assertEquals(2300, tsp.shortestPath(50));
  }
  public void testShortTest() {
    TSP tsp = new TSP();
    assertEquals(140, tsp.shortestPath(5));
  }
  public void testAnotherShortTest() {
    TSP tsp = new TSP();
    assertEquals(586, tsp.shortestPath(10));
  }
  public static Test suite() {
    TestSuite suite = new TestSuite();
    // Only include short tests
    suite.addTest(new TestClassTwo("testShortTest"));
    suite.addTest(new TestClassTwo("testAnotherShortTest"));
    return suite;
  }
}
class TSP {
  public int shortestPath(int numCities) {
    switch (numCities) {
    case 50:
      return 2300;
    case 5:
      return 140;
    case 10:
      return 586;
    }
    return 0;
  }
}





JUnit Test Composition

import junit.framework.*;
public class TestClassOne extends TestCase {
  public TestClassOne(String method) {
    super(method);
  }
  public void testAddition() {
    assertEquals(4, 2+2);
  }
  public void testSubtraction() {
    assertEquals(0, 2-2);
  }
}





Per-Test Setup and Tear-Down

import java.sql.Connection;
import junit.framework.TestCase;
public class TestDB extends TestCase {
  private Connection dbConn;
  protected void setUp() {
    // connect to database
  }
  protected void tearDown() {
    // disconnect from database
  }
  public void testAccountAccess() {
  }
  public void testEmployeeAccess() {
  }
}





Test algorithm

import junit.framework.TestCase;
public class TestLargest extends TestCase {
  public TestLargest(String name) {
    super(name);
  }
  public void testOrder() {
    assertEquals(9, Largest.largest(new int[] { 9, 8, 7 }));
    assertEquals(9, Largest.largest(new int[] { 8, 9, 7 }));
    assertEquals(9, Largest.largest(new int[] { 7, 8, 9 }));
  }
  public void testEmpty() {
    try {
      Largest.largest(new int[] {});
      fail("Should have thrown an exception");
    } catch (RuntimeException e) {
      assertTrue(true);
    }
  }
  public void testOrder2() {
    int[] arr = new int[3];
    arr[0] = 8;
    arr[1] = 9;
    arr[2] = 7;
    assertEquals(9, Largest.largest(arr));
  }
}
class Largest {
  public static int largest(int[] list) {
    int index, max = Integer.MAX_VALUE;
    if (list.length == 0) {
      throw new RuntimeException("Empty list");
    }
    for (index = 0; index < list.length - 1; index++) {
      if (list[index] > max) {
        max = list[index];
      }
    }
    return max;
  }
}





Test Skeleton

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
public class TestSkeleton extends TestCase {
  /**
   * Per-method test set up
   */
  public void setUp() {
  }
  /**
   * Per-method test tear down
   */
  public void tearDown() {
  }
  /**
   * Add tests here: public void testName() ...
   */
  public TestSkeleton(String name) {
    super(name);
  }
  /**
   * Default suite method
   */
  public static Test suite() {
    return new TestSuite(TestSkeleton.class);
  }
  /**
   * Note -- "main" will only be run when invoked individually from the command
   * line (not via Ant"s JUnit Task, etc.)
   */
  public static void main(String[] args) {
    TestSuite suite = new TestSuite();
    if (args.length != 0) {
      // Run specific tests as indicated from the
      // command line
      for (int i = 0; i < args.length; i++) {
        suite.addTest(new TestSkeleton(args[i]));
      }
    } else {
      // Dynamically discover all of them, or use
      // user-defined suite
      suite.addTest(TestSkeleton.suite());
    }
    junit.textui.TestRunner.run(suite);
  }
}





Test your stack structure

import junit.framework.TestCase;
public class TestMyStack extends TestCase {
  public void testEmpty() throws Exception {
    MyStack stack = new MyStack();
    stack.checkInvariant();
    stack.push("sample");
    stack.checkInvariant();
    assertEquals("sample", stack.pop());
    stack.checkInvariant();
    stack.delete(1);
    stack.checkInvariant();
  }
}
class MyStack {
  public MyStack() {
    stack = new String[100];
    next_index = 0;
  }
  public String pop() {
    return stack[--next_index];
  }
  public void delete(int n) {
    next_index -= n;
  }
  public void push(String aString) {
    stack[next_index++] = aString;
  }
  public String top() {
    return stack[next_index - 1];
  }
  public void checkInvariant() throws Exception {
    if (!(next_index >= 0 && next_index < stack.length)) {
      throw new Exception("next_index out of range: " + next_index + " for stack length "
          + stack.length);
    }
  }
  private int next_index;
  private String[] stack;
}





Use assertEquals in a test method

import junit.framework.TestCase;
public class TestLargest extends TestCase {
  public TestLargest(String name) {
    super(name);
  }
  public void testAdds() {
    assertEquals(2, 1 + 1);
    assertEquals(4, 2 + 2);
    assertEquals(-8, -12 + 4);
  }
}