extends TestCase : TestCase « junit.framework « Java by API






extends TestCase

 
import junit.extensions.TestSetup;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

public class Main extends TestCase {


  public static void main (String... args) {
    junit.textui.TestRunner.run (suite());
  }  

  
  public Main(String name) {
    super(name);
  }

  public void testPassNullsToConstructor() {
    try {
      Person p = new Person(null, null);
      fail("Expected IllegalArgumentException because both args are null");
    } catch (IllegalArgumentException expected) {
    }
  }

  public void testNullsInName() {
    fail("sample failure");
    Person p = new Person(null, "lastName");
    assertEquals("lastName", p.getFullName());

    p = new Person("Tanner", null);
    assertEquals("Tanner ?", p.getFullName());
  }

  public static void oneTimeSetup() {
    System.out.println("oneTimeSetUp");
  }

  public static void oneTimeTearDown() {
    System.out.println("oneTimeTearDown");
  }

  public static Test suite() {
    TestSetup setup = new TestSetup(new TestSuite(Main.class)) {
      protected void setUp() throws Exception {
        oneTimeSetup();
      }

      protected void tearDown() throws Exception {
        oneTimeTearDown();
      }
    };
    return setup;
  }
}

class Person {
  private String firstName;

  private String lastName;

  public Person(String firstName, String lastName) {
    if (firstName == null && lastName == null) {
      throw new IllegalArgumentException("Both names cannot be null");
    }
    this.firstName = firstName;
    this.lastName = lastName;
  }

  public String getFullName() {
    String first = (this.firstName != null) ? this.firstName : "?";
    String last = (this.lastName != null) ? this.lastName : "?";

    return first + " " + last;
  }

  public String getFirstName() {
    return this.firstName;
  }

  public String getLastName() {
    return this.lastName;
  }
}

   
  








Related examples in the same category

1.Assert.fail(String message)
2.Assert: assertEquals(Object expected, Object actual)
3.Assert: assertEquals(String expected, String actual)
4.Assert: assertEquals(String message, double expected, double actual, double precision)
5.Assert: assertEquals(int expected, int actual)
6.Assert: assertFalse(String message, boolean value)
7.Assert: assertNotSame(String message, Object expected, Object actual)
8.Assert: assertNull(String message, Object value)
9.Assert: assertSame(String message, Object expected, Object actual)
10.Assert: assertTrue(String message, boolean value)