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

Spring Boot:OpenApi Generator Plugin仍生成多余测试文件,如何禁用?

问题

我使用OpenApi 3.0及maven插件openapi-generator-maven-plugin生成API与实体类,对应的maven配置如下:

<execution>
    <goals>
        <goal>generate</goal>
    </goals>
    <configuration>
        <inputSpec>${project.basedir}/src/main/resources/BookingService.yaml</inputSpec>
        <generatorName>spring</generatorName>
        <modelPackage>${clientPackage}.model</modelPackage>
        <invokerPackage>${clientPackage}.invoker</invokerPackage>
        <apiPackage>${clientPackage}.api</apiPackage>
        <generateApis>true</generateApis>
        <generateApiTests>false</generateApiTests>
        <generateModelTests>false</generateModelTests>
        <configOptions>
            <delegatePattern>true</delegatePattern>
        </configOptions>
    </configuration>
</execution>

插件基本按预期工作,但仍生成了我不需要的测试文件。我已在配置中将generateApiTests和generateModelTests设为false,这些测试文件编译时因构建目标文件夹中无法解析'SpringBootTest'符号而失败,请问如何禁用这些多余的测试文件?

解决方法

  • 升级插件版本:部分旧版本的openapi-generator-maven-plugin存在参数不生效的bug,建议升级到最新稳定版(如6.x及以上),这是最直接的解决方案。
  • 添加全局禁用参数:在插件的configuration节点下新增全局测试生成开关,覆盖单独的API/模型测试配置:
    <generateTests>false</generateTests>
    <generateSupportingFiles>false</generateSupportingFiles>
    
    其中generateTests控制所有测试文件生成,generateSupportingFiles会阻止生成测试相关的支撑文件。
  • 跳过测试文件编译:如果仍有残留测试文件,可通过maven编译插件排除测试类:
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
            <testExcludes>
                <testExclude>**/*Test.java</testExclude>
            </testExcludes>
        </configuration>
    </plugin>
    
    或者执行构建命令时添加参数:mvn clean install -Dmaven.test.skip=true临时跳过测试编译。
  • 检查OpenAPI配置文件:确认BookingService.yaml中没有自定义扩展(如x-codegen-test)强制生成测试文件,如有则移除相关配置。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 16:27:36