Set JUnit Test case fail information : Unit Test « Development Class « Java






Set JUnit Test case fail information

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

public class MainClass extends TestCase {


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

  
  public MainClass(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(MainClass.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;
  }
}


           
         
  








JUint-FailInformation.zip( 98 k)

Related examples in the same category

1.JUnit assertEquals: Float With Delta
2.Assert equals: int
3.Assert equals: Long
4.JUnit assertEquals With Message
5.JUnit assertTrue
6.JUnit assertTrue: ObjectArray
7.Before annotation
8.JUnit BeforeClass
9.JUnit Extends TestCase
10.JUnit Ignore
11.Simple test with JUnit
12.JUnit Test Case With Expected Exception
13.JUnit Test Setup
14.Simple use of JUnit to test ArrayListSimple use of JUnit to test ArrayList
15.Debug frameDebug frame
16.Error HandlerError Handler
17.Redirect or reassign some standard descriptors
18.Utilities for debugging
19.Testing class ClassTesting class Class
20.Simple utility for testing program outputSimple utility for testing program output
21.Assertion tool for debugging
22.Simple DebuggingSimple Debugging
23.Random data for test
24.Demonstration of Design by Contract (DBC) combined with white-box unit testingDemonstration of Design by Contract (DBC) combined with white-box unit testing