Executin Specific JUnit Test Case using Maven Command - java

I have written a new JUnit Test case which I want to compile using maven command. I am trying to execute this command from the location which has pom.xml and this is the command-
mvn -Dtest=src\test\java\absolute\path\of\JUnit\test\hello_Test.java test
and this is the output I got (says there are no tests to run)-
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building my-maven-component
[INFO] task-segment: [test]
[INFO] ------------------------------------------------------------------------
[INFO] [resources:resources {execution: default-resources}]
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 81 resources
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Nothing to compile - all classes are up to date
[INFO] [resources:testResources {execution: default-testResources}]
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 85 resources
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] Nothing to compile - all classes are up to date
[INFO] [surefire:test {execution: default-test}]
[INFO] Surefire report directory: C:\projects\absolute\path\of\maven\component\my-maven-component\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
There are no tests to run.
Results :
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] No tests were executed! (Set -DfailIfNoTests=false to ignore this error.)
[INFO] ------------------------------------------------------------------------
[INFO] For more information, run Maven with the -e switch
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5 seconds
[INFO] Finished at: Thu Feb 04 13:53:08 IST 2016
[INFO] Final Memory: 26M/64M
[INFO] ------------------------------------------------------------------------

You don't need the full path to run a specific test, the value of the test option is actually a pattern.
As from official documentation
Specify this parameter to run individual tests by file name, overriding the includes/excludes parameters. Each pattern you specify here will be used to create an include pattern formatted like **/${test}.java, so you can just type "-Dtest=MyTest" to run a single test called "foo/MyTest.java". The test patterns prefixed with a ! will be excluded.
So you can even simply run:
mvn -Dtest=hello_Test test
Or double check your input in case any typo was made (as per your comment).

Related

Maven tries download multimodule dependency on `validate` phase

From a build pipeline I want to run validate phase for things like the enforcer plugin. However it does not work for a multimodule project as it tries to download dependencies from repository which are inside the project. However, compile phase does not do that, but for me it is not an option as it is too slow.
pom.xml:
<module>lib</module>
<module>app</module>
lib/pom.xml
<version>1.2.3</version>
app/pom.xml
<dependency>
<artifactId>lib</artifactId>
<version>1.2.3</version>
</dependency>
So, if I do mvn compile it works fine.
But if I do mvn validate it fails validating app module as it tries to download lib-1.2.3 from maven repo. For some reason it now could not see that the lib is a neighbour dependency. Why?
I have created a small repo: https://github.com/kan-izh/so63963768
mvn compile
[INFO] --- maven-enforcer-plugin:3.0.0-M3:enforce (enforce-no-snapshots) # app ---
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) # app ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Workspace\repo\so63963768\app\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) # app ---
[INFO] No sources to compile
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] parent ............................................ SUCCESS [1.612s]
[INFO] lib ............................................... SUCCESS [1.224s]
[INFO] app ............................................... SUCCESS [0.056s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
mvn validate
[INFO] --- maven-enforcer-plugin:3.0.0-M3:enforce (enforce-no-snapshots) # app ---
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] parent ............................................ SUCCESS [0.979s]
[INFO] lib ............................................... SUCCESS [0.015s]
[INFO] app ............................................... FAILURE [0.020s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.180s
[INFO] Finished at: Wed Sep 23 11:27:38 BST 2020
[INFO] Final Memory: 7M/34M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-enforcer-plugin:3.0.0-M3:enforce (enforce-no-snapshots) on project app: Execution
enforce-no-snapshots of goal org.apache.maven.plugins:maven-enforcer-plugin:3.0.0-M3:enforce failed: org.apache.maven.shared.dependency.graph.
DependencyGraphBuilderException: Could not resolve following dependencies: [com.so.q63963768:lib:jar:1.2.3 (compile)]: Could not resolve depend
encies for project com.so.q63963768:app:jar:1.2.3: Failure to find com.so.q63963768:lib:jar:1.2.3 in http://xxxxxxxxxxxxx.xx.xxxxxxxxxxxxxxxxx.
com:8081/repository/maven-public/ was cached in the local repository, resolution will not be reattempted until the update interval of nexus has
elapsed or updates are forced -> [Help 1]
This is a good question, and it shows a small flaw in how Maven handles dependencies.
You need to know that for every plugin-goal you can define if dependencies should be resolved, and for which scope. (and there's a small difference if having only the poms is enough, or that you also need the artifacts)
compiler:compile requires the dependencies that are required during compile, compiler:testCompile requires the dependencies that are required during test.
For the enforce goal it is tricky: the goal itself doesn't require to have resolved dependencies, nor do most rules( like requireJavaVersion or requireMavenVersion), but some rules do, like the one you try to enforce.
Ideally rules can define if they need to have resolved dependencies, but right now the API doesn't support that.
So you have a couple of solutions: always run with compile, or have an execution-block bound to the compile-pahse if it requires artifacts.

Testing a single package skips parametrized tests in TestNG

Overall description
I have a simple Maven project with a parameterized TestNG test. The parameter is specified through a suite XML file. When I run tests for the whole project, the suite XML file is read, parameter is used and the parameterized test is executed. However, when I attempt to test a single package only, then the suite XML file is not used apparently and the test is skipped with a complaint such as "Parameter '...' is required by #Test on method ... but has not been marked #Optional or defined". The question is how to run individual tests and test packages with parameters.
For more details see below.
Testing the whole project
This works as expected. Two test methods are executed. One test method is parameterless, other is parameterized and the parameter value is taken from the suite XML file. This is a equivalent of Run\Test project in NetBeans.
C:\Users\Andrew\Documents\NetBeansProjects\TestNGDemo>"c:\Program Files\NetBeans 8.2\java\maven\bin\mvn" test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building TestNGDemo 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) # TestNGDemo ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) # TestNGDemo ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) # TestNGDemo ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\Andrew\Documents\NetBeansProjects\TestNGDemo\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) # TestNGDemo ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.21.0:test (default-test) # TestNGDemo ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running TestSuite
testGetChangedString
getTrue
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.844 s - in TestSuite
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 9.702s
[INFO] Finished at: Wed Jun 06 23:42:20 ACST 2018
[INFO] Final Memory: 9M/245M
[INFO] ------------------------------------------------------------------------
C:\Users\Andrew\Documents\NetBeansProjects\TestNGDemo>
Testing a single package
In this case only one test method runs, the parameterless one. The parameterized test method is skipped. This is a equivalent of right-clicking a package in NetBeans and selecting "Test package".
C:\Users\Andrew\Documents\NetBeansProjects\TestNGDemo>"c:\Program Files\NetBeans 8.2\java\maven\bin\mvn" test -Dtest=com.endersoft.testngdemo.**.*
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building TestNGDemo 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) # TestNGDemo ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) # TestNGDemo ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) # TestNGDemo ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\Andrew\Documents\NetBeansProjects\TestNGDemo\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) # TestNGDemo ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.21.0:test (default-test) # TestNGDemo ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.endersoft.testngdemo.SimpleClassNGTest
getTrue
[WARNING] Tests run: 2, Failures: 0, Errors: 0, Skipped: 1, Time elapsed: 2.422 s - in com.endersoft.testngdemo.SimpleClassNGTest
[INFO]
[INFO] Results:
[INFO]
[WARNING] Tests run: 2, Failures: 0, Errors: 0, Skipped: 1
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 10.268s
[INFO] Finished at: Wed Jun 06 23:47:28 ACST 2018
[INFO] Final Memory: 9M/245M
[INFO] ------------------------------------------------------------------------
Project source
The zipped project source can be downloaded from here (very small)
You need separate XML files for separate package. When running TestNG with package as parameter, TestNG is not automatically aware about the XML file, like it doesn't exist.
So you have multiple options, specify the parameters in properties file, or hardcode them, or create separate XML files for every package that you can then pass as parameter.

Maven won't run tests from parent or child projects

I got a multi-module project. Each project has its own unittest same for the parent project. When I use mvn test no tests are run and the target folder does not contain any test classes. Parent project doesn't even create a target folder
Structure looks like this:
|-module1-> pom.xml
|-module2-> pom.xml
|-module3-> pom.xml
|-src/main
|-src/test/java/MyTest.java
|-pom.xml
See poms below ( I omited standard pom boilerplate)
parent pom.xml
<groupId>com.tests</groupId>
<artifactId>unit-tests</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>module1</module>
<module>module2</module>
<module>module3</module>
</modules>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
<version>RELEASE</version>
</dependency>
<dependencies>
Child pom has:
<parent>
<groupId>com.tests</groupId>
<artifactId>unit-tests</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
Whenever i run mvn clean test no tests are run and in the console i get something like:
[INFO] ------------------------------------------------------------------------
[INFO] Building unit-tests 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] ------------------------------------------------------------------------
Btw, parent project does not have any source code, just tests
You're using packaging pom. With that kind of packaging you're only allowed to run a small number of goals bound to phases (like install and deploy). In order to run the tests in the parent you must be explicit about your intentions, like this:
$mvn clean compiler:testCompile surefire:test
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.tests.unit_tests.TestModules
Test1!
[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.016 s <<< FAILURE! - in com.tests.unit_tests.TestModules
[ERROR] test1(com.tests.unit_tests.TestModules) Time elapsed: 0.003 s <<< FAILURE!
java.lang.AssertionError
at com.tests.unit_tests.TestModules.test1(TestModules.java:11)
[INFO]
[INFO] Results:
[INFO]
[ERROR] Failures:
[ERROR] TestModules.test1:11
[INFO]
[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] unit-tests ......................................... FAILURE [ 1.262 s]
[INFO] module1 ............................................ SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.473 s
[INFO] Finished at: 2017-12-02T11:04:50-02:00
[INFO] Final Memory: 16M/207M
[INFO] ------------------------------------------------------------------------
That way you are telling maven that you want to compile (generate your target) and run your tests in the aggregator (which has the parent pom). Otherwise maven will only run the tests in the modules.
$mvn test
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] unit-tests
[INFO] module1
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building unit-tests 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building module1 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) # module1 ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.2:compile (default-compile) # module1 ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) # module1 ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.2:testCompile (default-testCompile) # module1 ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.17:test (default-test) # module1 ---
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] unit-tests ......................................... SUCCESS [ 0.002 s]
[INFO] module1 ............................................ SUCCESS [ 0.424 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.504 s
[INFO] Finished at: 2017-12-02T11:06:24-02:00
[INFO] Final Memory: 9M/241M
[INFO] ------------------------------------------------------------------------
I know that it is a hard reading, but you should check it out:
http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Packaging
For example, a project that is purely metadata (packaging value is
pom) only binds goals to the install and deploy phases (for a complete
list of goal-to-build-phase bindings of some of the packaging types,
refer to the Lifecycle Reference).
EDIT
As eis pointed out. Even though it's possible to do it, maven made it hard to do it because you SHOULDN't do it by default. Your unit tests should be in the module they are testing. It's not a good practice to scatter them.
The answer is: you can do it, yes. But you shouldn't! There are somethings in life that you can do, but shouldn't...
If we were talking about integration tests that would be a different story.. That's a different discussion.
Cheers!
The maven surefire plugin runs all JUnit Test where the name of the testclass ends with Test - with a capital 'T'. According to your info your test class is named Mytest. Refactor that class to be named MyTest will make it work.
multimodule parent has the packaging pom, and as such, can by definition only contain the pom, not any source code (not even test source code!). This is the reason your tests won't run and you don't get a target folder: there cannot be anything to run or compile on a project of type pom. A multimodule parent is only meant to group the modules together.

How to prevent building if integration test was failed?

I've got the following test:
public class AppITCase
{
#Test
public void failTest() {
fail();
}
}
It just fails. But if I run mvn clean test-compile failsafe:integration-test, I will see this:
Results :
Failed tests:
AppITCase.failTest:16
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0
[WARNING] File encoding has not been set, using platform encoding Cp1251, i.e. build is platform dependent! The file encoding for reports output files should be provided by the POM property ${project.reporting.outputEncoding}.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.853 s
[INFO] Finished at: 2017-01-14T01:32:16+04:00
[INFO] Final Memory: 11M/141M
Important lines:
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0
[INFO] BUILD SUCCESS [INFO]
What? Why maven accepted this build, if it had test failures? How to solve this?
UPD
If I run mvn verify
[INFO] Scanning for projects...
[INFO]
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) # vsjws-samples ---
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) # vsjws-samples ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.333 s
[INFO] Finished at: 2017-01-14T01:41:23+04:00
[INFO] Final Memory: 15M/200M
[INFO] ------------------------------------------------------------------------

Jenkins not running TestNG tests via surefire

Have a local set up using Maven and TestNG which builds just fine. The build is a little odd given that in order for the tests to run, a packaged JAR is needed first. So in effect the local setup runs
#!/bin/bash
mvn clean package -Dmaven.test.skip.exec=true
mvn test
When run on Jenkins, the clean/package step succeeds as does test, except no tests are actually run:
[workspace] $ mvn -f some_sub_directory/pom.xml test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building ...-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) # project ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /var/lib/jenkins/jobs/...
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) # project ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) # project ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) # project ---
[INFO] Not compiling test sources
[INFO]
[INFO] --- maven-surefire-plugin:2.10:test (default-test) # project ---
[INFO] Tests are skipped.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.222s
[INFO] Finished at: Fri Nov 22 18:34:00 UTC 2013
[INFO] Final Memory: 11M/89M
[INFO] ------------------------------------------------------------------------
Finished: SUCCESS
Seems as though the testCompile and test goals in the surefire plugin aren't finding any test classes.
Is there an additional config needed in pom.xml?
The default and recommended path for test classes is {basedir}/src/test/java/.
But you can precise the path in your pom.xml
<build>
<testSourceDirectory>{basedir}/path/to/test/classes</testSourceDirectory>
...
</build>
You need to specify your tests that need to be run. I believe, it looks for tests starting with test*. If you are using a testng xml then you need to set that up. Check this link out for examples on how to specify tests in different ways.

Categories