您的位置:首页 > 其它

junit基础学习之-测试service层(3)

2015-08-15 21:23 351 查看
测试步骤:

在之前的文章中已经加了junit的环境,这就不需要了。

1.加载junit类,spring配置文件,指明junit测试器,@Runwith

2.定义变量,service,不可以使用spring注解,因为spring注解是建立在server上的。

3.初始化@Before注解。

4.实现测试方法,@Test注解。

package swust.edu.cn.postdoctors.service.impl;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import swust.edu.cn.postdoctors.service.UserService;
import junit.framework.TestCase;

@RunWith(SpringJUnit4ClassRunner.class) // 整合
@ContextConfiguration(locations={"classpath:spring-mybatis-test.xml"}) // 加载配置
public class UserServiceTest extends TestCase {

private UserService userService;

public UserService getUserService() {
return userService;
}

public void setUserService(UserService userService) {
this.userService = userService;
}

//省略setget

@Test
public void testSelectUserByLoginNameAndPswd() throws Exception {

swust.edu.cn.postdoctors.model.User resUser = null ;

resUser = userService.findUserByLoginNameAndPswd("smx", "123");
if(resUser == null){
System.out.println("userService 出错!");
}else{
System.out.println("userService 正确!");
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: