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

如何在Eclipse中通过POM配置跳过Maven集成测试并运行测试及集成测试类

在Eclipse中运行普通测试与集成测试

一、直接运行单个/批量测试类

不管是后缀为Tests的普通测试类,还是IntegrationTests(简称IT)的集成测试类,在Eclipse里都能快速执行:

  • 针对单个测试类:找到目标测试文件(比如你提供的SimpleTest),右键点击它,选择 Run As > JUnit Test,就能直接跑这个类里的所有@Test方法
  • 针对整个测试包:右键点击测试包(比如org.xmlcml.it),选择 Run As > JUnit Test,包内所有带@Test注解的测试方法都会被执行

二、通过Maven命令在Eclipse中执行测试

如果你习惯用Maven的阶段逻辑来运行测试,可以用Eclipse的Maven Build功能:

  1. 右键点击项目,选择 Run As > Maven Build...
  2. 在弹出的对话框里,Goals 输入框填对应的命令:
    • 运行普通测试:clean test(由Maven Surefire插件负责执行)
    • 运行集成测试:clean verify(由Maven Failsafe插件负责执行,verify阶段会触发集成测试的运行)
  3. 点击 Run 就能按Maven的逻辑执行测试了

通过POM配置在Eclipse中跳过Maven集成测试

要让Eclipse里执行Maven命令时自动跳过集成测试,我们可以通过配置Maven Failsafe插件(专门处理集成测试的插件)来实现,这里提供两种常用方案:

方法一:全局默认跳过集成测试

直接在Failsafe插件的配置里设置skip为true,这样不管是在Eclipse还是命令行执行Maven命令,都会跳过集成测试:

<build>
    <plugins>
        <!-- 配置Failsafe插件处理集成测试 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>3.2.2</version> <!-- 建议使用最新稳定版本 -->
            <configuration>
                <skip>true</skip> <!-- 全局开启跳过集成测试 -->
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

方法二:通过Profile灵活控制(推荐)

如果需要在“跳过集成测试”和“执行集成测试”之间灵活切换,可以配置Maven Profile:

<profiles>
    <profile>
        <id>skip-integration-tests</id>
        <properties>
            <skipITs>true</skipITs> <!-- 定义控制跳过的属性 -->
        </properties>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-failsafe-plugin</artifactId>
                    <version>3.2.2</version>
                    <configuration>
                        <skip>${skipITs}</skip> <!-- 引用属性来控制是否跳过 -->
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>integration-test</goal>
                                <goal>verify</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

在Eclipse中启用这个Profile的步骤:

  • 打开 Window > Show View > Other...,找到 Maven > Maven Profiles 视图
  • 在视图里勾选skip-integration-tests,之后执行Maven命令就会自动跳过集成测试
  • 需要执行集成测试时,取消勾选这个Profile即可

补充:确保测试类被Maven正确识别

Maven默认通过命名规则区分普通测试和集成测试:

  • 普通测试类:以Test结尾(比如SimpleTest),由Surefire插件执行
  • 集成测试类:以IT或IntegrationTest结尾,由Failsafe插件执行
    如果你的类名不符合这个规则,可以在插件里配置includes来指定匹配模式,比如:
<!-- 配置Surefire插件匹配自定义命名的普通测试类 -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.2.2</version>
    <configuration>
        <includes>
            <include>**/*Tests.java</include> <!-- 匹配所有以Tests结尾的类 -->
        </includes>
    </configuration>
</plugin>

内容的提问来源于stack exchange,提问作者peter.murray.rust

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 04:03:07