Testing a single package skips parametrized tests in TestNG - java

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.

Related

My test doesn't output in console I'm using Maven

I'm new to Maven and Im facing this kind of of weird stuff Im using Intellij.
class HelloWorldTest {
public void testHello() {
System.out.println("Test - hello! ");
}
public void testWorld() {
System.out.println("Test - World!");
}
}
When I run package on Default Lifecycle It show's output below Test run: 0. The test isn't running and output in console. I'm Following tutorial Apache Maven Beginner to Guru on udemy.
[INFO] ------------------< guru.springframework:hello-world >------------------
[INFO] Building hello-world 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.1.0:clean (auto-clean) # hello-world ---
[INFO] Deleting /home/jericho/Documents/spring-projects/hello-world/target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) # hello-world ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) # hello-world ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /home/jericho/Documents/spring-projects/hello-world/target/classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) # hello-world ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/jericho/Documents/spring-projects/hello-world/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) # hello-world ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to /home/jericho/Documents/spring-projects/hello-world/target/test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) # hello-world ---
[INFO] Surefire report directory: /home/jericho/Documents/spring-projects/hello-world/target/surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Results :
**Tests run: 0, Failures: 0, Errors: 0, Skipped: 0**
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) # hello-world ---
[INFO] Building jar: /home/jericho/Documents/spring-projects/hello-world/target/hello-world-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.044 s
[INFO] Finished at: 2020-10-10T16:57:23+08:00
[INFO] ------------------------------------------------------------------------
Your test didn’t run:
Results :
**Tests run: 0, Failures: 0, Errors: 0, Skipped: 0**
You need to annotate your test class with #Test. And you should consult the JUnit manual

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] ------------------------------------------------------------------------

Spring Roo 2.0.0M2 sample script home page & tomcat issues

I'm testing the sample script at Spring Roo 2.0.0M2 released. The script generates well, but when issueing mvn tomcat:run it breaks.
In the end there is no local website.
How to solve this to get the project running?
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building foo 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> tomcat-maven-plugin:1.1:run (default-cli) > compile # foo >>>
[INFO]
[INFO] --- apt-maven-plugin:1.1.3:process (default) # foo ---
[INFO]
[INFO] --- aspectj-maven-plugin:1.8:compile (default) # foo ---
[INFO] Showing AJC message detail for messages of types: [error, warning, fail]
[WARNING] incorrect classpath: D:\Tools\maven_repo\org\springframework\roo\org.springframework.roo.annotations\2.0.0.M2\org.springframework.roo.annotations-2.0.0.M2.pom
<unknown source file>:<no line information>
[INFO]
[INFO] --- aspectj-maven-plugin:1.8:test-compile (default) # foo ---
[WARNING] No sources found skipping aspectJ compile
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) # foo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 62 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) # foo ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] <<< tomcat-maven-plugin:1.1:run (default-cli) < compile # foo <<<
[INFO]
[INFO] --- tomcat-maven-plugin:1.1:run (default-cli) # foo ---
[INFO] Skipping non-war project
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.312 s
[INFO] Finished at: 2016-05-27T12:54:41+02:00
[INFO] Final Memory: 43M/602M
[INFO] ------------------------------------------------------------------------
Remember that Spring Roo 2.x generates Spring Boot applications. It includes some starters on your pom.xml file that provides you an embedded tomcat server. Also, your generated project includes the spring-boot-maven-plugin that allows you to run your application executing the following maven command:
mvn clean compile spring-boot:run
For more information about Spring Boot project read the official documentation
Hope this helps,

Apache Tomcat 7 HTTP Connector node not found in intelliJ

I am developing an application using GWT, Maven on Apache Tomcat 7 with intelliJ idea. i have recently debugged and run my application but faced the following problem:Error running Tomcat 7: HTTP Connector node not found: set up one in the server.xml.
Apache Tomcat run code:
"c:\program files (x86)\jetbrains\intellij idea 11.1.4\jre\jre\bin\java" -Dclassworlds.conf=C:\Tools\apache-maven-3.1.1\bin\m2.conf -Dmaven.home=C:\Tools\apache-maven-3.1.1 -Dfile.encoding=UTF-8 -classpath C:\Tools\apache-maven-3.1.1\boot\plexus-classworlds-2.5.1.jar org.codehaus.classworlds.Launcher --offline --no-plugin-registry --fail-fast --strict-checksums --update-snapshots -f D:\Projects\ebank\pom.xml package -P gwtDebug,oracle
[WARNING] Command line option -npr is deprecated and will be removed in future Maven versions.
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building AppFuse GWT Application 1.0
[INFO] ------------------------------------------------------------------------
[WARNING] The POM for com.sun.xml.bind:jaxb-impl:jar:2.1.13 is missing, no dependency information available
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) # AppfuseGWT ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 33 resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) # AppfuseGWT ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- native2ascii-maven-plugin:1.0-beta-1:native2ascii (native2ascii-utf8) # AppfuseGWT ---
[INFO] Includes: [ApplicationResources_zh*.properties, ApplicationResources_ko*.properties, displaytag_zh*.properties]
[INFO] Excludes: []
[INFO]
[INFO] --- exec-maven-plugin:1.2:exec (default) # AppfuseGWT ---
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) # AppfuseGWT ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 17 resources
[INFO] Copying 9 resources
[INFO]
[INFO] >>> hibernate3-maven-plugin:2.2:hbm2ddl (default) # AppfuseGWT >>>
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) # AppfuseGWT ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 33 resources
[INFO]
[INFO] <<< hibernate3-maven-plugin:2.2:hbm2ddl (default) # AppfuseGWT <<<
[INFO]
[INFO] --- hibernate3-maven-plugin:2.2:hbm2ddl (default) # AppfuseGWT ---
[INFO] skipping hibernate3 execution
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) # AppfuseGWT ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- dbunit-maven-plugin:1.0-beta-3:operation (test-compile) # AppfuseGWT ---
[INFO] Skip operation: CLEAN_INSERT execution
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) # AppfuseGWT ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- dbunit-maven-plugin:1.0-beta-3:operation (test) # AppfuseGWT ---
[INFO] Skip operation: CLEAN_INSERT execution
[INFO]
[INFO] --- gwt-maven-plugin:2.5.0:compile (gwtcompile) # AppfuseGWT ---
[INFO] uz.eopc.webapp.MainModuleDebug is up to date. GWT compilation skipped
[INFO]
[INFO] --- maven-war-plugin:2.3:war (default-war) # AppfuseGWT ---
[INFO] Packaging webapp
[INFO] Assembling webapp [AppfuseGWT] in [D:\Projects\ebank\target\AppfuseGWT-1.0]
[INFO] Processing war project
[INFO] Copying webapp webResources [D:\Projects\ebank\src/main/resources/packaged] to [D:\Projects\ebank\target\AppfuseGWT-1.0]
[INFO] Copying webapp resources [D:\Projects\ebank\src\main\webapp]
[INFO] Webapp assembled in [279 msecs]
[INFO] Building war: D:\Projects\ebank\target\AppfuseGWT-1.0.war
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.405s
[INFO] Finished at: Mon Dec 09 10:54:04 UZT 2013
[INFO] Final Memory: 12M/29M
[INFO] ------------------------------------------------------------------------
[INFO] Maven execution finished
What should be done to fix this problem?
I have found the answer to fix this problem. I was using ubuntu 12.04 and in linux systems everything is connected with permissions. After i have opened read and write permission to my tomcat directory, it has worked out. to open read and write permission to a directory in ubuntu just write:
$ sudo chmod 777 -R folder_name/

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