zoukankan      html  css  js  c++  java
  • Spring单元测试

    1.下载Spring4所需要的jar包,包括如下:spring-core、spring-context、spring-jdbc、spring-beans、spring-web、spring-expression、spring-orm等或创建maven项目来管理jar包;

    2.创建maven项目, 在pom.xml中编写Spring需要的jar包,maven会自动下载这些包以及相关的依赖jar包,在pom.xml中的代码如下:

     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接口及其实现类

    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文件

     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下创建测试类

     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单元测试成功!

  • 相关阅读:
    3.23.谷歌中国搜索关闭的日子
    在Fedora下成功将Vim打造成适用于C/C++的IDE
    有关内存DC和双缓冲位图的问题汇总
    [转]阶乘 n! 末尾 0 的个数
    [转]各种排序算法
    [转]使用CEGUI的Editbox进行中文输入
    [转]ASP.NET中文件上传下载方法集合
    背包问题的c++解法
    [转]经典C/C++算法
    [转]编写自己的MSN机器人
  • 原文地址:https://www.cnblogs.com/fenglanglang/p/6001610.html
Copyright © 2011-2022 走看看