1. 通过maven引入spring-test框架
org.springframework spring-test 4.2.0.RELEASE
2. 直接贴上测试用例
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = { "classpath:applicationContext*.xml" })@Rollback(false)public class UserServiceTest { @Resource protected UserService userService; @Resource protected RoleService roleService; @Before public void before() { // 操作前数据情况 测试查询 System.err.println("user total: " + userService.list().size()); } @After public void after() { // 操作后数据情况 System.err.println("user total: " + userService.list().size()); } @Test public void userTest() { try { User user = null; for (int i = 0; i < 10; i++) { user = new User(); user.setUserName("user-" + i); user.setPassword(user.getUserName()); // 测试插入 userService.insert(user); // if (i == 5) { // throw new RuntimeException("error!"); // } } // 测试查询 Listusers = userService.list(); System.err.println("user total: " + users.size()); // 测试删除 for (User user2 : users) { userService.deleteByPrimaryKey(user2.getId()); } } catch (Exception e) { e.printStackTrace(); } }}
3. 运行测试用例即可
spring-test测试框架的其他功能仍在摸索学习