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

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

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


            junit
            junit
            4.12
            test
        
        
            org.springframework
            spring-test
            3.2.9.RELEASE

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


            junit
            junit
            4.12
            test
        
        
            org.springframework
            spring-test
            3.2.9.RELEASE

注意:这里的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 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文件如下:

 
  4.0.0
  com.shangh
  SSM
  0.0.1-SNAPSHOT
  war


        
            org.springframework
            spring-context
            4.3.4.RELEASE
        

        
            org.springframework
            spring-webmvc
            4.3.4.RELEASE
        

        
            org.aspectj
            aspectjweaver
            1.8.6
        

          
            org.springframework  
            spring-jdbc  
            3.0.5.RELEASE  
          

        
                org.mybatis
                mybatis-spring
                1.2.3
            

       
          org.mybatis
          mybatis
          3.2.7
                       

        
            org.springframework
            spring-tx
            4.3.4.RELEASE
        

        
        org.springframework
        spring-core
        4.3.4.RELEASE
        

        
            javax.servlet
            jstl
            1.2
        

        
            javax.servlet
            servlet-api
            2.5
            provided
        

        
            javax.servlet.jsp
            jsp-api
            2.2
            provided
        

        
            mysql
            mysql-connector-java
            5.1.39
        

        
            junit
            junit
            4.12
            test
        

        
            org.springframework
            spring-test
            3.2.9.RELEASE
        

        
            log4j
            log4j
            1.2.17
        

        
    
        com.alibaba
        fastjson
        1.2.47
    

    
            redis.clients
            jedis
            2.7.2
        

      
            org.springframework.session
            spring-session-data-redis
            1.2.2.RELEASE
        
    

  
        
      
            
                src/main/java
                
                    **/*.xml
                
            
            
                src/main/resources
                
                    **/*.xml
                    **/*.properties
                
            
        

         
          
                org.apache.tomcat.maven
                tomcat7-maven-plugin
                2.2
                
                
                    8080
                    
                     
                    /abc
                    UTF-8
                   
                    tomcat
                    tomcat 
                    http://localhost:8080/manager/text
                
            
            
         

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


            junit
            junit
            4.12
            test
        
        
            org.springframework
            spring-test
            3.2.9.RELEASE
        

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

转载于:https://blog.51cto.com/59465168/2299617

你可能感兴趣的:(Sping MVC 整合Junit4进行单元测试及常见错误解决)