How to pass input from command line to junit maven test program - maven

I wrote a junit test to add two numbers. I need to pass this numbers from command line. I am running this junit test from maven tool as
mvn -Dtest=AddNumbers
My test program looks like this
int num1 = 1;
int num2 = 2;
#Test
public void addNos() {
System.out.println((num1 + num2));
}
How to pass these numbers from command line?

Passing the numbers as system properties like suggested by #artbristol is a good idea, but I found that it is not always guaranteed that these properties will be propagated to the test.
To be sure to pass the system properties to the test use the maven surefire plugin argLine parameter, like
mvn -Dtest=AddNumbers -DargLine="-Dnum1=1 -Dnum2=2"

To pass input from command line to junit maven test program you follow these steps. For example if you need to pass parameter fileName into unit test executed by Maven, then follow the steps:
In the JUnit code - parameter will be passed via System properties:
#BeforeClass
public static void setUpBeforeClass() throws Exception {
String fileName = System.getProperty("fileName");
log.info("Reading config file : " + fileName);
}
In pom.xml - specify param name in surefire plugin configuration, and use {fileName} notation to force maven to get actual value from System properties
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<!-- since 2.5 -->
<systemPropertyVariables>
<fileName>${fileName}</fileName>
</systemPropertyVariables>
<!-- deprecated -->
<systemProperties>
<property>
<name>fileName</name>
<value>${fileName}</value>
</property>
</systemProperties>
</configuration>
</plugin>
In the command line pass fileName parameter to JVM system properties:
mvn clean test -DfileName=my_file_name.txt

You can pass them on the command line like this
mvn -Dtest=AddNumbers -Dnum1=100
then access them in your test with
int num1=Integer.valueOf(System.getProperty("num1"));

Related

Maven: how to invoke a plugin with execution specific config

I have written a plugin and want to treat the goal vrs (check versions) differently,
depending on execution/command line.
In my code I added
#Parameter(name = "versionsWarnOnly", defaultValue = "true")
private boolean versionsWarnOnly;
public boolean getVersionsWarnOnly() {
System.out.println("invoked get");
return this.versionsWarnOnly;
}
public void setVersionsWarnOnly(boolean versionsWarnOnly) {
System.out.println("invoked set");
this.versionsWarnOnly = versionsWarnOnly;
}
I would expect, that without specifying versionsWarnOnly in the configuration in the pom, i just get the defaultValue specified.
The problem is, that does not happen, it is always false in that case.
If i configure in the configuration
of the plugin in the pom
<versionsWarnOnly>true</versionsWarnOnly>
Then this is done (well some success) if I build the phase mvn validate.
It is even true if I invoke the goal from the command line by goal mvn latex:vrs.
But if i specify that in an execution that like,
<execution>
<id>validate_converters</id>
<phase>validate</phase>
<configuration>
<versionsWarnOnly>true</versionsWarnOnly>
</configuration>
<goals>
<goal>vrs</goal>
</goals>
</execution>
again it has no effect.
I have no idea what i do wrong
or what kind of information you need to help me.
The problem is resolved and the question could be deleted altogether.
The problem was that I put almost all of the config into a class Settings.
On the other hand, I added the new parameter outside and so it had no effect.
Thats all.
Now all works fine.

Maven Enforcer: How to access maven properties from beanshell rule

I successfully created a evaluateBeanshell rule with the maven-enforcer-plugin that scans files in the workspace for common mistakes.
With a hardcoded path the rule works fine. When I want to use the ${project.basedir} variable from the surrounding pom, the script breaks on my Windows machine.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>${maven-enforcer-plugin.version}</version>
<executions>
<execution>
<id>enforce-banned-dependencies</id>
<inherited>true</inherited>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<evaluateBeanshell>
<condition>
import scanner.MyScanner;
scanner = new MyScanner();
//hack to read root dir
//project.basedir crashes beanshell with its backslashes
rootPath = new File("");
root = new File(rootPath.getAbsolutePath());
print("Scanning in: " + root);
print("${project.artifactId}"); // works fine
print("${project.basedir}"); // breaks the code
scanner.loopThroughProjects(root);
return everythingIsFine;
</condition>
</evaluateBeanshell>
</rules>
<fail>true</fail>
</configuration>
</execution>
</executions>
In the debug output the line:
print("${project.basedir}");
was replaced by:
print("D:\code\my-maven-project");
Is there another maven property with sanitized slashes or is there another way to access ${project.basedir}?
The hack outlined in the code example kind of works, but I don't like hacks that force me to leave comments.
You could try ${project.baseUri}.
See https://maven.apache.org/ref/3.8.5/maven-model-builder/#Model_Interpolation
On my Windows 10 machine with Java 8 and Maven 3 the following test properties in pom.xml:
<test>${project.baseUri}</test>
<test2>${project.basedir}</test2>
become the following in the 'effective-pom' (via Intellij IDEA maven plugin)
<test>file:/D:/test/path/</test>
<test2>D:\test\path</test2>
This is just as a proof of concept to show the path separators change, and become valid as a Java String.
You could then transform the URI to a file for your needs in the beanshell script as follows:
uri = java.net.URI.create("${project.baseUri}");
root = new java.io.File(uri);
Via https://docs.oracle.com/javase/8/docs/api/java/io/File.html#File-java.net.URI-

How to set an environment variable in application.yml - Spring Boot

I'm working on a spring boot application and I'm writing some tests which use MockServer.
In order to not have all the requests to the mock servers displayed in the logs, I have to set the environment variable mockserver.logLevel to OFF.
When I do it via command line, it works perfectly :
mvn clean install -Dmockserver.logLevel="OFF"
but when I try to do it in my application.yml, it doesn't work.
I've tried the following :
mockserver:
log-level: OFF
mockserver:
loglevel: OFF
mockserver:
logLevel: OFF
logging:
level:
org.mockserver: OFF
But none of these work.
I guess I don't write it correctly in the application.yml but I can't figure out the right way.
I finally found a way by adding it directly into the pom.xml as a system property variable in the concerned plugin :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${version.maven-failsafe-plugin}</version>
<configuration>
<systemPropertyVariables>
<mockserver.logLevel>OFF</mockserver.logLevel>
</systemPropertyVariables>
</configuration>
</plugin>
an another 'wiring' solution:
#Configuration
public class MockServerConfiguration {
...
public MockServerConfiguration(#Value("${mockserver.logLevel}") String mockserverLogLevel) {
if (Objects.isNull(System.getProperty("mockserver.logLevel"))) {
System.setProperty("mockserver.logLevel", mockserverLogLevel);
}
}
...
}

User defined system variable throws null pointer exception when executed as testng suite

There has been a situation when I need to run with the user defined <systemPropertyVariables> in one of my utility class.
<systemPropertyVariables>
<environment>uat</environment>
</systemPropertyVariables>
I have defined this in my pom.xml file.
inside my utility class I am extracting this value using below
System.getProperty("environment");
When I run the test case through pom.xml it runs fine. But when I try to run it through testing.xml file or as a testng test, it gives me nullpointerException.
I have defined vmarguments in run configuration as a temp solution but when this code gets distributed again this won't work as run configuration is limited to the local machines.
Can anyone please help me on how to resolve this error. Where should I define the SystemPropertyVariable so that it also works when I run the testng suite individually.
One thing you can do is pass this values from testNG using parameter tag and you can retrieve these values in Test class. By this you can run this both from maven and testng
Hi i have found a solution for this if any one else is stuck with this type of problem while designing framework.
Write a simple java code
try{
your code
}catch(NullPointerException ee){
environment="";
e.printStackTrace();
}catch(Exception e){
Assert.assertTrue(fail);
}
it only handles your code when you run via testng.xml (which is not part of your major test runs) and fails when we get any other exceptions.

Maven TestNG surefire plugin run groups via terminal

Im currently working on trying to get the maven command line to run a certain group. I have annotated my tests as follows:
#Test(priority = 2, groups = "MyGroup")
public void myFirstTest(){};
then I I kick off the test run via command line like this:
mvn clean install test -Dgroups=MyGroup
Aaaaannnndddd nothing runs. Maven starts up, then quits saying there are no tests. Like so:
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 38.071 sec - in TestSuite
Now I tried a few things. 1st. I tried putting a testng.xml file and adding the group name "MyGroup" to the file. I then reference this file inside my pom.xml as such:
<suiteXmlFiles>
<!-- command to run "mvn clean test -DsuiteFile=src/test/resources/testng.xml" -->
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
</suiteXmlFiles>
When i the re-run using the command line it works. Now i know we dont need to add an xml file specifying the group names. As this is made dynamically by maven, so then what am I missing.
Also, i put a breakpoint in the method overridden transformer
#Override
public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
And annotation.getGroups() is always an empty array. This means that maven is not passing in the test groups. Why!!!. I really dont see it and been hacking away at this for hours, Any help would be greatly appreciated.

Resources