Java by API/junit.textui/a
TestRunner: run(Test test)
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;
}
}