TestNG execute compiled tests in jar file - maven

I have a testNG test class which runs fine, but when i package it into a jar file with test files included, i am unable to run the tests from it. The jar file contains the test.class file in it. When i invoke the test from maven it returns "no tests to run".
Any suggestion on how to run a test class within a jar using testNG?

Can you open the jar file with winrar and see if everything is packed there?
Also, is testng your application entry point? What happens when you write:
java -jar yourjar.jar
in console? You should see testng output.
Also, are you sure your parameter xml file is pointing to the right class and that it works?
Are your classes in the jar file? the classes with your tests? Please check that with winrar to see the jar content.
I think that with this checklist your tests should run.

Related

Maven: Run Test On the JAR

I've coded my own ApplicationListener for Spring-Boot that looks for files on the classpath. When running the test in Maven it looks for files in the filesystem as the JAR doesn't exist yet. That's fine and is a good test as it covers running the final application from within an IDE.
However, I also want to test my class when it's run inside a JAR (not necessarily as a repackaged Spring-Boot JAR) to see if it grabs the files inside the JAR. The tests-JAR contains all those files I want to read so theoretically it's all ready.
So how do I set this up with Maven? There's just one test I want to run after the JAR was built, all other tests are run as tests and integration-tests in the common lifecycle-phase (well, I have to filter out that one test for those phases somehow, too).

Invoking maven from inside an uber jar using pom.xml located inside jar

I am running a set of tests using junit and I have created a maven project for it. I use the surefire plugin to run the tests. I wanted to package this project as a self executable jar for the sake of convenience.So I placed all of test classes in the src/main/java directory as recommended for using the maven shade plugin for generating an uber jar. I created a class with a main method and use maven invoker to execute my tests.
The tests get executed perfectly when I use my ide to run the main method. However, after I package it as a jar , the invoker is unable to locate the pom.xml. If I place a copy of the pom.xml in the same directory as the jar, maven is invoked but it is unable to execute the tests as surefire is unable to locate the tests.
I would love to know if there is a solution for this or if there is a better approach towards trying to achieve my goal.
#khmarbaise and everyone else trying to help me , let me describe my scenario a little more in detail.
Lets take any api , for example , lastfm api, I would be writing classes for each api end point and every method in each class would be a test. I use junit to execute the tests. I use a junit wrapper called serenity bdd that helps me structure my tests and generate aggregate reports. The maven goal serenity:aggregate would generate an aggregated report of all tests. I am using the maven invoker to run tests using surefire plugin and then aggregate the tests using serenity. In my eclipse project I simply create a maven run configuration and provide the maven goals. Or else I would use the terminal to run maven from my project base directory.
Apart from my class file, I have several resources, like csv files which are inputs for parametric tests a log folder where all log files that are generated get saved and other sample files for testing file upload apis. I felt that it would be simple to package all of this as a jar and let my developers add this jar as a dependency and run a simple script which runs the jar each time they create and deploy a build. Getting the pom file for invoking maven was trivial in case of eclipse or the command line but not so straight forward when its inside of a package.

Cucumber tests configured in main method are not executing from jar file

I have a BDD maven project for API testing. I want to execute the cucumber tests independently by/from anywhere. To do this I required two things:
1. A jar (I have built it via assembly plugin and maven-jar-plugin also )
2. A class having main() method OR Runner classes. (I have defined both in my framework)
Runner Class:
3. While running the command in cmd it is giving the error
java -jar dealflow-bdd-1.0.4-SNAPSHOT.jar
Note: By following the approach discussed in this question
Running Cucumber tests directly from executable jar
I am able to create the jar by maven-assembly-plugin but the error seems to change. Now by running
java -jar dealflow-bdd-1.0.4-SNAPSHOT.jar
it is giving this
Comment: Tests are running if I run the main() method directly from the IDE itself or Run the Runner class

Run testng.xml from command prompt. I have jar that is created from Maven Project. Surefireplugin is not solving my purpose

What i have
Jar created from maven project
TestNG.xml file
What i want
Want to run testng.xml from command prompt
What i tried
Used surefire plugin in pom then executed mvn test command.
What is problem then ?
Surefire plugin will need. src folder and every time it will build project.
Any solution available ? Using jar file and testng . I dont want to use src or bin
You could use a multi-module maven project.
For example with two modules, A and B. Module A will build the jar file, and in module B, you can specify A as a dependency, and run your tests from there.

Bin folder is not visible in the explorer for a Maven project

I have crated a Maven project for my selenium test. I have added some testNG classes under src/test/java. The package name under which testNG classes are is named as "tests".
Now I have created a Project in Jenkins to build that project and I am using a .bat file to do that.
The .bat file looks like this:
set classpath=D:\Learning\Eclipse\Workspace\CabiGroup\target\test-classes;D:\Learning\Eclipse\Workspace\CabiGroup\lib\*
java -cp bin;D:\Learning\Eclipse\Workspace\CabiGroup\lib\* org.testng.TestNG XML\CabiDirect.xml XML\CabiHome.xml
In the classpath; I understand that I need to provide the path of bin folder of the Maven project; but I do not see any bin folder under the Maven Project in my Workspace.
That is why the build is failing with error:
Cannot find class in classpath: tests.VerifyAdvanceSearch
(VerifyAdvanceSearch.class is a testNG class under tests package).
How do I see the bin folder under workspace and provide the path in the .bat file properly?
Please help.
Edit: I have followed what can I do to make display the bin folder on eclipse? but it did not help.
The compiled code for the maven testng code will be available under target\classes. In case of a normal Java project,
testng will create its compiled code under bin folder, hence we have this confusion.
Request you to kindly use the below commands
Navigate to the path of the project
cd F:\Automation\Workspace\testngproject
Set the classpath
set classpath=F:\Automation\Workspace\testngproject\target\classes;F:\Automation\Workspace\testngproject\lib\*
Run the testng.xml using the below command
java org.testng.TestNG testng.xml
Try the following command for latest testNG :
java -cp C:\eclipse-workspace\EclipseProject\src\main\java\tests; C:\eclipse-workspace\EclipseProject\jars* org.testng.TestNG testng.xml
Also for the latest testNG you need to have atleast the following jars in the jars folder specified :
testng.jar
jcommander.jar

Resources