Junit4,与3.x有所区别
1. 测试类不需要extends TestCase,代之以annotation,即测试类(不需要extends TestCase)的方法只要有@Test就可以了
2. 测试方法命名不需要一定要以test开头
可以用eclipse自动生成测试类,选中要测试的类,然后new -junit test case
测试类:
测试结果如下
为了进行TestSuit,再增加一个testcase
==================================================================================
增加一个TestSuit(使用ecilpse的new -junit Test Suit只能找到3.x风格的extends TestCase的测试类,不知为何)
TestSuit测试结果
以下是控制台输出信息,跟测试类的各种annotation有关
=====================================================================================
1. 测试类不需要extends TestCase,代之以annotation,即测试类(不需要extends TestCase)的方法只要有@Test就可以了
2. 测试方法命名不需要一定要以test开头
package
junit;
public class Unit {
private int data;
public Unit(int data){
this.data = data;
}
public boolean test(){
return true;
}
public boolean equals(Object o){
if(o instanceof Unit){
return ((Unit)o).data == data;
}
return false;
}
public void exception(){
if (this.data==0)
throw new IndexOutOfBoundsException("exception in Unit");
}
}
public class Unit {
private int data;
public Unit(int data){
this.data = data;
}
public boolean test(){
return true;
}
public boolean equals(Object o){
if(o instanceof Unit){
return ((Unit)o).data == data;
}
return false;
}
public void exception(){
if (this.data==0)
throw new IndexOutOfBoundsException("exception in Unit");
}
}
可以用eclipse自动生成测试类,选中要测试的类,然后new -junit test case
测试类:
package
junit;
import org.junit. * ;
public class UnitTest {
private Unit unit1 = new Unit(1);
private Unit unit2 = new Unit(2);
@BeforeClass
public static void setUpBeforeClass() throws Exception {
System.out.println("setUpBeforeClass");
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
System.out.println("tearDownAfterClass");
}
@Before
public void setUp() throws Exception {
System.out.println("setUp");
}
@After
public void tearDown() throws Exception {
System.out.println("tearDown");
}
@Test
public void notestTest() { // 方法名不以test开头
//fail("Not yet implemented");
Assert.assertTrue(unit1.test());
}
@Test
public void testEqualsObject() {
//fail("Not yet implemented");
Assert.assertEquals(unit1, new Unit(1));
Assert.assertEquals(unit1, unit2);
}
@Test(expected = IndexOutOfBoundsException.class)
public void testException() {
//fail("Not yet implemented");
new Unit(0).exception();
}
}
import org.junit. * ;
public class UnitTest {
private Unit unit1 = new Unit(1);
private Unit unit2 = new Unit(2);
@BeforeClass
public static void setUpBeforeClass() throws Exception {
System.out.println("setUpBeforeClass");
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
System.out.println("tearDownAfterClass");
}
@Before
public void setUp() throws Exception {
System.out.println("setUp");
}
@After
public void tearDown() throws Exception {
System.out.println("tearDown");
}
@Test
public void notestTest() { // 方法名不以test开头
//fail("Not yet implemented");
Assert.assertTrue(unit1.test());
}
@Test
public void testEqualsObject() {
//fail("Not yet implemented");
Assert.assertEquals(unit1, new Unit(1));
Assert.assertEquals(unit1, unit2);
}
@Test(expected = IndexOutOfBoundsException.class)
public void testException() {
//fail("Not yet implemented");
new Unit(0).exception();
}
}
测试结果如下
为了进行TestSuit,再增加一个testcase
package
junit;
public class Unit2 {
public boolean test(){
return true;
}
}
public class Unit2 {
public boolean test(){
return true;
}
}
package
junit;
import org.junit.Test;
public class Unit2Test {
@Test
public void testTest() {
//fail("Not yet implemented");
org.junit.Assert.assertTrue(new Unit2().test());
}
}
import org.junit.Test;
public class Unit2Test {
@Test
public void testTest() {
//fail("Not yet implemented");
org.junit.Assert.assertTrue(new Unit2().test());
}
}
==================================================================================
增加一个TestSuit(使用ecilpse的new -junit Test Suit只能找到3.x风格的extends TestCase的测试类,不知为何)
package
junit;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite. class )
@SuiteClasses( { UnitTest.class, Unit2Test.class } )
public class Suit {
}
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite. class )
@SuiteClasses( { UnitTest.class, Unit2Test.class } )
public class Suit {
}
TestSuit测试结果
以下是控制台输出信息,跟测试类的各种annotation有关
=====================================================================================