为什么80%的码农都做不了架构师?>>>
1. 通过maven引入spring-test框架
<dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>4.2.0.RELEASE</version>
</dependency>
2. 直接贴上测试用例
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext*.xml" })
@Rollback(false)
public class UserServiceTest {@Resourceprotected UserService userService;@Resourceprotected RoleService roleService;@Beforepublic void before() {// 操作前数据情况 测试查询System.err.println("user total: " + userService.list().size());}@Afterpublic void after() {// 操作后数据情况System.err.println("user total: " + userService.list().size());}@Testpublic 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!");// }}// 测试查询List<User> users = 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测试框架的其他功能仍在摸索学习