Spring之单元测试

Spring提供了一个运行器,可以读取配置文件(或注解)来创建容器。

1 整合Junit5

1.1 引入依赖
<dependencies>
    
    
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-contextartifactId>
        <version>6.1.1version>
    dependency>

    
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-testartifactId>
        <version>6.1.1version>
    dependency>

    
    <dependency>
        <groupId>org.junit.jupitergroupId>
        <artifactId>junit-jupiter-apiartifactId>
        <version>5.9.0version>
    dependency>

    
    <dependency>
        <groupId>org.apache.logging.log4jgroupId>
        <artifactId>log4j-coreartifactId>
        <version>2.19.0version>
    dependency>
    <dependency>
        <groupId>org.apache.logging.log4jgroupId>
        <artifactId>log4j-slf4j2-implartifactId>
        <version>2.19.0version>
    dependency>
dependencies>
1.2 配置扫描

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <context:component-scan base-package="com.giser.spring6" />

beans>
1.3 基础实体
package com.giser.spring6.junit5;

import org.springframework.stereotype.Component;

/**
 * @author giserDev
 * @description
 * @date 2024-01-07 19:37:52
 */
@Component
public class User {
    public User() {
        System.out.println("run user");
    }
}
1.4 测试
package com.giser.spring6.junit5;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

/**
 * @author giserDev
 * @description
 * @date 2024-01-07 19:41:24
 */
// 方式二
//@ExtendWith(SpringExtension.class)
//@ContextConfiguration("classpath:spring-bean.xml")
// 方式一 推荐
@SpringJUnitConfig(locations = "classpath:spring-bean.xml")
public class SpringJunit5Test {

    @Autowired
    private User user;

    @Test
    public void testUser(){
        System.out.println(user);
    }

}

2 整合Junit4

2.1 引入依赖

<dependency>
    <groupId>junitgroupId>
    <artifactId>junitartifactId>
    <version>4.12version>
dependency>
2.2 实体
package com.giser.spring6.junit4;

import org.springframework.stereotype.Component;

/**
 * @author giserDev
 * @description
 * @date 2024-01-07 19:37:52
 */
@Component
public class Junit4User {
    public Junit4User() {
        System.out.println("run junit4User");
    }
}
2.3 测试
package com.giser.spring6.junit4;

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.SpringJUnit4ClassRunner;

/**
 * @author giserDev
 * @description
 * @date 2024-01-07 19:41:24
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-bean.xml")
public class SpringJunit4Test {

    @Autowired
    private Junit4User junit4User;

    @Test
    public void testUser(){
        System.out.println(junit4User);
    }

}

你可能感兴趣的:(#,Spring,spring,单元测试,java)