如何使在Eclipse中运行JUnit测试的行为类似于在命令行上通过Maven运行测试 [英] How do I make running a JUnit test in Eclipse behave like running the test through Maven on the command line

查看:97
本文介绍了如何使在Eclipse中运行JUnit测试的行为类似于在命令行上通过Maven运行测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I’m using Eclipse Kepler and Maven 3.2.3. I have imported my projects using the M2Eclipse plugin. Normally, when I run a JUnit test on the command line, like

mvn clean test -Dtest=MyJunitTest

在执行测试之前,有些事情在"process-resources"和"process-classs"阶段中运行.在Eclipse中,当我在编辑器中打开JUnit测试文件时,右键单击类名(例如"MyJUnitTest"),然后右键单击"Run As"和"JUnit Test",这些阶段似乎都没有运行.所以我的问题是,如何右键单击Junit Test并选择"Run As"->"JUnit Test",就像在命令行上键入"mvn clean test -Dtest = MyJunitTest"一样?

there are some things that run in the "process-resources" and "process-classes" phases before my test is executed. In Eclipse, when I open the JUnit test file in an editor, right click the class name (e.g. "MyJUnitTest") and right click on "Run As" and "JUnit Test", those phases do not seem to be getting run. So my question is, how can I make right-clicking on my Junit Test and selecting "Run As" -> "JUnit Test" behave as if I had typed "mvn clean test -Dtest=MyJunitTest" on the command line?

推荐答案

我准备了以下内容(灵感来自 user3254289的答案):

I prepared the following (inspired by user3254289's answer):

<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, :-),并通过运行"图标()下拉列表.哎呀!就像一个魅力!

After I had all this set up I selected a JUnit class (not MavenTestRunner, of course :-) and invoked the run configuration created above via the Run icon's () dropdown list. Voilà! Like a charm!

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

The only shortcoming is that it can't be invoked via a resource's context menu. Anyone keen to write an Eclipse plugin?

这篇关于如何使在Eclipse中运行JUnit测试的行为类似于在命令行上通过Maven运行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆