junit基础学习之-参数初始化(5)
1 package swust.edu.cn.postdoctors.service.impl; 2 3 import java.util.Arrays; 4 import java.util.Collection; 5 6 7 8 import org.junit.Before; 9 import org.junit.Test; 10 import org.junit.runner.RunWith; 11 import org.junit.runners.Parameterized; 12 import org.junit.runners.Parameterized.Parameters; 13 import org.springframework.test.context.ContextConfiguration; 14 15 import swust.edu.cn.postdoctors.model.User; 16 import swust.edu.cn.postdoctors.service.UserService; 17 import junit.framework.TestCase; 18 19 @RunWith(Parameterized.class) 20 @ContextConfiguration(locations={"classpath:spring-mybatis-test.xml"}) // 加载配置 21 public class UserServiceTest extends TestCase { 22 23 24 private UserService userService; 25 26 public UserService getUserService() { 27 return userService; 28 } 29 30 public void setUserService(UserService userService) { 31 this.userService = userService; 32 } 33 34 @Parameters 35 public static Collection<Object[]> data(){ 36 return Arrays.asList(new Object[][]{{"aa","11"},{"bb","22"},{"cc","33"}}); 37 } 38 private String userLoginname; 39 private String userPswd; 40 41 public UserServiceTest(String userLoginname,String userPswd){ 42 this.setUserLoginname(userLoginname); 43 this.setUserPswd(userPswd); 44 } 45 46 47 48 public String getUserLoginname() { 49 return userLoginname; 50 } 51 52 public void setUserLoginname(String userLoginname) { 53 this.userLoginname = userLoginname; 54 } 55 56 public String getUserPswd() { 57 return userPswd; 58 } 59 60 public void setUserPswd(String userPswd) { 61 this.userPswd = userPswd; 62 } 63 64 @Before 65 public void before() throws Exception { 66 userService = new UserServiceImpl(); 67 } 68 69 @Test 70 public void testSelectUserByLoginNameAndPswd() throws Exception { 71 if(userService == null){ 72 System.out.println("========================userService 出错!"); 73 } 74 User exUser = new User(); 75 exUser.setUserLoginname(userLoginname); 76 exUser.setUserPassword(userPswd); 77 78 79 User outUser = null; 80 81 outUser = userService.findUserByLoginNameAndPswd(userLoginname, userPswd); 82 83 assertEquals(exUser,outUser); 84 85 } 86 87 88 }