Ignoring Maven/Scala unit test files - scala

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>

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.

How to correctly use play2 with maven, play2-maven-plugin and IDEA?

I setup a play2 project with Maven like in this example, adapted to Play 2.4 as per the official documentation of the play2-maven-plugin. I am using IntelliJ IDEA.
Now I have a simple controller:
object FooController extends Controller {
val foo = Action {
Ok("foo")
}
val bar = Action {
Redirect(routes.FooController.foo())
}
}
my routes file contains:
GET /foo controllers.FooController.foo
GET /bar controllers.FooController.bar
Now, I ran into several problems.
The first problem was FooController not finding routes. I had to manually do mvn play2:routes-compile on the command line to have the routes generated, but they are in an excluded directory named target/src_managed/main. To get them in scope, I have to use the build-helper-maven-plugin like so:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>${build-helper-maven-plugin.version}</version>
<executions>
<execution>
<phase>process-sources</phase>
<goals><goal>add-source</goal></goals>
<configuration>
<sources>
<source>${project.build.directory}/src_managed/main</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
The routes work after I do mvn clean play2:routes-compile again, and IDEA shows no errors. However, I must avoid touching the managed sources folder, and avoid opening any generated source files in there, even only for looking at them, because there will be other reference problems which are really not there, but they are there because IDEA does not figure out it is a Play2 project. When doing a mvn package play2:run on the command line, the application starts and works as expected.
Unfortunately, I currently must use Maven because it is a big Maven project with many submodules I cannot change to SBT in a few hours/days.
So, is there any advice on how to correctly use this and get IDEA to recognize my project as Play2? Is it possible to auto-run the play2:resources-compile goal when I select Build/Rebuild Project or Make module?
Use one of example projects instead of this.
I use ScalaIDE, but I've tested Idea integration and it works. Import Maven project, add Scala support, rebuild.

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>

How can I run a Maven webapp in Eclipse when I need resource filtering for properties files?

I need to get some updates on this issue, I found this thread back in 2009 here, but the answer was to use maven 2, I'm not sure if Q4E works with maven 3 or not. I need to have some properties files filtered during the mvn package phase for the resulting war to be functional, the resource filtering is working fine with CLI mvn install. But when I do "Run on server/debug on server", the filtering is not working any more.
The aforementioned thread author ended up using q4e, claiming q4e gets the resource filtering right. I have q4e installed as well along with m2e, but still doesn't work, so I don't know if q4e is not working with maven 3, or I'm doing something wrong.
Thanks,
David
updated to the latest m2e-wtp plugin 0.15 (resource filtering bug fix since 0.12), it works fine now.
I'm not sure if this matches your problem, but I wanted to populate my web.xml file with properties from the pom during build and I put a groovy script in the pom to do it. It worked a treat and might work for you too. It definately works in both eclipse and on the command line. Here is my pom fragment:
<plugin>
<!-- Groovy script to set the description and version in the web.xml display name -->
<groupId>org.codehaus.groovy.maven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<id>groovy-magic</id>
<phase>prepare-package</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>
def file = new File("src/main/webapp/WEB-INF/web.xml");
def fileText = file.text;
def match = "<display-name>[^<]*</display-name>";
def replace = "<display-name>"+project.description+" "+project.version+"</display-name>";
fileText = fileText.replaceAll(match, replace);
file.write(fileText);
println "Updated web.xml"
</source>
</configuration>
</execution>
</executions>
</plugin>

Including scala-library.jar in Maven generated package

I'd like to use Maven to include all the dependencies needed to run any Scala programs I write. I imagine this would mean at least scala-library.jar as well as any libraries I may use.
I don't mind where these dependencies are stored (inside the generated JAR or outside), I'm just looking for a solution that sets up stuff like the manifest file classpath and generally requires a minimum amount of manual intervention and boilerplate configuration.
Thanks.
You can use the jar-with-dependencies descriptor format that comes with the Assembly plugin:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
When you run mvn assembly:assembly you'll get a jar with all dependencies (including any necessary Scala libraries) in your target directory.
Use scala-archetype-simple archetype. Here are the list of other archetypes.