zoukankan      html  css  js  c++  java
  • Spring整合Junit框架进行单元测试Demo

    一.开发环境

    eclipse版本:4.6.1

    maven版本:3.3.3

    junit版本:4.12

    spring版本:4.1.5.RELEASE

    JDK版本:1.8.0_111

    二.项目结构

    三.文件清单

    pom.xml

    1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
    3.     <modelVersion>4.0.0</modelVersion>  
    4.     <groupId>springJunit</groupId>  
    5.     <artifactId>springJunit</artifactId>  
    6.     <version>1.0</version>  
    7.     <packaging>war</packaging>  
    8.     <properties>  
    9.         <!-- spring版本号 -->  
    10.         <spring.version>4.1.5.RELEASE</spring.version>  
    11.         <!-- junit版本号 -->  
    12.         <junit.version>4.12</junit.version>  
    13.     </properties>  
    14.     <dependencies>  
    15.         <dependency>  
    16.             <groupId>org.springframework</groupId>  
    17.             <artifactId>spring-test</artifactId>  
    18.             <version>${spring.version}</version>  
    19.         </dependency>  
    20.         <dependency>  
    21.             <groupId>org.springframework</groupId>  
    22.             <artifactId>spring-context</artifactId>  
    23.             <version>${spring.version}</version>  
    24.         </dependency>  
    25.         <dependency>  
    26.             <groupId>junit</groupId>  
    27.             <artifactId>junit</artifactId>  
    28.             <version>${junit.version}</version>  
    29.             <scope>test</scope>  
    30.         </dependency>  
    31.     </dependencies>  
    32. </project>  

    JunitTest.java

    1. package com.dqiang;  
    2.   
    3. public interface JunitTest {  
    4.     public String sayHelloWorld(String word);  
    5. }  

    JunitTestImpl.java

    1. package com.dqiang.impl;  
    2.   
    3. import org.springframework.stereotype.Service;  
    4.   
    5. import com.dqiang.JunitTest;  
    6.   
    7. @Service  
    8. public class JunitTestImpl implements JunitTest {  
    9.     public String sayHelloWorld(String word) {  
    10.         return "hello->" + word;  
    11.     }  
    12. }  

    JTest.java

    1. package springJunit;  
    2.   
    3. import org.junit.Test;  
    4. import org.junit.runner.RunWith;  
    5. import org.springframework.beans.factory.annotation.Autowired;  
    6. import org.springframework.test.context.ContextConfiguration;  
    7. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;  
    8.   
    9. import com.dqiang.JunitTest;  
    10.   
    11. @RunWith(SpringJUnit4ClassRunner.class)  
    12. @ContextConfiguration(locations = { "classpath:springmvc.xml" })  
    13. public class JTest {  
    14.     @Autowired  
    15.     private JunitTest junitTest;  
    16.   
    17.     @Test  
    18.     public void sayHelloWorld() {  
    19.         String result = junitTest.sayHelloWorld("junit Test");  
    20.         System.out.println(result);  
    21.     }  
    22. }  

    说明:

    @RunWith 使用RunWith注解改变JUnit的默认执行类,并实现自已的Listener在平时的单元测试,如果不使用RunWith注解,那么JUnit将会采用默认的执行类Suite执行。
    @ContextConfiguration 可以通过该属性手工指定 Spring 配置文件所在的位置,可以指定一个或多个 Spring 配置文件。

    Demo下载:点击下载

  • 相关阅读:
    The prefix "mvc" for element "mvc:annotation-driven" is not bound 的解决方法
    Intellij Idea14 jstl标签的引入
    17个短视频渠道分成收益全解析
    chrome浏览器插件推荐——Vimium 篇
    从Java代码到字节码(1)
    Java跳出循环-break和continue语句
    Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986
    Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.5:test
    XML文件解析
    jpa基于按annotation的hibernate主键生成策略
  • 原文地址:https://www.cnblogs.com/jpfss/p/8073091.html
Copyright © 2011-2022 走看看