【问题标题】:How do I make running a JUnit test in Eclipse behave like running the test through Maven on the command line如何使在 Eclipse 中运行 JUnit 测试的行为类似于在命令行上通过 Maven 运行测试
【发布时间】:2015-06-10 21:08:21
【问题描述】:

我正在使用 Eclipse Kepler 和 Maven 3.2.3。我已经使用 M2Eclipse 插件导入了我的项目。通常,当我在命令行上运行 JUnit 测试时,比如

mvn clean test -Dtest=MyJunitTest

在我的测试执行之前,有些东西会在“流程资源”和“流程类”阶段运行。在 Eclipse 中,当我在编辑器中打开 JUnit 测试文件时,右键单击类名(例如“MyJUnitTest”)并右键单击“Run As”和“JUnit Test”,这些阶段似乎没有运行。所以我的问题是,我怎样才能让右键单击我的 Junit 测试并选择“运行方式”->“JUnit 测试”表现得就像我在命令行上输入了“mvn clean test -Dtest=MyJunitTest”一样?

【问题讨论】:

    标签: eclipse maven junit m2eclipse eclipse-kepler


    【解决方案1】:

    我准备了以下内容(灵感来自 user3254289's answer):

    扩展项目 POM SO-30767338/pom.xml

    <dependency>
      <groupId>org.apache.maven.shared</groupId>
      <artifactId>maven-invoker</artifactId>
      <version>2.2</version>
    </dependency>
    

    已实现 SO-30767338/src/test/java/MavenTestRunner.java

    import static java.lang.System.out;
    
    import java.io.File;
    import java.util.Collections;
    import java.util.Properties;
    
    import org.apache.maven.shared.invoker.*;
    import org.junit.Test;
    
    public class MavenTestRunner
        {
        @Test
        public void testUnit()
            {
            String thisClassName = this.getClass().getSimpleName();
            if ( System.getProperty( "unit" ).contains( thisClassName ) )
                {
                throw new IllegalArgumentException(
                    String.format( "%s cannot be tested with %1$s.", thisClassName ) );
                }
    
            Properties properties = new Properties();
            properties.put( "test", System.getProperty( "unit" ) );
    
            out.printf( "Testing %s in project %s.\n",
                properties.getProperty( "test" ), System.getProperty( "pom" ) );
    
            InvocationRequest request = new DefaultInvocationRequest();
            request.setPomFile( new File( System.getProperty( "pom" ) ) );
            request.setGoals( Collections.singletonList( "test" ) );
            request.setProperties( properties );
    
            try
                {
                Invoker invoker = new DefaultInvoker();
                InvocationResult result = invoker.execute( request );
    
                if ( result.getExitCode() != 0 )
                    {
                    throw new IllegalStateException(
                        String.format( "Test build of %s failed.", System.getProperty( "unit" ) ) );
                    }
                }
            catch ( MavenInvocationException e )
                {
                e.printStackTrace();
                }
            } // testUnit()
        } // MavenTestRunner
    

    根据{workspace}/.metadata/.plugins/org.eclipse.debug.core/.launches/使用Maven.launch测试选择的测试单元创建了一个运行配置

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <launchConfiguration type="org.eclipse.jdt.junit.launchconfig">
    <listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
    <listEntry value="/SO-30767338/src/test/java/MavenTestRunner.java"/>
    </listAttribute>
    <listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
    <listEntry value="1"/>
    </listAttribute>
    <stringAttribute key="org.eclipse.jdt.junit.CONTAINER" value=""/>
    <booleanAttribute key="org.eclipse.jdt.junit.KEEPRUNNING_ATTR" value="false"/>
    <stringAttribute key="org.eclipse.jdt.junit.TESTNAME" value="testUnit"/>
    <stringAttribute key="org.eclipse.jdt.junit.TEST_KIND" value="org.eclipse.jdt.junit.loader.junit4"/>
    <stringAttribute key="org.eclipse.jdt.launching.CLASSPATH_PROVIDER" value="org.eclipse.m2e.launchconfig.classpathProvider"/>
    <stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="MavenTestRunner"/>
    <stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="SO-30767338"/>
    <stringAttribute key="org.eclipse.jdt.launching.SOURCE_PATH_PROVIDER" value="org.eclipse.m2e.launchconfig.sourcepathProvider"/>
    <stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-Dpom=${project_loc}/pom.xml -Dunit=${selected_resource_name}"/>
    </launchConfiguration>
    

    注意VM 参数中的重要部分:

    -Dpom=${project_loc}/pom.xml -Dunit=${selected_resource_name}

    完成所有这些设置后,我选择了一个 JUnit 类(当然不是 MavenTestRunner, :-)并通过运行图标的 () 下拉列表调用上面创建的运行配置。瞧!就像一个魅力!

    唯一的缺点是它不能通过资源的上下文菜单调用。有人热衷于编写 Eclipse 插件吗?

    【讨论】:

    • 您好,我很清楚,您右键单击 JUnit 类名,选择“运行配置”,然后选择“上面调用的运行配置”,这是什么?
    • @Dave No. 根据我的回答中的文件Test selected test unit with Maven.launch 创建运行配置。然后左键单击 (=select) JUnit 类名称 → 选择绿色/白色运行配置图标的 (>) 下拉菜单 → 选择 Test selected test unit with Maven.
    【解决方案2】:

    Maven 和 JUnit 是两个完全不同的工具,每个都有不同的构建 - 运行路径,并且每个都有不同的要求。仅仅因为 Maven可以利用 JUnit 作为其工具链的一部分,并不意味着它们是相同的!

    您显然使您的项目依赖于 Maven 构建路径。让 JUnit 走这条路非常困难,或者完全不可能。

    在 Eclipse 中,使用 m2e 插件,使用 Run As > Maven 构建创建自定义运行是微不足道的。 Documentation is available if you need it.

    此“Maven 构建”选项仅适用于您的项目的顶层,即您的 pom 所在的位置。然后,您必须指定所有选项,例如 -Dtest=MyJunitTest

    【讨论】:

    • 我知道这很难——这就是我问这个问题的原因!当我在 JUnit 文件中右键单击 Junit 测试的类名并选择“Run As”时,没有“Maven build”选项,只有“Run On Server”、“Junit Test”和“Run Configurations”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-03
    • 1970-01-01
    • 2017-07-08
    • 1970-01-01
    • 2011-07-24
    • 2019-07-26
    • 2016-03-22
    相关资源
    最近更新 更多