现在的位置: 首页 > 综合 > 正文

junit3与junit4的区别(转)

2016年07月13日 ⁄ 综合 ⁄ 共 5693字 ⁄ 字号 评论关闭

Junit4最大的亮点就是引入了注解(annotation),通过解析注解就可以为测试提供相应的信息,抛弃junit3使用命名约束以及反射机制的方法。

/**

 * 被测试类

 */

package com.stock.finance.service;

 

import java.util.List;

import java.util.zip.DataFormatException;

import com.stock.finance.db.dao.TableCompanyDAO;

import com.stock.finance.db.model.TableCompany;

 

/**

*

 */

public class CompanyService {

    private TableCompanyDAO dao = new TableCompanyDAO();

   

    public CompanyService(){

    }

    /**

     * @param code

     * @param name

     * @param masterBusiness

     */

    public void insert(String code,String name,String masterBusiness){

        TableCompany company = new TableCompany(code, name);

        company.setMasterBusiness(masterBusiness);

        insert(company);

    }

    /**

     * @param company

     */

    public void insert(TableCompany company){

        dao.save(company);

    }

    /**

     * @return

     */

    public int companyNum(){

        List<?> list = dao.findAll();

        return list.size();

    }

   

    public void justForDisplay() throws DataFormatException{

        throw new DataFormatException();

    }

}

/**

 * junit3测试类

 */

package test.com.stock.finance.service;

import java.util.zip.DataFormatException;

import com.stock.finance.service.CompanyService;

import junit.framework.TestCase;

/**

 */

public class Tester3 extends TestCase {

         private CompanyService service = new CompanyService();

         protected void setUp() throws Exception {

                   super.setUp();

         }

         protected void tearDown() throws Exception {

                   super.tearDown();

         }

         public final void testInsertStringStringString() {

                   fail("Not yet implemented"); // TODO

         }

         public final void testInsertTableCompany() {

                   fail("Not yet implemented"); // TODO

         }

         public final void testCompanyNum() {

                   fail("Not yet implemented"); // TODO

         }

         public final void testJustForDisplay() {

                   try {

                            service.justForDisplay();

                   } catch (DataFormatException e) {

                            assertTrue("捕获异常正确", true);

                   }

         }

}

/**

 * junit4测试类

 */

package test.com.stock.finance.service;

//静态引用

import static org.junit.Assert.*;

 

import java.util.zip.DataFormatException;

 

import org.junit.After;

import org.junit.AfterClass;

import org.junit.Before;

import org.junit.BeforeClass;

import org.junit.Ignore;

import org.junit.Test;

 

import com.stock.finance.service.CompanyService;

 

/**

*

 */

public class Tester4 {

         private CompanyService service = new CompanyService();

         /**

          * @throws java.lang.Exception

          */

         @BeforeClass

         public static void setUpBeforeClass() throws Exception {

         }

         /**

          * @throws java.lang.Exception

          */

         @AfterClass

         public static void tearDownAfterClass() throws Exception {

         }

         /**

          * @throws java.lang.Exception

          */

         @Before

         public void setUp() throws Exception {

         }

         /**

          * @throws java.lang.Exception

          */

         @After

         public void tearDown() throws Exception {

         }

         @Test

         @Ignore

         public final void testInsertStringStringString() {

//               fail("Not yet implemented"); // TODO

         }

         @Test(timeout = 1000)

         public final void testInsertTableCompany() {

//               fail("Not yet implemented"); // TODO

         }

         @Test

         public final void testCompanyNum() {

                   fail("Not yet implemented"); // TODO

         }

         @Test(expected = DataFormatException.class)

         public final void testJustForDisplay() throws DataFormatException {

                   service.justForDisplay();

         }

}

junit3和junit4的使用区别如下
1.
JUnit3中需要继承TestCase类,但在JUnit4中已经不需要继承TestCase
2.JUnit3中需要覆盖TestCase中的setUptearDown方法,其中setUp方法会在测试执行前被调用以完成初始化工作,而tearDown方法则在结束测试结果时被调用,用于释放测试使用中的资源,而在JUnit4中,只需要在方法前加上@Before@After
3.JUnit3中对某个方法进行测试时,测试方法的命令是固定的,例如对addBook这个方法进行测试,需要编写名字为tetAddBook的测试方法,而在JUnit4中没有方法命令的约束,在方法的前面加上@Test,这就代表这个方法是测试用例中的测试方法
4.新的断言assertThat
5.
@BeforeClass 和 @AfterClass 。在JUnit3,如果所有的test case仅调用一次setUp()和tearDown()需要使用TestSetup类
6.测试异常处理@Test(expected = DataFormatException.class)
7.设置超时@Test(timeout = 1000)
8.忽略测试@Ignore
9.集成测试

 

 

集成测试

    利用TestSuite可以将一个TestCase子类中所有test***()方法包含进来一起运行,还可将TestSuite子类也包含进来,从而行成了一种等级关系。可以把TestSuite视为一个容器,可以盛放TestCase中的test***()方法,它自己也可以嵌套。这种体系架构,非常类似于现实中程序一步步开发一步步集成的现况。

    在junit中,Test、TestCase和TestSuite三者组成了composiste pattern。通过组装自己的TestSuite,可以完成对添加到这个TestSuite中的所有的TestCase的调用。而且这些定义的TestSuite还可以组装成更大的TestSuite,这样同时也方便了对于不断增加的TestCase的管理和维护。

    它的另一个好处就是,可以从这个TestCase树的任意一个节点(TestSuite或TestCase)开始调用,来完成这个节点以下的所有TestCase的调用。提高了unit test的灵活性。

例如:

### JUnit-3.8.1结构:

import junit.framework.Test;

import junit.framework.TestSuite;

public class TestAll{

    //定义一个suite,对于junit的作用可以视为类似于java应用程序的main。

    public static Test suite(){

        TestSuite suite = new TestSuite("Running all tests.");

        suite.addTestSuite( TestCase1.class);

        suite.addTestSuite( TestCase2.class);

        return suite;

    }

}

### JUnit-4.X结构:

import org.junit.runner.RunWith;

import org.junit.runners.Suite;

@RunWith(Suite.class)

@Suite.SuiteClasses({ TestCase1.class, TestCase2.class })

public class AllCalculatorTests {

 

}

//代码示例:在junit3中如何通过执行多个测试方法,却只执行一次setUp,tearDown方法

import junit.framework.*;
import junit.extensions.TestSetup;
public class AllTestsOneTimeSetup {
    public static Test suite() {
        TestSuite suite = new TestSuite();
        suite.addTest(SomeTest.suite());
        suite.addTest(AnotherTest.suite());
        TestSetup wrapper = new TestSetup(suite) {
            protected void setUp() {
                oneTimeSetUp();
            }
            protected void tearDown() {
                oneTimeTearDown();
            }
        };
        return wrapper;
    }
    public static void oneTimeSetUp() {
        // one-time initialization code
    }
    public static void oneTimeTearDown() {
        // one-time cleanup code
    }
}

【上篇】
【下篇】

抱歉!评论已关闭.