首页 新闻 会员 周边

service 层怎么测试?用junit

0
悬赏园豆:40 [已解决问题] 解决于 2016-03-18 11:02

 @Service
 2 public class StudentServiceImpl implements StudentService{
 3   
 4     @Autowired
 5     private StudentDao studentDao;    
 6     
 7     public List<Student> findAll() {
 8         
 9         return studentDao.findAll();
10     }
11 
12     public void insertStudent(Student student) {
13         studentDao.insertStudent(student);
14     }
15 
16     public Student findById(String id) {
17         return studentDao.findById(id);
18     }
19 
20     public void delete(String id) {
21         studentDao.delete(id);
22     }
23 
24     public void edit(String id, Student student)throws StudentException {
25         studentDao.edit(id, student);
26     }
27 
28 }

上面是service层的代码,怎么用junit测试?

关外野店的主页 关外野店 | 初学一级 | 园豆:188
提问于:2016-03-17 15:20
< >
分享
最佳答案
0
 1 private Service Service;
 2     
 3     @Before
 4     public void before(){
 5         ApplicationContext  ac = new ClassPathXmlApplicationContext(new String[]{"spring-mvc.xml","spring-mybatis.xml"});
 6         Service = (Service) ac.getBean("Service");
 7     }
 8     
 9     @Test
10     public void findAll(){
11         Service.findAll("");
12     }

 

收获园豆:20
snowjake123 | 菜鸟二级 |园豆:469 | 2016-03-17 17:01

谢谢已经解决

关外野店 | 园豆:188 (初学一级) | 2016-03-18 11:01
其他回答(3)
0

用日志!lognet4。

收获园豆:10
大楚打码人 | 园豆:4313 (老鸟四级) | 2016-03-17 16:18

要求用junit测试啊。

因为得调用dao的方法,但是dao的测试代码又不带返回类型我就纠结了。这样该怎么才能测试?

支持(0) 反对(0) 关外野店 | 园豆:188 (初学一级) | 2016-03-17 16:21
0

你dao明显的是为了做注入准备的,你测试的时候做mock的dao,然后自己准备数据(比如从list返回和直接修改list的数据),最后对mock中的dao的数据做断言。

收获园豆:10
Daniel Cai | 园豆:10424 (专家六级) | 2016-03-17 18:44

之前没用过断言什么的,现在解决了

支持(0) 反对(0) 关外野店 | 园豆:188 (初学一级) | 2016-03-18 11:01
0

@关外野店: 你好,我也正在学junit测试,能请教下么?

少年少年 | 园豆:202 (菜鸟二级) | 2016-06-14 09:23
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册