8

A newbie question on Maven - Surefire - Eclipse - JUnit

I have configured the maven-surefire-plugin in the pom file of my project to pass some additional JVM arguments as below:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${maven.surefire.plugin.version}</version>
            <configuration>
                <argLine>-d64 -Xms128m -Xmx4096m -XX:PermSize=512m -Duser.timezone=UTC -XX:-UseSplitVerifier</argLine>
            </configuration>
        </plugin>

When I run a test case of this project from Eclipse as Run As->JUnit Test, though the classpath is correctly set, the additional arguments specified in the argLine are not included in the invocation. I have to go and manually key in the arguments under the relevant Debug Configurations. I don't quite understand how JUnit is aware that it needs to put jars of the test scope on the classpath and in some way means that JUnit tool in Eclipse is aware of Maven via M2E? If so, how can we make it also read argLine. I know this sounds very specific - but how do others manage in similar situations?

Thanks in advance!

Kilokahn
  • 2,281
  • 1
  • 25
  • 50

1 Answers1

9

Eclipse JUnit Launcher (choose Run As -> JUnit Test) is a independent test runner which has its own pre-defined build and running life cycle and has nothing to do with Maven, it will not pick up your pom magically and read in the surefire configuration and use them to drive the test running.

If your project is imported as an existing Maven project, use Maven (choose Run as -> Maven test) launch your JUnit test which will pick up and use the surefire configuration. This is exactly same as running mvn test from commandline, it only output run log in console and you will not able to use the nice red & green JUnit UI window.

Hope this make sense.

yorkw
  • 40,926
  • 10
  • 117
  • 130
  • if Eclipse JUnit is an independent test runner, how does it know the jars that it needs to put on the classpath based on the test scope? – Kilokahn Sep 13 '12 at 04:42
  • Generally speaking, it is handled by m2eclipse. When import existing maven project, it is actually m2eclipse that dominate project setup, i.e. generate Eclipse metadata file .project & .classpath, fill up project classpath (of cause from pom.xml) and etc. Once project was successfully imported into IDE, m2eclpise pretty much done its job. From here, if you like, you are free to do your job (coding/debuging/testing) without involve m2eclipse. AFAIK, there is no association between m2eclipse and Eclipse JUnit Launcher. – yorkw Sep 13 '12 at 05:06
  • 1
    thanks for the info! Then the next step would be to see if m2eclipse can also read the surefire plugin config's argLine and somehow populate the debug configuration of the tests in that project to have the arguments from the argLine, right? Or this asking for too much? :) – Kilokahn Sep 13 '12 at 05:20
  • You can use either `Run as -> Maven test` or `Run As -> JUnit Test` with manual configuration (explicitly add those arguments in the run configuration). – yorkw Sep 13 '12 at 08:42
  • 1
    I like the red green thingy that comes up when I run JUnit test and the ease with which I am able to see exceptions and test results as compared to running the Maven test. But adding those arguments over and over for all test cases manually is a pain. However, if there are no options left, we don't have a choice! :) – Kilokahn Sep 14 '12 at 04:20
  • 8
    You can view Surefire's test reports in Eclipse's JUnit view if you open the XML files in target/surefire-reports. – thSoft Mar 13 '13 at 13:11
  • 1
    m2e hooks into a lot of places, including the junit test runner launcher. It would be perfectly valid to have it inject this information. – Thorbjørn Ravn Andersen Jun 12 '14 at 13:28