Currently Im working in a Maven project where I need to run tests in different environments.
The idea is be possible to pass the environment as argument as examples below:
mvn test -Pdev
mvn test -Puat
How can I capture this argument -P from command line?
Thanks!
Related
Hello there are tests written on Java + cucumber
The structure is as follows:
https://i.imgur.com/moLVY6L.png
The main question is how to run this tests not from the IDE, say from the console or even wrap it all in jar file
The problems encountered are that you need the main class to create a jar,
It seems as there is a certain java cucumber.api.cli.Main - but how to use it I do not understand. Either way, there's probably a way to run this just from the command line. Please tell us how to do it?
You can download Maven with following terminal script :
$brew install maven
After brew installation, you need to go to the project directory which includes pom.xml file in terminal :
$cd /path/of/your/project
And finally you can run following command to run your tests :
$mvn clean test
You can try the below on command prompt.
java -cp "E:\Workspace\CucumberProj\Jars*;E:\Workspace\CucumberProj\bin" cucumber.api.cli.Main --glue stepDefination E:\Workspace\CucumberProj\Feature\Login_Test.feature
Trying to do some automated test. I have used below commands to execute the junit file (RunnerTest.java) in command line, getting an error as "could not find class"
Command line commands:
C:\Users\username\workspace\MavenCucumberPrototype\src\test\java\com\cucumber\MavenCucumberPrototype>javac -cp "C:/cjars/*" MavenCucumberPrototype/*.java
C:\Users\username\workspace\MavenCucumberPrototype>java -classpath C:/cjars/junit-4.12.jar org.junit.runner.JUnitCore src.test.java.com.cucumber.MavenCucumberPro
totype.RunnerTest
Maven Project structure
MavenCucumberPrototype
-/src/main/java
-com.cucumber.MavenCucumberPrototype
-/src/test/java
-com.cucumber.MavenCucumberPrototype
-postconn.java
-RunnerTest.java
-Steps.java
-/src/test/resource
-myfeature.feature
When I run Maven projects from a command line, I usually do
mvn clean install
or whatever lifecycle phase I want to execute. In your case, it might be sufficient to do
mvn test
The only imprtant thing to notice is that you must execute the command in the same directory as the pom.xml resides.
The error you recieve is nost likely due to a classpath that doesn't contain what you expected.
I've been developing a test suite for a provided "solution.jar" file in Eclipse for an assignment. I have a test suite which I can run in eclipse no problem, but the assignment requires that I run it from the command line, but I've no idea how to do this.
I've tried:
java -cp .;/usr/share/java/junit.jar org.junit.runner.JUnitCore AllTests
but it just said Error: could not load main class org.junit.runner.JUnitCore
Could someone help me out?
you can use maven build and then from command prompt fire this command
mvn test
It will run test case associated with your project.
I have clean integration-test -DclassId=appas a maven goal in a Jenkins Maven Job.
How can I retrieve this in Java using System.getenv();? When I do this I am only seeing null.
For command line options you have to use System.getProperty() instead of System.getenv():
String id = System.getProperty("classId");
Find more details here.
I wrote a little script to compile the test version of a .jar and put it out on my test server.
cd /home/myusername/workspace/td-daily-budget
mvn -P test clean compile package
scp /home/myusername/workspace/td-daily-budget/target/td-daily-budget-1.0.jar myusername#666.666.666.666:/home/myusername/bin/td-daily-budget.jar
When I run it I get a jar file whose config.properties contains things like
db.connect.string=${db.connect.string}
but when I run mvn -P test clean compile package all by itself in the terminal window I get a jar file put together with the test profile, e.g. config.properties contains db.connect.string=[what I expect it to be for the dev profile]. Why does the same command ignore the profile when run inside a script?
Thanks in advance!
[edit/addendum]
Tried changing the script line to
/bin/bash mvn -P env-test clean package
(it's bash, not Windows, so there's no call command, but using bash to call another script seems to make sense to me) but just got
*Error: Could not find or load main class org.codehaus.plexus.classworlds.launcher.Launcher
*
I think calling it as an external process mucks up Maven's understanding of where it's supposed to be executing.
[2nd edit/addendum]
Removed the redundant compile from the command.
It now appears that this is actually an intermittent problem. The filtering almost always fails when mvn -P [pretty much ANY profile] clean package runs from inside the bash script, but if I run it standalone from the command line repeatedly it will work eight or nine times and then fail several times in a row. I cannot find any pattern to this at all.
As soon as I discovered this I thought I knew the culprit: The m2e plugin for Eclipse was "helping" me in the background every time it saw files changing. So I excitedly shutdown Eclipse thinking the problem would vanish instantly, ran mvn -P env-test clean compile package several times in a row--it worked the first few times and then failed. /headdesk
[edit/addendum]
Removed the space between the -P and the profile name. Still no luck.
I think you need to run the mvn in your batch with "call" command, so sth like:
call mvn -P test clean compile package
The reason is, that mvn is a batch file itself and thus needs to be invoked with "call".
Please give it a try!
Bluddy
Are you allowed to have a profile with the same name as a phase? Try renaming your "test" profile to "env-test" and use:
mvn -P env-test clean package
EDIT:
If you believe it has to do with calling it from a script, try adding a bash declaration at the top:
#!/bin/bash
cd /home/myusername/workspace/td-daily-budget
mvn -P env-test clean package
scp /home/myusername/workspace/td-daily-budget/target/td-daily-budget-1.0.jar myusername#666.666.666.666:/home/myusername/bin/td-daily-budget.jar
Credit Bluddymarri for suggesting the batch script equivalent.