Configure in Maven that Eclipse follows maven-surefire-plugin configuration - eclipse

We got following situation: We have unit and integration tests. Unit test classes end with Test and integration tests end with ITest. To execute only unit tests at Maven build we use following configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<excludes>
<exclude>**/*ITest.java</exclude>
</excludes>
</configuration>
</plugin>
That's working in Maven build, but when I'm executing tests in Eclipse IDE via context menu > Run as > JUnit test it ignores these excludes and tests with ITest at the end fail.
Is there a way to configure (in POM) that Eclipse follows maven-surefire-plugin configuration?

Run as > JUnit test does not know about your pom.xml.
But if you place your unit tests and integration tests in different directories (the Maven convention suggests src/test/java and src/it/java), then you can use Eclipse’s ability to run all tests in a package or source folder; simply click on the integration-test source folder and select Run as > JUnit test from its context menu.

Related

Why would mvn clean install and eclipse differ in junit tests they run?

I have differences between Junit tests when I run them inside Eclipse and when they are run by maven surefire pluging doing an mvn clean install from a terminal
On one project, when I right click on src/test/java in Eclipse,Junit tells me there are 137 tests run. Doing mvn clean install gives me only 119. On this one, it seems that case in test name might be a possible explanation some of tests do not start with lower case and this makes surefire ignore them but are there any other possible explanation?
On a second project, I have a more annoying problem : en entire test package is not run by mvn clean install. I have 2 packages under src/test/java : com.project and com.project.services. Test classes under com.projectare run correctly by surefire, not the ones under com.project.services.
The only specificity I can see is classes under com.project.services have several level of inheritance :
public class ActualTestsCasesA extends GenericTestSituationA {}
public class GenericTestSituationA extends ServicesAbstractTests {}
public abstract ServicesAbstractTests extends ProjectAbstractTests {}
ActualTestsCasesA, GenericTestSituationA and ServicesAbstractTests are all under com.project.services test package. ProjectAbstractTests stays in an other maven project.
Here is the dependency to surefire plugin in my pom.wml :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<forkMode>always</forkMode>
<encoding>${project.build.sourceEncoding}</encoding>
<sourceEncoding>${project.build.sourceEncoding}</sourceEncoding>
</configuration>
</plugin>
As you already discovered, Surefire has a specific naming convention when running tests. You can, however, configure additional naminig conventions to match your own project's test filenames. This is helpful for legacy tests that may not have been adhering to the Maven standard, or for a large suite of test classes you would rather not refactor.
Check out the Surefire documentation for details: http://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html
In your case, you could configure Surefire to include test classes with additional patterns like so:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<forkMode>always</forkMode>
<encoding>${project.build.sourceEncoding}</encoding>
<sourceEncoding>${project.build.sourceEncoding}</sourceEncoding>
<includes>
<include>**/*Tests*.*</include>
<include>**/*TestSituation*.*</include>
</includes>
</configuration>
</plugin>
Eclipse, however, isn't bound by such restrictions. Instead, it relies on the existence of the junit4 library to run tests, and allows jUnit itself to determine if a class is considered a runnable test or not. See the Eclipse Mars docs for a little more info.

Errors when executing tests in Tycho but not in Eclipse

I have a set of test cases that use the Eclipse WorkbenchPage and a couple of other classes to perform a set of functions.
When I execute the test bundle in Eclipse, all the test results are green. But when I "clean install" the same package in the command prompt, the build fails and shows test failures in my test classes.
What could be the problem here? I tried debugging my code from Maven but it didn't help at all.
Tycho and Eclipse differ in how they determine the test runtime:
In Eclipse, by default the entire target platform and all projects from the workspace are included in the test runtime.
In Tycho, only the test bundle/fragment and its transitive dependencies are part of the test runtime. If your test has implicit dependencies, e.g. on a bundle which provides some UI via an extension point, you need to explicitly configure these in Tycho.
With the following build configuration, you can for example include the feature org.eclipse.rcp and all its transitive dependencies into the test runtime:
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>target-platform-configuration</artifactId>
<version>${tycho-version}</version>
<configuration>
<dependency-resolution>
<extraRequirements>
<requirement>
<type>eclipse-feature</type>
<id>org.eclipse.rcp</id>
<versionRange>0.0.0</versionRange>
</requirement>
</extraRequirements>
</dependency-resolution>
</configuration>
</plugin>

Redeploy plugin into eclipse installation

I have a eclipse plugin that contains JUnit Tests developed using SWTBot.
I'm trying to run the test against a eclipse installation using tycho-surefire-plugin on a hadless mode. Here is my maven conviguration:
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-surefire-plugin</artifactId>
<version>${tycho-version}</version>
<configuration>
<useUIThread>false</useUIThread>
<testRuntime>p2Installed</testRuntime>
<work>${work.dir}</work>
<useUIHarness>true</useUIHarness>
<useUIThread>false</useUIThread>
<argLine>${ui.test.args}</argLine>
<appArgLine>${ui.test.vmargs}</appArgLine>
<application>com.myapplication</application>
</configuration>
</plugin>
I'm running test using:
mvn verify
Everything is ok except that fact that if I'm doing some code change on my plugin the newer version of the code is not deployed into eclipse installation.
Before running mvn verify I'm running a mvn install command.
If I'm adding a new test class I get
Caused by: org.apache.maven.surefire.util.NestedRuntimeException: Unable to create test class 'com.tests.MyNewClassTest'; nested exception is java.lang.ClassNotFoundException:
I there any configuration that I'm missing. How can I redeploy test plugin?
Incremental builds, i.e. builds without clean require that the build plug-ins correctly cope with the build results in the target folder from the previous execution. This is apparently not the case for the tycho-surefire-plugin.

Ignoring Maven/Scala unit test files

I am writing my unit test cases for a Java project using Scala (JUnit 4). I am running the tests using Maven.
I have written a src/test/scala/com.xxx.BaseTest class for the tests to provide some common functionality (#BeforeClass, etc.), but no actual #Test cases.
Whenever I run the tests using mvn on the command line, it insists on trying to look for tests in the BaseTest class, and gets an error because there are none present.
Other than using an #Ignore, is there any way to have Maven/Scala/Surefire not try to run the BaseTest class? Adding the #Ignore is not a big deal, but my test run shows one more test than I actually have with the label "Skipped: 1".
UPDATE: I found a solution. I renamed BaseTest to Base; Maven now ignores it. Is there any other way?
You can either rename the base test class not to have *Test ending, for example BaseTestCase.java. This is what I would recommend.
Most likely maven executes tests with surefire plugin, so alternatively you just can configure surefire plugin to skip BaseTest.java. I think, by default surefire assumes that all classes ending with *Test are test classes. Something like this in the pom.xml.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.6</version>
<configuration>
<excludes>
<exclude>**/BaseTest.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>

GWT Maven and web.xml

I'm using the GWT Maven plugin from Codehaus with m2eclipse. Where is my web.xml file supposed to end up? Isn't the Maven build supposed to copy it to the /war directory? I can't see it there. Or does Jetty pick it up automatically from src/main/webapp/WEB-INF/?
Here's a relevant section from my pom.xml.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<warSourceDirectory>war</warSourceDirectory>
<webXml>src/main/webapp/WEB-INF/web.xml</webXml>
</configuration>
</plugin>
I believe web.xml (and everything else under src/main/webapp/) gets copied into target/<projectname>-<version>/ during the normal maven lifecycle (For example, when you run mvn install).
If you're running any of the gwt-maven plugin goals, then check out this link.
When running gwt:run, if you want to run the full web app just as if you have built and deployed a war, I found the best way is to add the following to the configuration for the gwt-maven plugin:
<hostedWebapp>
${project.build.directory}/${project.build.finalName}
</hostedWebapp>
This tells gwt-maven plugin to look for the web.xml (and all the other parts of the war file) under target/<projectname>-<version>/. So make sure to either run mvn install first (or mvn war:exploded), then run mvn gwt:run and you should be set.