如何在Eclipse中运行JUnit测试就像在命令行上通过Maven运行测试一样

时间:2015-06-10 21:08:22

标签: eclipse maven junit m2eclipse eclipse-kepler

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

mvn clean test -Dtest=MyJunitTest

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

2 个答案:

答案 0 :(得分:0)

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

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

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

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

答案 1 :(得分:0)

我准备了以下内容(灵感来自 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, :-)并通过Run图标的()下拉列表调用上面创建的运行配置。瞧!喜欢魅力!

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