JaveEE測(cè)試:第一步-簡(jiǎn)單整合

目標(biāo)

項(xiàng)目是基于Spring MVC的JaveEE項(xiàng)目。準(zhǔn)備使用Junit4進(jìn)行單元測(cè)試。整合spring-test。

整合過(guò)程

  1. 修改maven文件,增加如下的依賴(lài)
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-test</artifactId>  
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
  1. 書(shū)寫(xiě)測(cè)試用的基類(lèi)(整合了Spring-test)
    我們需要指定運(yùn)行器:SpringJUnit4ClassRunner.class
    指定需要的Spring配置文件:比如classpath:test-context.xml
    加入事務(wù)控制:@Transactional
    加入實(shí)物控制器(這個(gè)需要在spring配置文件中配置):@TransactionConfiguration
import javax.annotation.Resource;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;
//使用junit4進(jìn)行測(cè)試
@RunWith(SpringJUnit4ClassRunner.class)  
@ContextConfiguration({
    "classpath:test-context.xml", 
    "classpath:test-jpa.xml", 
    "classpath:test-servlet.xml",
    "classpath:test-memcache.xml"}) 
//  這個(gè)非常關(guān)鍵,如果不加入這個(gè)注解配置,事務(wù)控制就會(huì)完全失效!  
@Transactional  
//  這里的事務(wù)關(guān)聯(lián)到配置文件中的事務(wù)控制器(transactionManager = "transactionManager"),同時(shí)
//  指定自動(dòng)回滾(defaultRollback = true)。這樣做操作的數(shù)據(jù)才不會(huì)污染數(shù)據(jù)庫(kù)!  
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)  
public class BaseJunit4Test {
    //  可以注入spring的bean
    @Resource
    private MyService _myService;
    @Before
    public void login(){
        //  這里進(jìn)行登錄操作
    }
    @After
    public void clear(){
        //  這里進(jìn)行清理工作
    }
}
  1. 書(shū)寫(xiě)測(cè)試類(lèi)(這個(gè)測(cè)試類(lèi)對(duì)數(shù)據(jù)庫(kù)的修改會(huì)回滾,保證環(huán)境的清潔)
    注意:@Rollback(true) 標(biāo)明使用完此方法后事務(wù)是否回滾,這個(gè)配置比較重要
import static org.junit.Assert.assertEquals;
import java.math.BigDecimal;
import java.util.Date;
import javax.annotation.Resource;
import org.junit.Test;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional;
public class UserAssignServiceTest extends BaseJunit4Test{
    @Resource  //自動(dòng)注入,默認(rèn)按名稱(chēng)  
    private MyServices services;  
    @Test   //標(biāo)明是測(cè)試方法  
    @Transactional   //標(biāo)明此方法需使用事務(wù)  
    @Rollback(true)  //標(biāo)明使用完此方法后事務(wù)不回滾,true時(shí)為回滾  
    public void insert( ) {  
        long iCount = services.count(); 
        
        UserBean userBean = new UserBean();
        
        userBean.setInfoContext("測(cè)試");
        userBean.setOrgId(new BigDecimal(5));
        userBean.setInfoName("標(biāo)題");
        userBean.setInfoLevel(new BigDecimal(1));
        userBean.setInfoPath("path");
        userBean.setReleaseDate(new Date());
        
        services.save(userBean);
        
        assertEquals(iCount + 1 , services.count());
    }  
}

事實(shí)上我們已經(jīng)可以使用上面的例子進(jìn)行測(cè)試工作了。

問(wèn)題

我希望輸出如下的效果。也就是在測(cè)試開(kāi)始前輸出即將測(cè)試的方法名,或者一些其他信息;在測(cè)試結(jié)束后執(zhí)行一些操作。

UserAssignServiceTest -- insert()  測(cè)試開(kāi)始
-- 各種測(cè)試輸出數(shù)據(jù)
UserAssignServiceTest -- insert()  測(cè)試結(jié)束

最理想的地點(diǎn)是在@Befor,@After注解的方法上輸出,但是怎么知道馬上要執(zhí)行的測(cè)試方法名稱(chēng)是什么那?準(zhǔn)備在下一步解決這個(gè)問(wèn)題。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容