Spring Junit测试用例
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:spring-bean.xml") public class TestUser { @Resource private UserDao dao; @Test public void testlogin() { User user = new User(); user.setUserName("aaron"); user.setPassword("123456"); user.setRoleName("系统管理员"); System.out.println("登录用户:"+dao.login(user)); } }
加上事务回滚的测试用例
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:spring-bean.xml") @TransactionConfiguration(transactionManager="transactionManager",defaultRollback=true) public class TestUser { @Resource private UserDao dao; @Test @Transactional public void testsave() { User user = new User(); user.setUserName("test1"); user.setPassword("test1"); user.setEmail("test111111@163.com"); user.setPhone("123123111"); user.setRoleName("manager2"); user.setTrueName("testuser2"); int i = dao.addUser(user); System.out.println("新增 "+i+" 位用户"); } }
这里junit.jar包需要4.5以上的版本