How do I run an individual test in Maven? - maven

We're using Maven 3.0.3 and are using JUnit 4.8.1 ...
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
We have this test file ...
./src/test/java/com/myco/clearing/common/xml/TextNodeTest.java
How can I run this individual test? When I try
mvn -Dtest=TextNodeTest test
I get an error saying no tests were run. I get the same error if I specify the entire package name to my test. ...
mvn clean -Dtest=com.myco.clearing.common.xml.TextNodeTest test
which produces the error message ...
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project myco-productplus-web: 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
Here is the surefire configuration I'm using
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
<configuration>
<skip>false</skip>
<additionalClasspathElements>
<additionalClasspathElement>${project.build.sourceDirectory}</additionalClasspathElement>
<additionalClasspathElement>${project.build.testSourceDirectory}</additionalClasspathElement>
</additionalClasspathElements>
<useManifestOnlyJar>false</useManifestOnlyJar>
<forkMode>always</forkMode>
<systemProperties>
<property>
<name>gwt.args</name>
<value>-out "${webappDirectory}"</value>
</property>
</systemProperties>
<systemPropertyVariables>
<tomcat.port>${tomcat.servlet.port}</tomcat.port>
<project.artifactId>${project.artifactId}</project.artifactId>
</systemPropertyVariables>
</configuration>
</plugin>

The way to run a single test (method) in surefire is to do:
mvn test -Dtest=uk.co.farwell.AppTest#testSlow
Note the # instead of the space between the class name and the method name.
However, as #Andrew says, there is a bug in 2.12 (SUREFIRE-827: Surefire 2.12 cannot run a single test, regression from 2.11), but it works in 2.11.
The above bug is still open (as of 24.02.2012), but actually works for me using 2.13-SNAPSHOT.
EDIT: This is now marked as fixed in 2.12.1.

Looks like it's a bug in 2.12 version - SUREFIRE-827. Try downgrading to 2.11.

Like the others say, it's a bug in Surefire and it's fixed since version 2.11.1.

Related

AssertJ custom Aspects doesn't work with maven-surefire-plugin

I have the maven project written on Kotlin.
pom.xml uses maven-surefire-plugin plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<suiteXmlFiles>
<suiteName>${suiteName}</suiteName>
</suiteXmlFiles>
<testFailureIgnore>false</testFailureIgnore>
<argLine>
-javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
</argLine>
<systemPropertyVariables>
<env>${env}</env>
<deviceName>${deviceName}</deviceName>
</systemPropertyVariables>
</configuration>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>
I'd like to add additional aspect to my code, so I added another plugin:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.14.0</version>
<configuration>
<complianceLevel>1.8</complianceLevel>
<source>1.8</source>
<target>1.8</target>
<showWeaveInfo>true</showWeaveInfo>
<verbose>true</verbose>
<Xlint>ignore</Xlint>
<encoding>${project.build.sourceEncoding}</encoding>
<forceAjcCompile>true</forceAjcCompile>
<weaveDirectories>
<weaveDirectory>${project.build.outputDirectory}</weaveDirectory>
<weaveDirectory>${project.build.testOutputDirectory}</weaveDirectory>
</weaveDirectories>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
with aspectjrt dependency.
In this state, my tests are working.
Next, I created a new class with #Aspect annotation - still working.
Now I am adding very simple code (just to check) in this class:
#Pointcut("execution(* *(..))")
fun method() {
}
#AfterReturning(pointcut = "method()")
fun afterMethod() {
println("After method")
}
This causes suite to fail with:
[ERROR] There was an error in the forked process
[ERROR] 'utils.Aspects utils.Aspects.aspectOf()'
[ERROR] org.apache.maven.surefire.booter.SurefireBooterForkException: There was an error in the forked process
[ERROR] 'utils.Aspects utils.Aspects.aspectOf()'
[ERROR] at org.apache.maven.plugin.surefire.booterclient.ForkStarter.fork(ForkStarter.java:656)
[ERROR] at org.apache.maven.plugin.surefire.booterclient.ForkStarter.run(ForkStarter.java:282)
[ERROR] at org.apache.maven.plugin.surefire.booterclient.ForkStarter.run(ForkStarter.java:245)
[ERROR] at org.apache.maven.plugin.surefire.AbstractSurefireMojo.executeProvider(AbstractSurefireMojo.java:1183)
[ERROR] at org.apache.maven.plugin.surefire.AbstractSurefireMojo.executeAfterPreconditionsChecked(AbstractSurefireMojo.java:1011)
[ERROR] at org.apache.maven.plugin.surefire.AbstractSurefireMojo.execute(AbstractSurefireMojo.java:857)
[ERROR] at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:137)
[ERROR] at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute(MojoExecutor.java:301)
[ERROR] at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:211)
[ERROR] at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:165)
[ERROR] at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:157)
[ERROR] at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:121)
[ERROR] at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:81)
[ERROR] at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:56)
[ERROR] at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:127)
[ERROR] at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:294)
[ERROR] at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:192)
[ERROR] at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:105)
[ERROR] at org.apache.maven.cli.MavenCli.execute(MavenCli.java:960)
[ERROR] at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:293)
[ERROR] at org.apache.maven.cli.MavenCli.main(MavenCli.java:196)
[ERROR] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[ERROR] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
[ERROR] at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[ERROR] at java.base/java.lang.reflect.Method.invoke(Method.java:564)
[ERROR] at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:282)
[ERROR] at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:225)
[ERROR] at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:406)
[ERROR] at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:347)
I have created stand-alone project to check if understand AOP concept correctly here: https://github.com/heavy-razzer/AssertJ-Maven-Kotlin
It work.
So looks like there is something wrong with the configuration of my main project, but what?
I just cloned your sample project, which was verfy helpful. For me, the project works just fine in Maven:
[INFO] --- aspectj-maven-plugin:1.14.0:test-compile (default) # aop-project-kotlin ---
(...)
[INFO] Join point 'method-execution(void tests.NewTest.myTest())' in Type 'tests.NewTest' (NewTest.kt:12) advised by before advice from 'Aspects' (test-classes!Aspects.class(from Aspects.kt))
[INFO] Join point 'method-execution(void steps.Steps.printCaption())' in Type 'steps.Steps' (Steps.kt:7) advised by afterReturning advice from 'Aspects' (test-classes!Aspects.class(from Aspects.kt))
[INFO] Join point 'method-execution(void steps.Steps.printMessage())' in Type 'steps.Steps' (Steps.kt:11) advised by afterReturning advice from 'Aspects' (test-classes!Aspects.class(from Aspects.kt))
[INFO]
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) # aop-project-kotlin ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running tests.NewTest
Test started: Very simple test
This this a simple test
Test step performed: printCaption
Testing in progress
Test step performed: printMessage
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.592 s - in tests.NewTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
In IntelliJ IDEA, it does not work out of the box, because doing post-compile weaving with Maven in a single module together with Kotlin compilation does not work out of the box. The test runs, but the aspect is not triggered. You need to delegate the build and run actions to Maven to be able to run the test directly from IDEA:
Test started: Very simple test
This this a simple test
Test step performed: printCaption
Testing in progress
Test step performed: printMessage
===============================================
Default Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
Under no circumstances, however, can I reproduce your Maven problem.
The problem in my main project (not that sample project I shared here) lies in qase-testng library that I use to upload test run results to the Qase Test Management system.
me and Qase use AssertJ and have aspects logic implemented in some classes
Qase requires some additional configuration to SureFire maven plugin to upload all test data correctly.
SureFire uses AssertJ too
And when I run my project, SureFire got exceptions when trying to combine Aspects from my code and from Qase library.
Workaround is:
Remove argLine from SureFire config (from Qase testing docs)
Remove goal "compile" from aspectj-maven-plugin config - it is not needed for a project with tests.
The only drawback of these actions is that step names in the Qase dashboard will not have green/red icons next to them in the test run page. I have contacted Qase support, and they are thinking about this situation.

Unable to resolve groovy.text.XmlTemplateEngine

Trying to use groovy.text.XmlTemplateEngine in the Groovy based unit tests of my Maven based project with gmaven plugin configured as following
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.5-jenkins-3</version>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.4.12</version>
</dependency>
</dependencies>
</plugin>
My command line build mvn clean install fails not being able to compile my simple Groovy unit test file with an error message
[ERROR]
file:/Users/../my-service/src/test/groovy/myapp/MyApplicationTest.groovy:
84: unable to resolve class groovy.text.XmlTemplateEngine [ERROR] #
line 84, column 30. [ERROR] XmlTemplateEngine engine = new
groovy.text.XmlTemplateEngine() [ERROR] ^
And in the file I'm only doing a simple command
import groovy.text.XmlTemplateEngine
...
XmlTemplateEngine engine = new groovy.text.XmlTemplateEngine()
Both lines fail with the same error message.
The version of Groovy installed in my OSX command line is 2.4.12.
I have analyzed the output of the
mvn -e -X test
command and didn't find the groovy-all.jar on the classpath during the test compilation phase. Only groovy.jar was present and it doesn't contain groovy.text.XmlTemplateEngine class.
So, I added an additional test dependency:
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.4.12</version>
<scope>test</scope>
</dependency>
And it compiles my tests well now.

Maven spring boot pre-integration-test timeout error

When I run the command install of my server's Lifecycle I get the following error:
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 34.507 s
[INFO] Finished at: 2017-03-10T15:32:41+01:00
[INFO] Final Memory: 69M/596M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.4.0.RELEASE:start (pre-integration-test) on project challenger-server-boot: Spring application did not start before the configured timeout (30000ms -> [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/MojoExecutionException
It obviously comes from the timeout settings but I cannot find where I have to change this value...
Not sure if it can help but here's some of my pom.xml related to the unit and integration testings:
<!-- Unit testing -->
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<!-- Integration testing -->
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<skipTests>false</skipTests>
</configuration>
</plugin>
Here's the debug log: http://pastebin.com/kUkufHFS
You are trying to start a spring boot app before in pre-integration-test phase.
The spring-boot-maven-plugin StartMojo class (org.springframework.boot.maven) is complaining because the app is not started within the default timeout, wich is defined by the attributes "wait" (defautl value: 500 ms) and "maxAttempts" (default: 60) -> 500 * 60.
/**
* The number of milli-seconds to wait between each attempt to check if the spring
* application is ready.
*/
#Parameter
private long wait = 500;
/**
* The maximum number of attempts to check if the spring application is ready.
* Combined with the "wait" argument, this gives a global timeout value (30 sec by
* default)
*/
#Parameter
private int maxAttempts = 60;
"wait" and "maxAttempts" are annotated #Parameter, which means you can change their value from within your pom file, in the plugin configuration like this:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<wait>1000</wait>
<maxAttempts>180</maxAttempts>
</configuration>
<executions>
...
</executions>
</plugin>

Cannot execute mvn release:prepare

I have a simple project with just one class file and one junit test file. Trying and testing a release using mvn release:prepare but fetting following exception while executing the following command. I have SVN server installed but on a different Windows machine. I do have the SVN client (TortoiseSVN) installed on the current machine.
Can anybody please guide as to what am I missing ?
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-release-plugin:2.0:prepare (default-cli) on project maven-svn-release: Unable to check for local modifications
[ERROR] Provider message:
[ERROR] The svn command failed.
[ERROR] Command output:
[ERROR] 'svn' is not recognized as an internal or external command,
[ERROR] operable program or batch file.
[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.
pom.xml configuration:
<scm>
<connection>scm:svn:https://gsi-541243.gsiccorp.net/svn/maven-rnd/maven-svn-release/trunk</connection>
<developerConnection>scm:svn:https://gsi-541243.gsiccorp.net/svn/maven-rnd/maven-svn-release/trunk</developerConnection>
<url>https://gsi-541243.gsiccorp.net/svn/maven-rnd/maven-svn-release/trunk</url>
</scm>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<configuration>
<tagBase>https://gsi-541243.gsiccorp.net/svn/maven-rnd/maven-svn-release/tags</tagBase>
</configuration>
</plugin>
</plugins>
</build>

Error initializing enunciate - generate step error

I have configured my REST/Spring/Jersey project to generate documentation for APIs using enunciate.
The relevant changes in my pom.xml are
<plugin>
<groupId>org.codehaus.enunciate</groupId>
<artifactId>maven-enunciate-spring-plugin</artifactId>
<version>1.28</version>
<configuration>
<generateDir>${project.build.directory}/enunciate-generate</generateDir>
<configFile>enunciate.xml</configFile>
<exports>
<jaxws.client.library.binaries>client.jar</jaxws.client.library.binaries>
</exports>
</configuration>
<executions>
<execution>
<goals>
<goal>assemble</goal>
</goals>
</execution>
</executions>
</plugin>
.....
<dependency>
<groupId>org.codehaus.enunciate</groupId>
<artifactId>maven-enunciate-spring-plugin</artifactId>
<version>1.28</version>
</dependency>
My enunciate.xml is as follows
<?xml version="1.0"?>
<enunciate xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://enunciate.codehaus.org/schemas/enunciate-1.28.xsd">
<api-classes>
<include pattern="com.xxx.rest.*"/>
</api-classes>
<modules>
<docs docsDir="api" title="REST WebServices API"/>
<spring-app>
<war mergeWebXML="war/WEB-INF/web.xml"/>
<springImport file="war/WEB-INF/applicationContext-jdbc.xml"/>
</spring-app>
</modules>
</enunciate>
When I run my maven project I get the following error initializing enunciate. As you can see the error is not very helpful. Any hints what could be wrong in my configuration.
--- maven-enunciate-spring-plugin:1.28:assemble (default) # xxx ---
initializing enunciate.
[csharp] C# compilation is disabled, but the source code will still be generated.
invoking enunciate:generate step...
BUILD FAILURE
Total time: 3.324s
Failed to execute goal org.codehaus.enunciate:maven-enunciate-spring-plugin:1.28:assemble (default) on project xxx: Problem assembling the enunciate app. String index out of range: 0 -> [Help 1]
To see the full stack trace of the errors, re-run Maven with the -e switch.
Re-run Maven using the -X switch to enable full debug logging.
For more information about the errors and possible solutions, please read the following articles:
[Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
That StringIndexOutOfBoundsException was reported and fixed here.
The "invocation of API has failed" is almost always caused by some compile failure. Maybe when you messed with your dependencies to get around the first problem, you introduced a compile failure in your code base?

Resources