基於SSM的註解Junit測試

如標題,該篇展示我是怎麼使用ssm進行測試的。
步驟一:導出項目(備份,萬一項目炸了我可負責不了的~ ~ ~)
步驟二:導包(點擊下載,密碼:8820,那些數據庫,mybatis的包就不列了,舉重要的)
junit-4.9.jar
validation-api-2.0.1.Final.jar
hibernate-validator-6.0.14.Final.jar
classmate-1.0.0.jar
spring的Test包(除了這個,其他我會在上面打包好放到某雲不用你們一個個下載)在這裏插入圖片描述
注意:我這裏用環境是jdk1.8,spring4.1,所以儘量採用新的包1.8的,以防止出現版本衝突,如jdk會對4.0一下的spring不兼容(降低jdk版本或升級spring版本解決),而且會對一些老的包不兼容,如(validation-api-1.0.0.GA.jar、hibernate-validator-4.3.0.Final.jar),然後就出bug

org.hibernate.validator.internal.engine.ConfigurationImpl.getDefaultParameterNameProvider()Ljavax/validation/ParameterNameProvider;

org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘(inner bean)#72cf4dc6’: Cannot resolve reference to bean ‘validator’ while setting bean property ‘validator’; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘validatorH’ defined in class path resource [spring/springmvc.xml]: Invocation of init method failed; nested exception is java.lang.AbstractMethodError:

以上兩個就是我遇到的版本不兼容bug,還有就是缺少classmate包的,找不到控制檯了信息了,就不貼問題了。

步驟三:測試代碼(看這個人的,總結的概念齊全,我基本是跟着他來敲的)
新建基類加載spring、mybatis等配置文件
BaseTest.java

package cn.campus.fleasstore.ssm.junit;

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;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:mybatis/sqlMapConfig.xml",
	"classpath:spring/applicationContext-dao.xml",
	"classpath:spring/applicationContext-service.xml",
	"classpath:spring/applicationContext-transaction.xml",
	"classpath:spring/springmvc.xml"})
@TransactionConfiguration(transactionManager="transactionManager",defaultRollback=true)

public class BaseTest {
	
}

再在測試類中繼承基類

package cn.campus.fleasstore.ssm.junit;

import java.util.Date;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional;

import cn.campus.fleasstore.ssm.po.Users;
import cn.campus.fleasstore.ssm.service.UsersService;

public class loginJUnit extends BaseTest{
	@Autowired
	private UsersService usersService;
	
	@Test
	@Transactional
	@Rollback(false)  //標明使用完此方法後事務不回滾,true時爲回滾
	public void insertUser() throws Exception {
		Users usersCustom = new Users();
		usersCustom.setUserId(2013111457);
		usersCustom.setUsername("一波不停");
		usersCustom.setPassword("abc123");
		usersCustom.setRealname("黃海波");
		usersCustom.setPhone("1893047611");
		usersCustom.setBirthday(new Date());
		usersService.insertUser(usersCustom);
		System.out.println("註冊成功");
		
	}

}

控制檯(圖片好像有點長)
在這裏插入圖片描述
數據庫
在這裏插入圖片描述

大功告成~~
覺得有用的給個讚唄!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章