#SelectPackages not working with Maven CLI - junit4

I run mvn clean package and all other tests run except the suite test. I'm running both Junit 4 and 5 tests - #Rules tests run and #RegisterExtension/#ExtendWith tests also run. Not sure why the suite will not run - any ideas?
The code follows:
Test suite
#SelectPackages("com.company.platform.test.suite")
#IncludeClassNamePatterns({"^.*Suite$"})
public class SuiteTest {
}
Test being called by SuiteTest
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
public class TestingSuite {
#Test
public void add_2_plus_1(){
Assertions.assertThat(2 + 1).isEqualTo(3);
}
}
Pom
<junit-jupiter-engine.version>5.5.0</junit-jupiter-engine.version>
<junit-platform-runner.version>1.5.0</junit-platform-runner.version>
<!-- ellided -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit-jupiter-engine.version}</version>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>${junit-jupiter-engine.version}</version>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>${junit-platform-runner.version}</version>
</dependency>
<!-- ellided -->
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<includes>
<include>**/*Test.java</include>
</includes>
</configuration>
</plugin>
Maven, Java and OS details
mvn11 --version Sun Jul 21 14:43:24 EDT 2019
Apache Maven 3.5.4 (1edded0938998edf8bf061f1ceb3cfdeccf443fe; 2018-06-17T14:33:14-04:00)
Maven home: /usr/local/Cellar/maven/3.5.4/libexec
Java version: 11.0.1, vendor: Oracle Corporation, runtime: /Users/me/Downloads/jdk-11.0.1.jdk/Contents/Home
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.14.5", arch: "x86_64", family: "mac"

So I replicated your problem. When running
mvn clean test
i got the following output (among many others):
Juli 23, 2019 8:17:30 VORM. org.junit.vintage.engine.discovery.DefensiveAllDefaultPossibilitiesBuilder$DefensiveAnnotatedBuilder buildRunner
WARNING: Ignoring test class using JUnitPlatform runner: com.example.project.SuiteTest
That's why I looked up the code of DefensiveAllDefaultPossibilitiesBuilder$DefensiveAnnotatedBuilder and found the following comment:
/**
* Customization of {#link AnnotatedBuilder} that ignores classes annotated
* with {#code #RunWith(JUnitPlatform.class)} to avoid infinite recursion.
*/
private static class DefensiveAnnotatedBuilder extends AnnotatedBuilder {...}
This strongly suggests that suites using #RunWith(JUnitPlatform.class) are ignored when run through the platform mechanism. You might ask the junit 5 team if there is an explicit way to switch that security mechanism off but I'd guess there is none.
I suggest you extend the configuration of maven-surefire-plugin to mimic what you do in the suite:
#SelectPackages("com.company.platform.test.suite")
#IncludeClassNamePatterns({"^.*Suite$"})
can be translated into something like
<include>com.company.platform.test.suite/*Suite.java</include>
I didn't test it though.

Related

How to write a test suite with junit 5 for eclipse?

I am trying to run by junit5 tests from a test suite. however Im getting an error
import org.junit.platform.runner.JUnitPlatform;
import org.junit.platform.suite.api.SelectClasses;
import org.junit.runner.RunWith;
import com.test.studentservice.controllers.StudentControllerTest2;
import com.test.studentservice.repo.StudentServiceRepoDataTest;
import com.test.studentservice.service.StudentServiceTest;
#RunWith(JUnitPlatform.class)
#SelectClasses({StudentControllerTest2.class, StudentServiceRepoDataTest.class, StudentServiceTest.class})
public class StudentServiceTestSuite {
}
This is the error I'm getting:
Thanks
You get this error, because in the JUnit launch configuration (Run > Run Configurations...) in the Test tab, you selected as Test runner the option JUnit 5 instead of JUnit 4.
#RunWith(JUnitPlatform.class) is used for the purpose to run JUnit 5 tests as JUnit 4 tests, in case a tool only supports JUnit 4 but not JUnit 5 (for details see this answer). But you don't need that since Eclipse supports JUnit 5 and should not use it since running it via the JUnit 4 API has some limitations.
Baeldung - A Guide to JUnit 5 - 7. Test Suites which you mentioned in a comment you are following here, is wrong, or at least misleading in this point. Instead of a JUnit Test Suite class, tag your tests and use them to include or to exclude tests (in the JUnit launch configuration, in the Test tab, hit the Configure... button for that).
From October-November 2021 jUnit5 support #Suite annotation:
import org.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.Suite;
import org.junit.platform.suite.api.SuiteDisplayName;
#Suite
#SuiteDisplayName("Build Verification Test")
// Insert the class names in the order of execution
#SelectClasses({
FirstTest.class,
SecondTest.class
})
public class SuiteTest {
}
Pom:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.jupiter.version>5.8.0</junit.jupiter.version>
<junit.platform.version>1.8.0</junit.platform.version>
</properties>
...
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite</artifactId>
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite-api</artifactId>
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite-commons</artifactId>
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite-engine</artifactId>
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
...

Error "Source option 5 is no longer supported. Use 6 or later" on Maven compile

I am getting the following error on $ mvn compile:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project Sym360: Compilation failure: Compilation failure:
[ERROR] Source option 5 is no longer supported. Use 6 or later.
[ERROR] Target option 1.5 is no longer supported. Use 1.6 or later.
[ERROR] -> [Help 1]
Here is the code of my pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test.app</groupId>
<artifactId>Tset</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>Test</name>
<url>http://maven.apache.org</url>
<properties>
<maven.compiler.source>6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
<!-https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-
java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.14.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
<scope>test</scope>
</dependency>
I tried to add properties in the pom.xml code, but still getting the same error.
What helped me was these lines in pom.xml file
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
I had same issue, the problem is with properties. Check your JavaSE version in your project, it will be displayed beside JRE System Library folder in your project. If it is 1.5, then it will throw an error. Most probably you will have a updated version, so check the version and update it. I have updated it below based on your code.
<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
Also in one of my IntelliJ IDEA projects, in addition to all of answers above, another try works:
Just change Language level in Modules section of Project Structure [image below]
I had same issue and i have added below configuration in pom.xml and it works.
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
I think you have wrong your pom.xml:
<properties>
<maven.compiler.source>6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
change to:
<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
Now depending if you are using the command line use:
mvn clean compile
or either way(eclipse ide)
Right click on project Run with maven>build>Goal (compile)
In Eclipse This helped me:
Right Click on Project.
Click on Build path.
Click on Configure Build path.
It opens a Java Build path window.
Click on Java Compiler in the Left side.
It navigates to Java Compiler window in that to set the Compiler compliance level is set as according to your jre version(ex if java version is 1.8
then choose 1.8) as select.
Click on [Apply] button.
Click on [OK] button.
Right click on Project > Maven > Update the project.
Right click on Project > Run As > Maven install -- The pom.xml file is running and java jars are download and installed to project.
Right click on Project > Run As > Maven Test -- The pom.xml file is running and java jars are download and installed to project.
Then you got the Build Success message and your maven project is created successfully.
adding below code in pom will resolve the issue
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<profiles>
I was facing the same issue and resolved it with the lines of code below:
<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
I fixed this by adding this in pom.xml file:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
For the new Apache net Bean its a little bit different from the suggestion by SUPARNA SOMAN
Right Click on your Project
-Hover on "set configuration" and click customize configuration
-.A new dialogue box opens....
At the left corner where the categories are, Click on "Source"
At the select form on the page below, select your required version of JDK ----see image for this last step.the last step required to change jdk version
On MacOS I have multiple versions
user> ls /Library/Java/JavaVirtualMachines/
jdk-11.0.4.jdk jdk-12.0.2.jdk jdk1.8.0_221.jdk
and JAVA_HOME was not defined properly so Maven used jdk-12. I have jdk-11,jdk-8, and jdk-12.
user> mvn -version
Apache Maven 3.6.1
Maven home: /usr/local/Cellar/maven/3.6.1/libexec
Java version: 12.0.2, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk-12.0.2.jdk/Contents/Home
Default locale: XXX, platform encoding: UTF-8
OS name: "mac os x", version: "10.14.6", arch: "x86_64", family: "mac"
So:
Define JAVA_HOME to use jdk-8.
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home
Try again, now maven is:
user> mvn -version
Maven home: /usr/local/Cellar/maven/3.6.1/libexec
Java version: 1.8.0_221, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/jre
and the build is:
[INFO] BUILD SUCCESS
None of the solutions above worked for me.
For some reasons the file with the name of my project.iml had changed. Found the difference in the previous subversion repository submission...
In projectname.iml I found this line:
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_5">
And all I had to do was changing it to 11
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_11">
After so many years, happily seeing the same error ! :P
[ERROR] Source option 5 is no longer supported. Use 7 or later.
[ERROR] Target option 5 is no longer supported. Use 7 or later.
pom.xml did not have any source or target declarations, so maven must have taken some archaic versions as default, rather than being clever enough to get it from the install JVM (which is currently on 18).
So, without knowing the original issue, I could also resolve it through the below snippet below. That declares a specific source and target version, which subpresses the problem.
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>18</source>
<target>18</target>
</configuration>
</plugin>
</plugins>
</build>
Some people have asked:
I understand that this works, but I don't understand why maven uses a default source value that it does not support? –
The answer is:
Maven doesn't know what version of the JDK you will use.
When used with JDK 1.8 Maven compiles work successfully.
When using newer versions of the JDK, it's not clear what version of the byte code you want to use. So Maven's has punted and continues use what it has used for many years.
At this day and age, it is difficult for Maven (as a generic build tool) to select a default byte code version that everyone will like.
So it's probably best to get used to putting the version of your source code and the byte code you want to generate in your pom.xml file.
You can argue (and I would agree) that maven should (by default) use a newer version of the maven-compiler-plugin but as I stated, whatever version was picked, someone would have a problem with it.
Example
For example, if you use JDK 11, you might very well be using Java 11 syntax and need -source 11 -target 11 when compiling your code.
Even the most recent release of the plugin maven-compiler-plugin:3.10.1 defaults to JDK 1.7 syntax which would result in compilation errors for Java 11 code.
A full description of the problem
Other's have said this, but to be complete. The default pom.xml doesn't specify the maven-compiler-plugin. To find out the version used you can use
mvn help:effective-pom | more
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
You'll see that the maven-compiler-plugin:3.1 is being used.
You can see that this version generates a command with -target 1.5 -source 1.5 which generates an error when used with versions of Java newer than Java 1.8.
The following shows the error that occurs when using JDK 17.
mvn --version
Apache Maven 3.8.6 (84538c9988a25aec085021c365c560670ad80f63)
Maven home: D:\p\apache-maven-3.8.6
Java version: 17.0.2, vendor: Oracle Corporation, runtime: D:\p\jdk-17.0.2
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"
mvn clean install -X
. . .
[DEBUG] -d D:\Play\maven\helloworld\target\classes -classpath D:\Play\maven\helloworld\target\classes; -sourcepath D:\Play\maven\helloworld\src\main\java; -g -nowarn -target 1.5 -source 1.5
. . .
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] Source option 5 is no longer supported. Use 7 or later.
[ERROR] Target option 5 is no longer supported. Use 7 or later.
[INFO] 2 errors
The fix
The fix was to update the maven pom.xml file to specify either a newer maven-compiler-plugin. I tested with maven-compiler-plugin:3.10.1 and it uses -target 1.7 -source 1.7
The syntax is:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
</plugin>
</plugins>
</build>
At the time of this writing version 3.10.1 was the latest version.
The other option is to specify the version of the byte code you want to generate, one way is to use this (as stated in another answer):
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
This will work even with the old version 3.1 plugin.
Here is the solution which helped me:
I had the same issue on error source option 5 is no longer supported, Use 6 or later
So i followed these instructions and problem SOLVED
Open Project Properties (File Menu)
Change the Source / Binary Format to the latest version (JDK 7 in my case)
Project Properties
Source / Binary Format
Clean and Build, Then Run the project
This is a message from a newer javac, e.g.:
$ java -version
openjdk version "11" 2018-09-25
$ javac -source 1.5 -target 1.5 Test.java
error: Source option 5 is no longer supported. Use 6 or later.
error: Target option 1.5 is no longer supported. Use 1.6 or later.
So, apparently you're using a newer JDK version with a Maven version prior to 3.8.0 ("<source>/<target> ... NOTE: Since 3.8.0 the default value has changed from 1.5 to 1.6"). The maven-compiler-plugin:3.1 you use is from April 2013.
There are two possibilities:
Update your Maven version to the latest (I'd recommend that)
Setting the Java Version in Maven:
2.2. Java 9 and Beyond
...
<properties>
<maven.compiler.release>...</maven.compiler.release>
</properties>
If in Eclipse, Write click on project and go to properties. Search for maven and configure a jdk higher version (1.7 onwards) there and apply. Now try maven install.
In my case, running MacOS Big Sur and JDK version 15, I added the code below in pom.xml file shown below. I added the 15 for my JDK version.
<properties>
<maven.compiler.source>15</maven.compiler.source>
<maven.compiler.target>15</maven.compiler.target>
</properties>
I then re-ran $ mvn clean wildfly:deploy and it worked.
Both options work for me to resolved Source option 5 is no longer supported. Use 6 or later” on Maven compile
Open pom.xml file
Option1: add build tags
Option2: add properties tags
<project>
<groupId>com.pluralsight</groupId>
<artifactId>HelloWorld</artifactId>
<version>1.0-SNAPSHOT</version>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>12</release>
</configuration>
</plugin>
</plugins>
</build>
</project>
OR
<project>
<groupId>com.pluralsight</groupId>
<artifactId>HelloWorld</artifactId>
<version>1.0-SNAPSHOT</version>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
</project>
I faced the same challenge, what I did was to go to:
File
Project structure
Module
Changed compile SDK to 30 (Latest at the time)
Build tools 30.0.3 (Latest)
Source compatibility (1.8 Java 8)
Target compatibility (1.8 Java 8)
Change the
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5">
to
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_11">
Set updated java version from build path as per you installed
I solve the problem by finding that my $JAVA_HOME is empty. After setting it, compilation succeeded!

Maven Exec plugin, ClassLoader ClassNotFoundException

I've tried many suggestions on StackOverflow, and tested with many combinations of setup and pom.xml for 3 days, none of them works. Please help.
I started with a big project with a lot of dependencies, spring-boot, hibernate, etc. Then I create another small console project which import and use some classes from the big project. They are not parent and child project. All I do is add <dependency/> to the child project pom, like this.
P.S. the big project has a <parent/> spring-boot-starter-parent and use spring-boot-maven-plugin.
<dependency>
<groupId>myGroup</groupId>
<artifactId>bigProject</artifactId>
<version>1.0.1</version>
</dependency>
This works on Eclipse with m2e, I just use "Run as" java application and the small project works.
But I need to upload and run those project on my linux VM, which does not use GUI and Eclipse, only terminal.
Then after some reading, I try to use maven exec plugin to run the small project. My steps:
1. do mvn clean install on the big project, confirmed that it appears in my /.m2, local repository.
2. run mvn compile on small project
3. run mvn exec:java on small project
It fails on step 2, those import ... in the class of small project throw package xxx does not exist. maven fail with compilation error.
Then, I try to simplify the problem. I create two test projects with only 1 class, myLib and HelloWorld console application, then I add myLib dependency (pom). HelloWorld project to print a message from the class in myLib package. run step 1 to 3. It works.
public class App
{
public static void main( String[] args )
{
//SystemLog from big project, does not work
//SystemLog log = new SystemLog();
//log.setValue("test value");
//System.out.println(log.getValue());
//CustomMessage from myLib, works fine
CustomMessage cm = new CustomMessage();
cm.setTheMessage("custom message");
System.out.println(cm.getTheMessage() );
System.out.println(CustomMessage.defaultMsg);
}
}
pom.xml of small project
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>dependOnOthers</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>dependOnOthers</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>test</groupId>
<artifactId>myLibrary</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com</groupId>
<artifactId>bigproject</artifactId>
<version>1.0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.5.0</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<includePluginDependencies>true</includePluginDependencies>
<mainClass>test.dependOnOthers.App</mainClass>
<!-- <arguments> <argument>argument1</argument> </arguments> -->
<!-- <arguments> <argument>-classpath</argument> <argument>target/lib/bigproject-1.0.1.jar</argument>
</arguments> -->
</configuration>
</plugin>
</plugins>
</build>
</project>
Then I add a reference to big project, try to print a log message using HelloWorld, it pass step 2, mvn compile, but when I do step 3, mvn exec:java, java.lang.ClassLoader.loadClass() throw ClassNotFoundException on the line where I new an SystemLog() instance, the class defined in big project.
Summarize:
Goal: try to run a console application using Maven exec plugin, which has a dependency on another project, that is installed in local repo /.m2
The SystemLog class is a model class with hibernate annotations.
1. small project depend on big project, fail on mvn compile, package does not exist.
2. HelloWorld depend on myLib, works fine.
3. HelloWorld depend on big project, fail at runtime with ClassNotFoundException.
It's a little late but i ran in the same error, migrating a project from 1.5.3 to 2.1.4
I have found a solution for this problem.
Spring Boot has changed the jar structure in version 2.0.0+, so you don't need to specify your class as <mainClass>test.dependOnOthers.App</mainClass>, you need to point JarLauncher as main entry point.
When the plugin will execute the jar, java will call the main method JarLauncher and several code of spring will be called after that he reads the manifest and call the class definied in Start-Class:. in your case test.dependOnOthers.App.
<configuration>
<!-- Since version 2 of spring, the jar is packed in
another way and has his main method here, don't worry in the MANIFEST file is described which file to boot -->
<mainClass>org.springframework.boot.loader.JarLauncher</mainClass>
The old strucutre of Spring 1.0, was the classical java way todo it, so the java classloader can load it without problems and refer to the class when it is in the classpath, with the version 2.0.0+, they copy user class files and dependency into BOOT-INF, so the java classloader cannot load it anymore, you need to use the Spring classloader to get the class instance.
I have a workaround and know what is causing the problem, but don't know all the details, hope that someone will comment on this, or elaborate the answer.
The thing that cause this problem is spring-boot-maven-plugin, which I use to run spring-boot embedded tomcat server, using mvn spring-boot:run. This plugin seems to change something on the .jar file, maybe the manifest?
The workaround is to remove this plugin, run mvn clean install, that .jar installed in local repository will work correctly. run mvn compile on the small project. Then add the plugin back to big project, run mvn spring-boot:run
I do not know what the plugin changes that result in a .jar with package not exist, does it changes package name?
My workaround is clumsy, if there is a way to choose to compile with or without spring-boot-maven-plugin by defining maven goal, so that I do not need to change the pom for different build (with/without plugin). That would be a better solution.

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.

JUnit + Maven + Eclipse: Why #BeforeClass does not work?

I am using JUnit 4, Maven 2 and latest Eclipse. Problem is simple: I would like to perform some setup (connecting to a database) before my tests are executed.
I tried #BeforeClass in many different locations but Eclipse and Maven are ignoring this. Any help on accomplishing this initial setup?
Thanks!
public abstract class BaseTestCase extends TestCase {
#BeforeClass
public static void doBeforeClass() throws Exception {
System.out.println("No good #BeforeClass");
// DO THE DATABASE SETUP
}
}
Now the tests extending BaseTestCase:
public class LoginActionTest extends BaseTestCase {
#Test
public void testNothing() {
System.out.println("TEST HERE");
assertEquals(true, true);
}
}
Maven and Eclipse just ignore my #BeforeClass ??? Any other way to perform setup before tests?
Sergio, you were right about extending TestCase causing the problem. If you extend TestCase, JUnit treats your test class as an old (pre JUnit 4 class) and picks org.junit.internal.runners.JUnit38ClassRunner to run it. JUnit38ClassRunner does not know about #BeforeClass annotation. Check out source code of runnerForClass method of AllDefaultPossibilitiesBuilder and runnerForClass method of JUnit3Builder for more details.
Note: This problem is not related to Eclipse or Maven.
I suspect that you are running with JUnit 3. Try renaming your test to something which does not start with "test". If the test is no longer executing, you are using JUnit 3 (which assumes that test methods are methods which starts with "test").
Please post your Eclipse launch config.
I have similar problem and I fixed it by specifying surefile and junit versions explisitly:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.8.1</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>2.8.1</version>
</dependency>
</dependencies>
<configuration>
<parallel>methods</parallel>
<threadCount>10</threadCount>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
</excludes>
</configuration>
</plugin>
More info is here: http://maven.apache.org/plugins/maven-surefire-plugin/examples/junit.html
It seems junit 3.8.1 version is used transiently through maven-resources-plugin and plexus-container-default. You can print dependency tree by calling mvn dependency:tree. I think there's no other way to make surefire use junit 4.