HelloWorld.java
public class HelloWorld {
public String hello() {
return "world";
}
public String world() {
return "hello";
}
public String nil() {
return null;
}
public String notNil() {
return "abc";
}
public String ext() {
return null;
}
}
测试文件
public class TestHello extends TestCase {
private HelloWorld hw;
@Override
protected void setUp() throws Exception {
super.setUp();
hw = new HelloWorld();
System.out.println("helloWorld init");
}
public void testHello() {
String str = hw.hello();
assertEquals("测试world失败", str, "world");
}
public void testWorld() {
String str = hw.world();
assertEquals("测试world失败", str,"hello");
}
public void testNotNil() {
assertNotNull("对象为空", hw.notNil());
}
public void testNil() {
assertNull("对象不为空", hw.nil());
}
public void testExt() {
try {
hw.ext();
fail("没有抛出异常");
} catch (NumberFormatException e) {
}
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
System.out.println("hello world destory");
hw = null;
}
}