Spring-IOC-Spring6和JUnit5集成

1、父工程pom.xml

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>6.0.6</spring.version>
        <junit.version>5.3.1</junit.version>
        <lombok.version>1.18.20</lombok.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring.version}</version>
            </dependency>

            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok.version}</version>
            </dependency>

            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-api</artifactId>
                <version>${junit.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>${spring.version}</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

2、子工程pom.xml

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
        </dependency>
    </dependencies>

3、Car.java

package com.atguigu.ioc;
import lombok.Data;
@Data
public class Car {
    private String cname;
}

4、MySpringConfiguration3.java

package com.atguigu.ioc;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MySpringConfiguration3 {
    @Bean
    public Car getCar3() {
        Car car = new Car();
        car.setCname("仰望");
        return car;
    }
}

5、SpringJUnitConfigTest.java

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

@SpringJUnitConfig(MySpringConfiguration3.class)
public class SpringJunitConfigTest {

    @Autowired
    private Car getCar3;

    @Test
    public void test() {
        System.out.println(getCar3);
    }

}
//Car(cname=仰望)

Spring6和JUnit5可以很好地集成,以下是基本的步骤:

  1. 在Maven或Gradle中添加JUnit5的依赖项。

  2. 在测试类上使用JUnit5的注解,例如:

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

@SpringBootTest
public class MyServiceTest {

    @Autowired
    private MyService myService;

    @Test
    public void myTest() {
        // test method body
    }

}

  1. 如果需要使用Spring的依赖注入功能,可以使用@Autowired注解将服务注入到测试类中。

  2. Spring6提供了一个@SpringBootTest注解,帮助我们创建Spring应用程序上下文和测试环境并自动配置Spring应用程序上下文。这样,我们就可以创建一个真实的Spring应用程序环境,并在测试中使用。

  3. 我们还可以使用@MockBean注解来模拟服务并将其注入到测试类中。这使我们能够测试服务的行为,而不依赖于具体的实现。

  4. 最后,我们可以使用各种JUnit5的断言和前置条件来编写测试用例,例如assertEquals和assertNotNull等。

总的来说,Spring6和JUnit5的集成非常容易和灵活,可以帮助开发人员编写高质量的测试用例并确保Spring应用程序的正确性。

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值