You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot集成测试配置疑问及多模块测试咨询

问题与解决方案

主应用代码

package com.example.demo;

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

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {

        System.out.println("Hello !");

        // SpringApplication.run(DemoApplication.class, args);

        SpringApplication application = new SpringApplication(DemoApplication.class);
        application.setBannerMode(Banner.Mode.OFF);
        application.run(args);
    }

}

测试代码

package com.example.demo;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;


@SpringBootTest(classes = DemoApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class DemoApplicationTests {

    @Test
    void contextLoads() {
    }

}

问题及解决方案

1. DemoApplicationTests未感知@SpringBootApplication注解

  • 先确认spring-boot-starter-test依赖已正确引入:
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    
  • 你的测试类和主应用类在同一根包下,无需额外配置。若仍有问题,尝试去掉@SpringBootTest中的classes = DemoApplication.class参数,让框架自动扫描主配置类;或者检查主应用类的包路径是否被测试类的扫描范围覆盖。

2. Maven多模块项目将其他模块纳入测试范围

  • 在测试模块的pom.xml中添加对目标模块的依赖,根据需求设置scope:
    <dependency>
        <groupId>com.example</groupId>
        <artifactId>other-module</artifactId>
        <version>${project.version}</version>
        <!-- 仅测试时使用则设为test,若需要主代码依赖则用compile -->
        <scope>test</scope>
    </dependency>
    
  • 确保目标模块中需要被测试访问的类为public修饰,或测试类与目标类在同一包下。若需要加载其他模块的Spring配置,可通过@SpringBootTest(scanBasePackages = "com.example")指定全局扫描路径。

3. 测试中的Spring应用不识别测试环境Profiles

  • 激活测试Profiles有三种方式:
    1. 在测试类上添加@ActiveProfiles("test")
    2. 在@SpringBootTest中通过properties参数指定:
      @SpringBootTest(
          properties = "spring.profiles.active=test"
      )
      
    3. 在src/test/resources/application-test.properties(或yml)中配置测试专属参数,配合@ActiveProfiles激活
  • 注意:测试环境配置文件必须放在src/test/resources目录下,测试时会优先加载该目录下的配置。

4. 配置测试与独立运行服务行为一致

  • 核心思路:复用主应用配置逻辑,仅覆盖测试特有的参数:
    1. 在src/test/resources下创建application-test.yml,只修改测试需要的配置(如使用内存数据库),其余配置继承主应用的application.yml
    2. 测试类激活测试环境并保持与主应用一致的启动配置:
      @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
      @ActiveProfiles("test")
      public class DemoApplicationTests {
      
          @Test
          void contextLoads() {
              // 验证上下文加载及配置生效
          }
      }
      
  • 若需模拟外部服务,可使用@MockBean替换真实Bean,避免依赖外部资源。

额外问题:实现CommandLineRunner后测试显示Banner

  • 主应用中是在main方法手动关闭Banner,但测试时@SpringBootTest会创建新的SpringApplication实例,不会复用main方法的配置。解决方法:
    1. 在配置文件中全局关闭:spring.main.banner-mode=off(可在主配置或测试配置中添加)
    2. 在@SpringBootTest中通过properties参数指定:
      @SpringBootTest(
          classes = DemoApplication.class,
          webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT,
          properties = "spring.main.banner-mode=off"
      )
      

内容的提问来源于stack exchange,提问作者catch22

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 04:05:54