Maven surefire arguments when running JUnit test via eclipse - eclipse

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!

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.

Related

My JUnit tests executes with maven build in Java 11, but unable to run same Junit tests via RunIT->Run As->Units, which always says not JUnits found

My module test project is supposed to be built using Java 11 as it is referencing to a jar which is build in Java 11. Hence I had to compile my code in Java 11. When I do Maven clean install, my JUnit test cases are executed without any issue. But when I tried to run my test cases via RunIT->Run As-> JUnit tests, it says "no JUnit tests found"
When I first set up this project, I was able to run via RunIT->Run As-> JUnit tests, which worked absolutely fine. But I had another module in my workspace, which was working on Java 1.8. After testing the tests in that module and coming back to my test project, this stopped working.
When I set compiler to Java 1.8, my tests are identified
Following are the configurations I have tried with
I am working on Eclipse Java 2019-03, which supports Java 11
I have set Preferences-> java -> Compiler -> Compiler compliance level to 11, and Installed JREs point to Java 11.
Project -> Properties -> Java Build Path -> Libraries -> set to Java 11
Project -> Properties -> Java Compiler -> set to Java 11
In my module pom, I am referencing to JUnit 4
While executing RunIT-> Run As -> Run Configurations, I have selected Java 11 JRE.
I have done maven clean install, and all the test-classes are present in the expected target folder. I have Maven-> clean, deletes the target folder.
After building my project via Maven, I have done "Maven Update" as well.
In my module pom file, I have set compiler version
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>11</source>
<target>11</target>
<encoding>${project.build.sourceEncoding}</encoding>
<testSource>11</testSource>
<testTarget>11</testTarget>
</configuration>
</plugin>
The issue got solved by opening the Java 11 project in different workspace. Before that I ensure that JAVA_HOME is set with to JAVA 11 and then open the eclipse.

Eclipse NoClassDefFoundError when running Elasticsearch JUnit tests

I am trying to run some Unit Tests written for a Mavenized project in Eclipse. The tests run perfectly fine from the command line. They also run perfectly fine if I open up Eclipse and run the tests directly off the POM file (i.e. right click on pom.xml and say run maven tests or whatever).
However, I get the following error when I run the tests as JUnit tests in Eclipse:
java.lang.NoClassDefFoundError: Could not initialize class org.elasticsearch.test.ESIntegTestCase
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:340)
at com.carrotsearch.randomizedtesting.RandomizedRunner$1.run(RandomizedRunner.java:562)
I am using Elasticsearch version 2.1.0. The elasticsearch-2.1.0-tests.jar that contains org.elasticsearch.test.ESIntegTestCase is listed as a dependency in pom.xml and all Maven Dependencies show up in my Eclipse project's build path. I even tried specifically adding the jar to my buildpath and to the run configuration's classpath in Eclipse. Both do not work. Cleaning my project via Project -> Clean... also does not work.

How do I get Eclipse to respect the #FixMethodOrder JUnit directive?

I’m using Eclipse Kepler (Java 8). with my Maven (v 3.2.3) project. I’m using JUnit 4.11, as declared in my pom.xml file …
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
I’m trying to run some JUnit tests and I have this at the top of my tests
#FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class MyTest
{
However, when I run the test in Eclipse (by right clicking the name of the file and selecting “Run As”, “JUnit Test”), Eclipse is not running the tests in ascending order by name, but instead running the tests one by one as they are listed in the file.
My question is, how can I get Eclipse to respect the “FixMethodOrder” directive in my file? Note that I’m not interested in lectures about why it shouldn’t matter what order you run JUnit tests in.
Open your project build path (right-click on project > Build Path > Configure Build Path...) and go to the Libraries tab. Do you have "JUnit 4" on your Eclipse classpath or just "Maven Dependencies":
Remove it by selecting "JUnit 4" and hitting the remove button to the right. Maven specifies all your dependencies so you don't need Eclipse adding to that.
In my experience Eclipse seems to prefer its own JUnit library at runtime over the JUnit library provided by "Maven Dependencies": I get different results in test execution order between Eclipse and direct Maven when using #FixMethodOrder if the Eclipse "JUnit 4" library is on the classpath, but if I remove that library then Eclipse and Maven are in agreement.

Running out of memory with gwt:run-codeserver

I have a large GWT application that I'd like to test through Super Dev Mode.
When running the app with
mvn gwt:run-codeserver
it will compile OK and provide the URL for the server. At this point I fire up my app in a web browser and try to compile it again use the bookmarklets. This second compile brings up a range of memory errors like:
Caused by: java.lang.OutOfMemoryError: Java heap space
...
[ERROR] Out of memory; to increase the amount of memory, use the -Xmx flag at startup (java -Xmx128M ...)
So it's no secret that I need to assign more memory to the Java process running the server. What I can't work out is how to do that.
I have the following in my pom.xml
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>gwt-maven-plugin</artifactId>
<version>${gwt.maven.version}</version>
<configuration>
<extraJvmArgs>-Xmx2048M -Xms1024M -Xss1024k -XX:MaxPermSize=256M -XX:PermSize=128M</extraJvmArgs>
</configuration>
</plugin>
My MAVEN_OPTS environment variable is set to
MAVEN_OPTS=-Xmx2048M
And I have tried executing maven with
mvn -Dgwt.extraJvmArgs=-Xmx2048M gwt:run-codeserver
I have also tried it with the Oracle JDK and the open source JDK supplied with Fedora.
Nothing makes any difference. What am I doing wrong here?
UPDATE
I can confirm that this is an issue with the Maven plugin, and not the code. Running the project using IntelliJ's built in support for Super Dev Mode allow me to compile and recompile the application just fine. So this boils down to knowing the correct way to increase the memory that the Maven plugin has access too. It is obviously not one of the ways I have tried above...
Ok, so this was my fault. The POM.XML had some settings for the gwt plugin used when compiling against a profile, which had the memory settings configured correctly, and then another set of gwt plugin settings not tied to a profile (which I had not noticed).
So when I was compiling for a specific profile (the app server), everything was fine. The settings defined in the profile were being applied, and everything worked as you would expect. But when I running in superdevmode without any profiles set, the second, less specific settings were being used over and above anything I was setting on the command line.
I ended up debugging the problem using the -X maven flag, which dumps out the configuration actually used by the gwt plugin. If anyone has the same problem, look for the output like the sample below. From this you can tell what setting was finally applied, which you can then use to find out where it is set.
[DEBUG] --- exit fork of org.jboss.pressgang.ccms:pressgang-ccms-ui:1.0-SNAPSHOT for org.codehaus.mojo:gwt-maven-plugin:2.5.1-rc1:run-codeserver (default-cli) ---
[DEBUG] -----------------------------------------------------------------------
[DEBUG] Goal: org.codehaus.mojo:gwt-maven-plugin:2.5.1-rc1:run-codeserver (default-cli)
[DEBUG] Style: Regular
[DEBUG] Configuration: <?xml version="1.0" encoding="UTF-8"?>
<configuration>
<bindAddress>${gwt.bindAddress}</bindAddress>
<codeServerPort>${gwt.codeServerPort}</codeServerPort>
<extraJvmArgs default-value="-Xmx1024m">-Xmx1024m</extraJvmArgs>
<gen default-value="${project.build.directory}/.generated">${gwt.gen}</gen>
<genParam default-value="true">${gwt.genParam}</genParam>
<generateDirectory default-value="${project.build.directory}/generated-sources/gwt"/>
<gwtSdkFirstInClasspath default-value="false">${gwt.gwtSdkFirstInClasspath}</gwtSdkFirstInClasspath>
<inplace default-value="false">${gwt.inplace}</inplace>
<jvm>${gwt.jvm}</jvm>
<localRepository>${localRepository}</localRepository>
<logLevel default-value="INFO">INFO</logLevel>
<module>${gwt.module}</module>
<persistentunitcache>${gwt.persistentunitcache}</persistentunitcache>
<persistentunitcachedir>${gwt.persistentunitcachedir}</persistentunitcachedir>
<pluginArtifacts>${plugin.artifacts}</pluginArtifacts>
<project>${project}</project>
<remoteRepositories>${project.remoteArtifactRepositories}</remoteRepositories>
<style default-value="OBF">${gwt.style}</style>
<version>${plugin.version}</version>
<warSourceDirectory default-value="${basedir}/src/main/webapp"/>
<webappDirectory default-value="${project.build.directory}/${project.build.finalName}">${gwt.war}</webappDirectory>
</configuration>

"Plugin not found for prefix" error in Eclipse

In Eclipse I imported a maven-based project which uses maven jetty plugin. If I run mvn jetty:run from command line, everything works fine. If I add a run configuration in Eclipse and try to run it, I get the error message:
[ERROR] No plugin found for prefix 'jetty' in the current project and in the plugin groups [org.apache.maven.plugins, org.codehaus.mojo] available from the repositories [local (/home/eugene/.m2/repository), central (http://repo1.maven.org/maven2)] -> [Help 1]
In the Eclipe run configuration, I use:
Base directory: ${project_loc}
Goal: jetty:run
Maven Runtime: External
I read the [Help1] page. I don't have pluginGroup settings in maven configuration files, but I have the jetty plugin mentioned in pom.xml, so I guess everything should be fine (especially because everything works in command-line). I tried to "Run as > Maven clean" in Eclipse before executing the jetty run configuration, but it didn't help. Project compiles and passes all tests, only jetty:run does not work in Eclipse.
Please help, I'm an Eclipse & Maven newbie. Thanks in advance.
It does not work for me from command-line either.
Can you check if it works after adding the following in settings.xml as documented?
<pluginGroups>
<pluginGroup>org.mortbay.jetty</pluginGroup>
</pluginGroups>
Also note that there are two different versions of the plugin - the older maven jetty plugin and the newer jetty maven plugin.
I met this problem too, an easier way to solve this is to edit your pom.xml, add following plugin:
<project>
...
<build>
<plugins>
...
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>7.6.8.v20121106</version>
</plugin>
...
</plugins>
</build>
...
</project>
Note:
jetty-maven-plugin is used for jetty version 7 and above, if you want jetty version 6, you should use maven-jetty-plugin
for the version, you may want to have a look at here and here for your desired version's full name.
I apologize for wasting your time. Now I looked through maven warnings which appeared in Eclipse console after I ran the run configuration. I noticed
[WARNING] Failed to retrieve plugin descriptor for Plugin [org.mortbay.jetty:maven-jetty-plugin]: null
so it became obvious why it couldn't recognize jetty: prefix. Couple of lines above I saw a bunch of warnings about missing plugin versions. So I added a version specification for the jetty plugin (<version> entry in pom.xml) and it solved the problem. I forgot a common rule that if something breaks the first thing to check is warnings you get.
I've got this issue in eclipse after importing an appengine project.
The target:
mvn appengine:devserver
The error:
[ERROR] No plugin found for prefix 'appengine' in the current project and in the plugin groups [org.apache.maven.plugins, org.codehaus.mojo] available from the repositories [local (/Users/averasko/.m2/repository), central (http://repo.maven.apache.org/maven2)] -> [Help 1]
The problem was in the incorrect base directory in the eclipse run configuration. When maven does not see a pom.xml file and is asked to run some non-standard target if fails like this as it don't know anything about the plugin that defines the target.
The solution is to correct the base directory to wherever your pom.xml file resides.
if you following this tutorial:
http://tapestry.apache.org/creating-the-skeleton-application.html
don't forget to follow this one crucial instruction:
Change into the newly created directory, and execute the command: