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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM