码迷,mamicode.com
首页 > Web开发 > 详细

Sping MVC 整合Junit4进行单元测试及常见错误解决

时间:2018-10-13 22:56:57      阅读:339      评论:0      收藏:0      [点我收藏+]

标签:extc   data   weave   mod   .json   junit4   mvc   测试   text   

1.Sping整合Junit4进行单元测试:使用spring-test和Junit4进行单元测试

(1)maven依赖:添加spring-test和Junit4 jar包
对于jdk1.7版本,spring4版本,依赖如下:

<dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>3.2.9.RELEASE</version>
</dependency>

对于jdk1.8,spring 5,依赖如下:

<dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>3.2.9.RELEASE</version>
</dependency>

注意:这里的junit和spring-test版本号,否则报错;junit和spring-test之间具有版本对应关系(上面的对应关系是经过多次测试得到的)。

(2)编写单元测试:

package com.shang;

import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4Cla***unner;

import com.alibaba.fastjson.JSON;
import com.shang.pojo.Account;
import com.shang.service.AccountService;

@RunWith(SpringJUnit4Cla***unner.class)
@ContextConfiguration(locations={"classpath:applicationContext.xml"})  
public class TestController {
  @Autowired
  AccountService accountService;

  @Test
  public void test(){
    List<Account> list=accountService.getAll();
    System.out.println(JSON.toJSONString(list));
  }
}


注意:
(1)ContextConfiguration(locations={"classpath:applicationContext.xml"}) 中的applicationContext.xml的存储路径为:src/main/resources
(2)ContextConfiguration(locations={"classpath:applicationContext.xml","其他路径"})中的locations中可以指定多个路径

2.单元测试中的注释含义:
(1)@RunWith 注释是 Junit 提供的,用来说明此测试类的运行者,这里用了 SpringJUnit4Cla***unner,这个类是一个针对 Junit 运行环境的自定义扩展。

(2)@ContextConfiguration 注释是 Spring test context 提供的,用来指定 Spring 配置信息的来源,支持指定 XML 文件位置或者 Spring 配置类名,这里我们指定 classpath 下的 applicationContext.xml为配置文件的位置。

(3)@Autowired 进行对象的实例化,用来说明测试类是在 Spring 容器中管理的,可以获取容器的 bean进行注入,不用手工获取要测试的 bean 实例了。

3.完整的Spring MVC和单元测试的pom文件如下:

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.shangh</groupId>
  <artifactId>SSM</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>

<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.4.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.4.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.6</version>
        </dependency>

        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-jdbc</artifactId>  
            <version>3.0.5.RELEASE</version>  
        </dependency>  

        <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>1.2.3</version>
            </dependency>

       <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis</artifactId>
          <version>3.2.7</version>
       </dependency>                

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>4.3.4.RELEASE</version>
        </dependency>

        <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.3.4.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.39</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>3.2.9.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.47</version>
    </dependency>

    <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.7.2</version>
        </dependency>

      <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</artifactId>
            <version>1.2.2.RELEASE</version>
        </dependency>
    </dependencies>

<build>  
        <!-- 资源拷贝插件 -->
      <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                </includes>
            </resource>
        </resources>

         <plugins>
          <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <!--控制tomcat端口号 -->
                <configuration>
                    <port>8080</port>
                    <!-- 发布到tomcat后的名称 -->
                     <!--/ 相当于把项目发布成ROOT -->
                    <path>/abc</path>
                    <uriEncoding>UTF-8</uriEncoding>
                   <!--  <finalName>mgr</finalName>
                    <server>tomcat7</server> -->
                    <username>tomcat</username>
                    <password>tomcat </password>
                    <url>http://localhost:8080/manager/text</url>
                </configuration>
            </plugin>
            </plugins>
         </build>
</project>

通过以上配置,就可以在spring中进行单元测试了。
4.单元测试配置时的错误
(1)org.springframework.test.context.junit4.SpringJUnit4Cla***unner的解决
原因:依赖的jar包对应的版本不一致。
解决:(1)添加依赖:

<dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>3.2.9.RELEASE</version>
        </dependency>

(2)右键根项目—maven—update dependencies.重新更新依赖关系,让工程可以找到最新的依赖。

Sping MVC 整合Junit4进行单元测试及常见错误解决

标签:extc   data   weave   mod   .json   junit4   mvc   测试   text   

原文地址:http://blog.51cto.com/59465168/2299617

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!