maven surefire plugin does not inject system properties - java

This is how I set the values
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>
<systemProperties>
<property>spring.active.profiles</property>
<value>Test</value>
</systemProperties>
<systemPropertyVariables>
<spring.active.profiles>development</spring.active.profiles>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
The test fails
#Test
public void testGetProfile() throws Exception {
assertEquals("development", System.getProperty("spring.active.profiles"));
}
I see
java.lang.AssertionError:
Expected :development
Actual :null
What am I missing here?

Nothing wrong!
Run it in a console mvn test
OR
Eclipse --> [project] --> Run as --> Maven test
This test maybe fail if you run it via Eclipse --> Run as --> JUnit, sometimes this action ignores the pom.xml variables.

My bad, plugin was in a different module that test

Related

maven surfire testng execution not working

I have problems setting up my project for testng usage.
Background is that I want to execute my test.xml with maven command line.
I use this plugin within the build section of my pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>test.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
However... when I call mvn clean test -DxmlSuiteFile=test.xml
all my unit tests are executed but the test.xml is untouched.
when I change the version of surefire to 2.22.2 everything works fine.
BUT I get the message: testSuiteXmlFiles0 has null value
when running only mvn test
I am confused how to get that to run with the surefire 3.0.0-M5 ?
-DxmlSuiteFile=test.xml is not required if it already defined in pom.xml.
Try to define surefire testNG provider explicitly:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>test.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-testng</artifactId>
<version>3.0.0-M5</version>
</dependency>
</dependencies>
</plugin>
test.xml file should be in the project root.
See also:
https://maven.apache.org/surefire/maven-surefire-plugin/examples/providers.html

how to avoid runing testcases without using skipTests in maven while using the mvn clean install

I have written the test cases but it is activating when I am running the mvn clean install test cases also running but my case no need to run the test cases while running the mvn clean install when I am using the particular profile then only activate the test cases.
You can configure surefire plugin in your pom.xml to include/exclude tests based on their names, and use different configuration for the profiles you want to run these tests on.
Something like:
<profiles>
<profile>
<id>without-tests</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<excludes>
<exclude>**/someTests.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>without-other-tests</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<excludes>
<exclude>**/otherTests.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
See more about it here
Name the tests you dont want to run with a suffix that enables you to group them ...
e.g. sometimes we use ...FunctionalTest to label functional tests. These tests can then be grouped/included/excluded when running unit tests, as a part of the standard build.
See this post for more info How to permanently exclude one test class in a Maven build

Executing single TestNG test case with using Maven/Jenkins

Can anyone tell me how to execute single testcase with maven or jenkins.
mvn -Dtest=smoke tests seem to be not working for me.
I have tried other options like mvn clean install -Dtest=smoketest test that time i got below error if is set to true, and no tests execute if set to false`
Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.19.1:test (default-test) on project ABC: No tests were executed! (Set -DfailIfNoTests=false to ignore this error.)
POM USED
<project>
<groupId>com.testng.smoke</groupId>
<artifactId>ABC</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<build> <!-- Source directory configuration -->
<sourceDirectory>src</sourceDirectory>
<plugins> <!-- plugins executes the testng tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<systemPropertyVariables>
<AppName>XYX</AppName>
<browserName>chrome</browserName>
<isLocal>true</isLocal>
</systemPropertyVariables>
<!-- Suite testng xml file to consider for test execution -->
<suiteXmlFiles>
<suiteXmlFile>${suiteFile}</suiteXmlFile>
</suiteXmlFiles>
<groups>${chooseGroup}</groups>
<includes>
<include>**.java</include>
</includes>
<testFailureIgnore>true</testFailureIgnore>
<argLine>
-javaagent:${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar
</argLine>
</configuration>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>
<!-- Compiler plugin configures the java version to be usedfor compiling the code -->
</plugins>
</build>
</project>
It doesnt matter if its jenkins or local build.
You have to specify you test in the following way:
mvn -Dtest=TestClass#testmethod test
You forgot about that #.

Cannot run multiple executions in maven surefire?

I want to run test classes whose name end with ResourceTest.java, so I defined following execution.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</configuration>
<version>2.12.2</version>
<executions>
<execution>
<id>resource-tests</id>
<phase>resource-tests</phase>
<goals>
<goal>resource-tests</goal>
</goals>
<configuration>
<includes>**/*ResourceTest.java</includes>
<!-- <exludes>**/*.java</exludes> -->
</configuration>
</execution>
</executions>
</plugin>
But I'm not sure how to run this, I've searched a lot and I'm missing something.
I tried surefire:test, it skipped all the test cases as defined in above configuration. So, I tried surefire:resource-tests, maven is saying no goal is not defined.
I'm using eclipse to run my maven build, by passing these parameters. How can I run by the execution id?
How to select a specific execution when running with surefire:test when I've mulltiple executions defined in my pom?
What am I missing? Any help would be appreciated.
There are several problems with your current configuration :
you are forcing the maven-surefire-plugin to be executed in the resource-tests phase but this phase does not exist. You should delete that declaration to keep the default plugin binding phase, which is test.
you are invoking the goal resource-tests but maven-surefire-plugin does not define such a goal.
the <includes> element is ill-defined. There should be a <include> tag under it.
you are excluding all Java files from the plugin configuration so no test will be run
the configuration of the plugin should be done under the <configuration> element not for each <executions>.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.2</version>
<configuration>
<includes>
<include>**/*ResourceTest.java</include>
</includes>
</configuration>
</plugin>
When you have multiple executions and you want "select" one of them, you can use a profile:
<profiles>
<profile>
<id>resource-tests</id>
<properties>
<test-classes>**/*ResourceTest.java</test-classes>
</properties>
</profile>
<profile>
<id>task-tests</id>
<properties>
<test-classes>**/*TaskTest.java</test-classes>
</properties>
</profile>
</profiles>
with the following plugin configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.2</version>
<configuration>
<includes>
<include>${test-classes}</include>
</includes>
</configuration>
</plugin>
With such a configuration:
when you run mvn clean test -Presource-tests, only the classes matching **/*ResourceTest.java will be tested
when you run mvn clean test -Ptask-tests, only the classes matching **/*TaskTest.java will be tested

How do I exclude specific tests by default but include them if activate a a certain profile?

Right now I am aware that if I use
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/Benchmark*.java</exclude>
</excludes>
</configuration>
</plugin>
I will be able to skip the tests I don't want to run. However, I want to have a specific profile whereby the above excluded tests will run if i build using that particular profile. I've tried
<profiles>
<profile>
<id>benchmark</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/Test*.java</include>
<include>**/*Test.java</include>
<include>**/*TestCase.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
along with the above but it seems like it doesn't work.
Maven is merging your configurations together instead of overwriting as you expect it to, so your final configuration with the profile active would look like:
<configuration>
<includes>
<include>**/Test*.java</include>
<include>**/*Test.java</include>
<include>**/*TestCase.java</include>
</includes>
<excludes>
<exclude>**/Benchmark*.java</exclude>
</excludes>
</configuration>
There's no need for all those default <include>s. They're already there anyway. Just put <excludes/> to tell Maven you want to stop doing the excludes that you specified in the basic POM. I.e., in your profile, just say:
<configuration>
<excludes/>
</configuration>
You could define your test source directory using a property, and override it using a custom profile, like this:
<properties>
<test.dir>src/test/java</test.dir>
</properties>
<profiles>
<profile>
<id>benchmark</id>
<properties>
<test.dir>src/benchmark-tests/java</test.dir>
</properties>
</profile>
</profiles>
<build>
<testSourceDirectory>${test.dir}</testSourceDirectory>
</build>
This way, executing mvn test would run all your tests inside src/test/java and mvn test -Pbenchmark would run the tests inside src/benchmark-tests/java.
Console output:
mvn clean test
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running br.com.instaweb.sample.SampleUnitTest
sample unit test was run
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.034 sec
and:
mvn clean test -Pbenchmark
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running br.com.stackoverflow.BenchmarkTest
benchmark test was run

Categories

Resources