1、首先引入 junit-4.12 和 hamcrest-core 包或者使用Maven。
2、建立測試類:
public class UserTest {
private UserService userServiceImpl;
@Before
public void loginBefore() {
//加載
BeanFactory beanFactory = new ClassPathXmlApplicationContext("applicationContext.xml");
//獲取bean
userServiceImpl = (UserService) beanFactory.getBean("userService");
}
@Test
public void loginTest() {
//封裝實體類
UserPO userPO = new UserPO();
userPO.setUserId(10000);
userPO.setPassword("10000");
//業務層調用登錄方法
UserPO user = userServiceImpl.login(userPO);
if (user == null) {
System.out.println("登錄失敗");
} else {
System.out.println(user.getUserId() + "登錄成功");
}
}
}
3、打印結果
10000登錄成功