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

测试步骤:

在之前的文章中已经加了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

 

posted @ 2015-08-15 21:23  小小小的程序猿  阅读(7658)  评论(1编辑  收藏  举报
window.onload = function(){ $("#live2dcanvas").attr("style","position: fixed; opacity: 0.7; left: 70px; bottom: 0px; z-index: 1; pointer-events: none;") }