集成测试例子
public class UserServiceIntegrateWithJdbcTest extends AbstractAnnotationAwareTransactionalTests { protected UserService userService; protected User user; public UserServiceIntegrateWithJdbcTest() { //设置不需要为spring component添加set get方法 setPopulateProtectedVariables(true); //设置遇到没有注入的protected变量时是否报错 // setDependencyCheck(false); //设置spring装配方式(AUTOWIRE_BY_NAME,AUTOWIRE_BY_TYPE, AUTOWIRE_NO) // setAutowireMode(AUTOWIRE_BY_NAME); } //设置spring 配置文件路径 @Override protected String[] getConfigLocations() { return new String[] {"baobaotao-dao.xml", "baobaotao-service.xml"}; } @Test public void testUserMatchService() { assertFalse(userService.hasMatchUser("test", "test")); assertTrue(userService.hasMatchUser("admin", "123456")); } @Test public void testRegisterUser() { User user = new User(); user.setUserId(2); user.setUserName("john"); user.setPassword("123456"); userService.registerUser(user); String sqlStr = " select user_id from t_user where user_name = 'john'"; int userId = getJdbcTemplate().queryForInt(sqlStr); assertEquals(userId, user.getUserId()); //默认db操作在方法结束时不提交事物,可以用下面方法提交 // setComplete(); } //要求测试方法丢出异常 @Test @ExpectedException(RuntimeException.class) public void testThrowException() { throw new RuntimeException("test"); } //要求在规定时间内测试完成 @Timed(millis=200) public void testTimedMethod() throws InterruptedException { Thread.sleep(100); } //设定无事务环境运行测试方法 @NotTransactional public void testNoTransactionMethod() { System.out.println(""); } }
mvn dependency:sources