Spring單元測試

1.下載Spring4所須要的jar包,包括以下:spring-core、spring-context、spring-jdbc、spring-beans、spring-web、spring-expression、spring-orm等或建立maven項目來管理jar包;java

2.建立maven項目, 在pom.xml中編寫Spring須要的jar包,maven會自動下載這些包以及相關的依賴jar包,在pom.xml中的代碼以下:web

 1 <properties>
 2           <!-- spring的版本號 -->
 3           <spring-version>4.1.4.RELEASE</spring-version>
 4       </properties>
 5       <dependencies>
 6           <dependency>
 7               <groupId>org.springframework</groupId>
 8               <artifactId>spring-core</artifactId>
 9               <version>${spring-version}</version>
10           </dependency>
11           <dependency>
12               <groupId>org.springframework</groupId>
13               <artifactId>spring-context</artifactId>
14               <version>${spring-version}</version>
15           </dependency>
16           <dependency>
17               <groupId>org.springframework</groupId>
18               <artifactId>spring-jdbc</artifactId>
19               <version>${spring-version}</version>
20           </dependency>
21           <dependency>
22               <groupId>org.springframework</groupId>
23               <artifactId>spring-beans</artifactId>
24               <version>${spring-version}</version>
25           </dependency>
26           <dependency>
27               <groupId>org.springframework</groupId>
28               <artifactId>spring-web</artifactId>
29               <version>${spring-version}</version>
30           </dependency><dependency>
31               <groupId>org.springframework</groupId>
32               <artifactId>spring-expression</artifactId>
33               <version>${spring-version}</version>
34           </dependency>
35           <dependency>
36               <groupId>org.springframework</groupId>
37               <artifactId>spring-orm</artifactId>
38               <version>${spring-version}</version>
39           </dependency>
40     </dependencies>

3.編寫Service接口及其實現類spring

1 package com.youms.ssh.webService.service;
2 
3 public interface UserService {
4     public void test();
5 }
 1 package com.youms.ssh.webService.service.impl;
 2 
 3 import org.springframework.stereotype.Service;
 4 import com.youms.ssh.webService.service.UserService;
 5 
 6 @Service("userService")
 7 public class UserServiceImpl implements UserService{
 8 
 9     @Override
10     public void test() {
11         // TODO Auto-generated method stub
12         System.out.println("Hello Spring!");
13     }
14 
15 }

4.在src/main/resources目錄下建立一個spring.xml文件express

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 4     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans
 6                http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
 7                http://www.springframework.org/schema/context
 8                http://www.springframework.org/schema/context/spring-context-4.0.xsd
 9                http://www.springframework.org/schema/tx
10                http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
11                http://www.springframework.org/schema/aop
12                http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
13                ">
14     <!-- 自動掃描service包(自動注入) -->
15     <context:component-scan base-package="com.youms.ssh.webService" />
16 
17</beans>

5.導入junit包,在src/test/java下建立測試類ssh

 1 package test;
 2 
 3 import org.junit.After;
 4 import org.junit.Before;
 5 import org.junit.Test;
 6 import org.springframework.context.ApplicationContext;
 7 import org.springframework.context.support.ClassPathXmlApplicationContext;
 8 
 9 import com.youms.ssh.webService.service.UserService;
10 
11 public class SpringTest {
12     //ApplicationContext繼承自BeanFactory接口,
13     //其實現類能尋找指定的XML配置文件,找到並裝載完成ApplicationContext的實例化工做
14     private ApplicationContext ac = null;
15 
16     @Before
17     public void setUp() throws Exception {
18         //配置文件的路徑,能夠是多個 文件
19         String[] configs = {"classpath:spring.xml"};
20         //經過spring.xml配置文件建立Spring的應用程序上下文環境
21         ac = new ClassPathXmlApplicationContext(configs);
22     }
23 
24     @After
25     public void tearDown() throws Exception {
26     }
27 
28     @Test
29     public void test() {
30         UserService userService = (UserService) ac.getBean("userService");
31         userService.test();
32     }
33 
34 }

JUnit運行test()方法,控制檯輸出Hello Spring!說明Spring單元測試成功!maven

相關文章
相關標籤/搜索