系列二、Spring整合单元测试

一、概述

        Spring中获取bean最常见的方式是通过ClassPathXmlApplicationContext 或者 AnnotationConfigApplicationContext的getBean()方式获取bean,那么在Spring中如何像在SpringBoot中直接一个类上添加个@SpringBootTest注解,即可在类中注入自己想要测试的bean呢?解决方案是有的,spring-test即提供了这个功能。Spring整合单元测试步骤如下:

二、Spring整合Junit单元测试

2.1、整体结构

系列二、Spring整合单元测试_第1张图片

2.2、pom


    4.0.0

    org.star
    spring5x06-mybatis
    1.0-SNAPSHOT
    jar

    spring5x06-mybatis
    http://maven.apache.org

    
        UTF-8
    

    
        
        
            org.springframework
            spring-aop
            5.2.5.RELEASE
        
        
            org.springframework
            spring-beans
            5.2.5.RELEASE
        
        
            org.springframework
            spring-context
            5.2.5.RELEASE
        
        
            org.springframework
            spring-core
            5.2.5.RELEASE
        
        
            org.springframework
            spring-expression
            5.2.5.RELEASE
        
        
            org.springframework
            spring-test
            5.2.5.RELEASE
        

        
        
            mysql
            mysql-connector-java
            8.0.26
        
        
            com.alibaba
            druid
            1.2.16
        
        
            org.springframework
            spring-jdbc
            5.3.27
        
        
            org.mybatis
            mybatis
            3.5.11
        
        
            org.mybatis
            mybatis-spring
            2.1.0
        

        
        
            org.projectlombok
            lombok
            1.18.22
        
        
            org.slf4j
            slf4j-api
            1.7.32
        
        
            ch.qos.logback
            logback-classic
            1.2.10
        

        
        
            cglib
            cglib
            3.1
        
        
            aopalliance
            aopalliance
            1.0
        
        
            org.aspectj
            aspectjweaver
            1.9.19
        

        
        
            junit
            junit
            4.13.2
            test
        
        
            com.alibaba
            fastjson
            1.2.76
        
        
            org.apache.commons
            commons-collections4
            4.3
        
        
            org.apache.commons
            commons-lang3
            3.11
        
        
            cn.hutool
            hutool-all
            5.7.22
        
        
            com.fasterxml.jackson.core
            jackson-databind
            2.12.1
        
        
            commons-logging
            commons-logging
            1.1.1
        

    

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


2.3、使用

/**
 * @Author : 一叶浮萍归大海
 * @Date: 2023/11/23 19:12
 * @Description: Spring整合单元测试
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class SpringJunitTest {

    @Resource
    private UserMapper userMapper;
    
    @Resource
    private UserService userService;

    @Test
    public void userMapperTest() {
        List userDOS = userMapper.listAllUser();
        System.out.println("userMapper = " + userMapper);
        System.out.println("userDOS = " + userDOS);
    }

    @Test
    public void userServiceTest() {
        List userDOS = userService.listAllUser();
        System.out.println("userService = " + userService);
        System.out.println("userDOS = " + userDOS);
    }

}

系列二、Spring整合单元测试_第2张图片

你可能感兴趣的:(Spring5系列,spring,单元测试)