您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 > junit
Spring mvc和Junit 4集成的注意點(diǎn)
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2015/6/10 15:10:09 ] 推薦標(biāo)簽:單元測試 Junit

  常規(guī)步驟:
  1、導(dǎo)入jar包,主要有兩個(gè),spring-test 和 junit4,主要用maven管理,直接依賴即可。可以在這個(gè)網(wǎng)站上進(jìn)行查找或下載:http://mvnrepository.com/
  2、編寫基類,這也不用每個(gè)測試類都要實(shí)現(xiàn)一遍,代碼如下:
/**
* 顯示基類,主要是用來加載配置文件的
* @author joy
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:/spring/applicationContext-base.xml","classpath:springmvc-config.xml","classpath:sqlMapConfig.xml"})
public class BaseJunit4Test extends AbstractJUnit4SpringContextTests{
//do nothing
}
  3、編寫測試類,注意繼承基類
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import com.wei.interfaces.UserService;
import com.wei.junit.BaseJunit4Test;
public class UserServiceImplTest extends BaseJunit4Test {
@Autowired
private UserService userService;
@Test
public void testGetUserById(){
assertEquals(userService.getUserById(1).getLoginName(), "admin");
}
}
  注意點(diǎn):
  1、已經(jīng)有了一個(gè)完整mvc層級(jí)架構(gòu),這樣依賴的時(shí)候不會(huì)報(bào)無法依賴注入的錯(cuò)誤;
  2、要添加servlet-api,否則會(huì)報(bào)如下錯(cuò)誤:


 

軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請(qǐng)使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd