I have some test-classes under the folder src/test/java (some of these having name ending by *Test). Each class has some methods annotated with #Test
Running these tests with JUnit (on Eclipse, right click on the class, then run as, the JUnit Test) I have no problems. But I want to run these tests using mvn test.
The problem is that I obtain always this:
It seems that mvn finds the tests, but it doesn't execute them. Why?
Furthermore, it seems that also classes having a name that doesn't end with *Test are considered by Maven.
This is part of my pom.xml:
And this is part of my effective pom:
You are using a very outdated version of JUnit.
In JUnit 3.x, tests did not use annotations; the method names had to start with test*.
In JUnit 4.x, test methods had to be annotated with #Test, which is what you apparently have.
In your pom, you need to upgrade JUnit:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
Related
I'm just going through a tutorial which is a Maven project in Eclipse, and it should run some tests using JUnit4, so I have put this dependency in the POM:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
Still, trying to run the tests I get the following:
No tests found with testrunner 'JUnit5'
Why is Eclipse not using JUnit4 when this is the only dependency given in the project?
Because in the run configuration you have chosen JUnit 4 as Test Runner, Eclipse uses JUnit 4 instead of JUnit 5 to run this test.
Please note that even though Eclipse preselects the matching Test Runner for new run configurations, it does not change the Test Runner for existing run configurations.
(There was no question for this problem in stackoverflow, so I decided to share question & solution here.)
I wanted to migrate my Spring Boot project from JUnit 4 to JUnit 5, so I added the new API as dependency and removed the old the ones:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
</dependency>
I wanted to do a full migration of my project, so I explicitly excluded everything from JUnit 4, and also did not include the junit-vintage-engine (as recommended in the official migration guide).
Then, I did the various changes to make the tests compile, like replacing #Before with #BeforeEach and organizing imports. All in all, this was pretty straightforward.
But running the tests in Eclipse caused some trouble:
First I got the message "Cannot find 'junit.framework.TestCase' on project build path. JUnit 3 tests can only be run if JUnit is on the build path." In the launch configuration dialog that popped up automatically, I was able to spot my mistake: I needed to select the 'Test runner' JUnit 5.
Still no success. Now I got the message "No tests found with test runner 'JUnit 5'. This was confusing, because I had used the correct JUnit 5 annotations on the test methods. Eclipse even confirmed this because the launch configuration 'Test method' search listed the test methods just fine.
After I while, I figured out that the test execution had printed a stack trace in the console:
java.lang.NoClassDefFoundError: org/junit/platform/engine/TestEngine
at org.junit.platform.launcher.core.ServiceLoaderTestEngineRegistry.loadTestEngines(ServiceLoaderTestEngineRegistry.java:35)
at org.junit.platform.launcher.core.LauncherFactory.create(LauncherFactory.java:87)
at org.junit.platform.launcher.core.LauncherFactory.create(LauncherFactory.java:67)
at org.eclipse.jdt.internal.junit5.runner.JUnit5TestLoader.<init>(JUnit5TestLoader.java:34)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at java.lang.Class.newInstance(Class.java:456)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.createRawTestLoader(RemoteTestRunner.java:370)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.createLoader(RemoteTestRunner.java:365)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.defaultInit(RemoteTestRunner.java:309)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.init(RemoteTestRunner.java:224)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:208)
Caused by: java.lang.ClassNotFoundException: org.junit.platform.engine.TestEngine
at java.net.URLClassLoader.findClass(URLClassLoader.java:444)
at java.lang.ClassLoader.loadClass(ClassLoader.java:486)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:378)
at java.lang.ClassLoader.loadClass(ClassLoader.java:419)
... 14 more
This pointed me to the root cause of the problem (see answer below)...
The problem was that I was missing the dependency to the engine library:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</dependency>
Then, the tests run like a charm in Eclipse.
Migration blogs like https://dev.to/martinbelev/how-to-enable-junit-5-in-new-spring-boot-project-29a8 do mention this dependency, but I primarily worked with the JUnit 5 documentation, and somehow I must have overlooked this important piece of information there...
In my pom.xml, I added this dependency:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<scope>provided</scope>
</dependency>
<!-- Annotation processor that raising compilation errors whenever constraint
annotations are incorrectly used. -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator-annotation-processor</artifactId>
<scope>provided</scope>
</dependency>
When I compile and install the project by executing "mvn clean install" at terminal, the model classes are generated in this directory:
target/generated-sources/annotations/com/myproject/ne/model/
Then if I import this Maven project from Eclipse, it works all fine without complaining the model classes automatically generated.
However, if I don't execute "mvn clean install" at a terminal to generate the model classes and directly import the clean project, Eclipse doesn't generate the model classes, and therefore generates compilation errors.
What's needed to use the hibernate-jpamodelgen to automatically when a clean maven project is imported and compiled?
You can achieve it by enabling it with annotation processing.
In eclipse right click on project --> properties --> Java Compiler --> Annotation Processing --> Factory Path enable it.
Now populate the .factorypath file at the root of your project with the following contents:
<factorypath>
<factorypathentry kind="PLUGIN" id="org.eclipse.jst.ws.annotations.core" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/hibernate/hibernate-jpamodelgen/1.0.0.Final/hibernate-jpamodelgen-1.0.0.Final.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/hibernate/javax/persistence/hibernate-jpa-2.0-api/1.0.0.Final/hibernate-jpa-2.0-api-1.0.0.Final.jar" enabled="true" runInBatchMode="false"/>
</factorypath>
Refresh the project in Eclipse
Follow #Tanvi B answer, and 1 more step:
project --> properties --> Java Compiler --> Annotation Processing:
Enable project specific settings (check all). And at Generated source directory, you have to define target/generated-sources
Maven compilation failes even after adding the external jars to eclipse. My Eclipse codes are okay with external jars, however when I compile Maven complains package blah blah not found, and I have almost 50 external packages.
I will use the mvn dependency to add the jars later.
However It should work, but not luck.
Any troubleshooting/suggestion please.
I think you can not get away with Maven Project without having <dependencies> tag in your pom.xml (Whether you run it from eclipse or from command-line) . That too for the project which is dependent on classes that are coming from 50 external jars.
If you don't want maven to look for these jars, you have to add below entry in pom.xml with system scope:
<dependencies>
<dependency>
<groupId>selenium-server-standalone</groupId>
<artifactId>selenium-server-standalone</artifactId>
<version>2.46.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/selenium-server-standalone-2.46.0.jar</systemPath>
</dependency>
</dependencies>
With this, groupID & artifactID are meaningless. You can write such 50 <dependency> tags for referring your 50 external jars.
Trying to get Roboguice working in Eclipse which contains 2 Maven projects, Astroboy and Roboguice (let's call them A and B where A depends on B). My problem is that unit tests in A which work fine in Maven give compile errors in Eclipse because it can't find Junit. Junit is declared in A's pom as a provided dependency, as below. I did tell Eclipse that project A depends on B by adding B to A's build path/projects.
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>provided</scope>
</dependency>
I got it to work temporarily by changing the scope to test (then doing a mvn install and maven/update project on A), but what must I do to have it find Junit by itself?