Skip to content

common-test-starter

1. 功能介绍

1.引入测试依赖

  • 集成 spring-boot-starter-test,为项目提供丰富的测试功能,包括单元测试、集成测试和 Mock 功能。

2.排除默认日志依赖

  • 排除 spring-boot-starter-logging,避免与 Log4j2 日志框架冲突,确保日志管理的灵活性和一致性。

2.案例演示

1.创建模块

CleanShot 2025-01-20 at 15.42.16@2x

2.目录结构

CleanShot 2025-01-20 at 15.49.57@2x

3.pom.xml

1.基本配置
xml
<!-- 通过properties来指定版本号 -->
<properties>
    <!-- 指定编译版本 -->
    <java.version>1.8</java.version>
    <maven.compiler.source>8</maven.compiler.source>
    <maven.compiler.target>8</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <!-- 指定Sunrays-Framework的版本 -->
    <sunrays.version>1.0.0</sunrays.version>
</properties>

<dependencyManagement>
    <!-- 使用sunrays-dependencies来管理依赖,则依赖无需加版本号 -->
    <dependencies>
        <dependency>
            <groupId>cn.sunxiansheng</groupId>
            <artifactId>sunrays-dependencies</artifactId>
            <version>${sunrays.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
2.引入依赖
xml
<dependencies>
    <!-- common-test-starter< -->
    <dependency>
        <groupId>cn.sunxiansheng</groupId>
        <artifactId>common-test-starter</artifactId>
    </dependency>
    <!-- common-log4j2-starter 是必须引入的!!! -->
    <dependency>
        <groupId>cn.sunxiansheng</groupId>
        <artifactId>common-log4j2-starter</artifactId>
    </dependency>
</dependencies>

4.application.yml 配置日志存储根目录

yaml
sun-rays:
  log4j2:
    home: /Users/sunxiansheng/IdeaProjects/sunrays-framework-demo/common-test-starter-demo/logs # 日志根目录(默认./logs)

5.TestService.java 测试服务类

java
package cn.sunxiansheng.test.service;

import org.springframework.stereotype.Service;

/**
 * Description: 测试服务类
 *
 * @Author sun
 * @Create 2024/11/12 15:37
 * @Version 1.0
 */
@Service
public class TestService {

    public String test() {
        return "test";
    }
}

6.TestServiceTest.java 测试类

java
package cn.sunxiansheng.test;

import cn.sunxiansheng.test.service.TestService;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;

/**
 * Description: TestServiceTest
 *
 * @Author sun
 * @Create 2024/11/12 15:38
 * @Version 1.0
 */
@SpringBootTest
class TestServiceTest {

    @Resource
    private TestService testService;

    @Test
    void test1() {
        String test = testService.test();
        System.out.println("test = " + test);
    }
}

7.TestApplication.java 启动类

java
package cn.sunxiansheng.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Description: TestApplication
 *
 * @Author sun
 * @Create 2025/1/20 15:47
 * @Version 1.0
 */
@SpringBootApplication
public class TestApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }
}

8.测试

CleanShot 2025-01-20 at 15.56.02@2x