Issue
I have a project with four packages. With three of them everything is fine. I have generated JUnit tests and they are executed during the build or while I am running mvn test command.
But my fourth package, containing just one class. I have created a JUnit test for it, but when I am running the build, Surefire does not see this test class. All other eight test classes are executed, but this one is ignored.
When I am trying to execute just this one test class explicitly using
mvn -Dtest=com.company.dpt.prj.pkg.MySpecificClassTest test
I am getting these messages
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 16.058 s
[INFO] Finished at: 2020-12-19T23:58:01-08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.2:test (default-test) on project my-project-name: No tests were executed! (Set -DfailIfNoTests=false to ignore this error.) -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
I went through the test code line by line and couldn't find any reasons for such behavior.
I even deleted the test file and regenerated a very basic one. It is still not executed.
Has anyone encountered something similar? Any hint will be greatly appreciated, as I couldn't find any clues.
I am using JUnit 4.11
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
<version>4.11</version>
</dependency>
All tests were originally generated using Netbeans v.12 "Tools->Create/Update Test" and than edited to reflect methods functionality. However, all test but this one are working fine both in Netbeans GUI and CLI "mvn test". This one is ignored. The purpose of method I want to test is to build HttpEntity for the REST API client. Here is the code of the test:
/**
* Test of prepareRequest method, of class BatchServiceClient.
* @throws java.net.URISyntaxException
*/
@Test
public void testPrepareRequest() throws URISyntaxException {
System.out.println("prepareRequest");
ObjectMapper mapper = Json.mapper();
URL configFileUrl = this.getClass().getClassLoader().getResource("config_f.json");
if (configFileUrl == null) {
fail("Missing configuration file");
} else {
File configFile = new File(configFileUrl.toURI());
BatchConfig config = BatchConfigLoader.loadConfig(configFile);
assertNotNull(config);
boolean validated = config.validate();
assertTrue(validated);
BatchServiceClient client = new BatchServiceClient(config, null, null, null, null, null, null);
List<Object> requestData = Arrays.asList("16759408", "", "", "", "The Home Depot", "",
"Baltimore", "MD", "", "840", "", "", "", "", "", "", "", "", "", "", 10, "",
true, true, "merchantName", "merchantName", "merchantName");
HttpEntity<String> request = client.prepareRequest(requestData);
assertNotNull(request);
}
}
My problem is that it is not failing within the test code. I will be happy if it would, because I will be able to debug it than. It is just not executing. I tried to upgrade JUnit to v.4.13.1 and surefire maven plugin to v.3.0.0-M5 but it did not have any affect.
I tried to run mvn with -e option and got the following stack trace, but it still did not give me any clues on why it is happening.
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:215)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
at org.apache.maven.cli.MavenCli.execute (MavenCli.java:957)
at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:289)
at org.apache.maven.cli.MavenCli.main (MavenCli.java:193)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:564)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
Caused by: org.apache.maven.plugin.MojoFailureException: No tests were executed! (Set -DfailIfNoTests=false to ignore this error.)
at org.apache.maven.plugin.surefire.SurefireHelper.reportExecution (SurefireHelper.java:149)
at org.apache.maven.plugin.surefire.SurefirePlugin.handleSummary (SurefirePlugin.java:364)
at org.apache.maven.plugin.surefire.AbstractSurefireMojo.executeAfterPreconditionsChecked (AbstractSurefireMojo.java:1041)
at org.apache.maven.plugin.surefire.AbstractSurefireMojo.execute (AbstractSurefireMojo.java:857)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
at org.apache.maven.cli.MavenCli.execute (MavenCli.java:957)
at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:289)
at org.apache.maven.cli.MavenCli.main (MavenCli.java:193)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:564)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
Solution
I did find the cause of the problem. Somehow, when IDE generated (and regenerated) that test file it placed the wrong import for the @Test annotation. Instead of
import org.junit.jupiter.api.Test;
it placed
import org.junit.Test;
Therefore, jupiter engine, ran by surefire did not recognized @Test annotation. After I corrected import test was executed.
Answered By - Gary Greenberg
Answer Checked By - Mary Flores (JavaFixing Volunteer)