码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-08-15 22:50:26      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:

测试步骤:

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

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

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

3.初始化@Before注解。

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

 1 package swust.edu.cn.postdoctors.service.impl;
 2 
 3 import javax.annotation.Resource;
 4 
 5 import org.junit.Test;
 6 import org.junit.runner.RunWith;
 7 import org.springframework.beans.factory.annotation.Autowired;
 8 import org.springframework.test.context.ContextConfiguration;
 9 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
10 
11 import swust.edu.cn.postdoctors.service.UserService;
12 import junit.framework.TestCase;
13 
14 @RunWith(SpringJUnit4ClassRunner.class) // 整合 
15 @ContextConfiguration(locations={"classpath:spring-mybatis-test.xml"}) // 加载配置 
16 public class UserServiceTest extends TestCase {
17 
18 19     private UserService userService;
20     
21     public UserService getUserService() {
22         return userService;
23     }
24 
25 26     public void setUserService(UserService userService) {
27         this.userService = userService;
28     }
29 
30     //省略setget
31 @Test 32 public void testSelectUserByLoginNameAndPswd() throws Exception { 33 34 swust.edu.cn.postdoctors.model.User resUser = null ; 35 36 resUser = userService.findUserByLoginNameAndPswd("smx", "123"); 37 if(resUser == null){ 38 System.out.println("userService 出错!"); 39 }else{ 40 System.out.println("userService 正确!"); 41 } 42 43 } 44 } 46 47

 

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

标签:

原文地址:http://www.cnblogs.com/x739400043/p/4733142.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!