SystemProperties from maven failsafe plugin with Spock Tests - java

I am trying to get maven failsafe to execute my integration tests, however I am having a problem with the server port being correctly assigned to the system properties.
My failsafe plugin configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.21.0</version>
<executions>
<execution>
<id>integration-tests</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
<systemPropertyVariables>
<test.server.port>${tomcat.http.port}</test.server.port>
</systemPropertyVariables>
<skipTests>${skip.integration.tests}</skipTests>
</configuration>
</execution>
</executions>
</plugin>
My spock test configuration:
def "verifies port is being set correctly"() {
expect:
System.getProperties().getProperty('test.server.port') != null
}
When I run the test, test.server.port does not appear in my system properties array. However, if I change it to asdf or anything NOT test.server.port, it then appears in my system properties.

The fix was to use Spring Boot's random port for environment initialization
#SpringBootTest(
classes = OrderQueueApplication.class,
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT
)

Related

How to integrate Spring Instrument javaagent in ALL JUnit tests

I am writing unit tests on a large project which I need to pass JVM arguments to, those are my JVM arguments built into the Eclipse run configuration for that project :
--module-path lib/javafx-sdk-13.0.2/lib --add-modules=javafx.controls
-javaagent:lib/aspectjweaver-1.9.5.jar
-javaagent:lib/spring-instrument-5.2.3.RELEASE.jar
My issue is that I need to add those arguments for EVERY JUnit test or testing sequence. Is there a better approach for this? Some way to not have to add those arguments manually into every new test I create?
******EDIT******
This also has the nasty side-effect of not letting me build this project at all! Maven does not use my custom JUnit run config for running the entire set of tests for the application (which works fine because I set the JVM arguments in there) but rather its own which obviously fails because the arguments are not there. That is a huge problem, is there a way to "hardcode" those JVM arguments directly into the POM somehow?
******EDIT 2******
This is my Spring-Boot-Maven-Plugin config in my POM.xml file :
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<jvmArguments>
--module-path lib/javafx-sdk-13.0.2/lib
--add-modules=javafx.controls
-javaagent:lib/aspectjweaver-1.9.5.jar
-javaagent:lib/spring-instrument-5.2.3.RELEASE.jar
</jvmArguments>
</configuration>
</plugin>
******SOLUTION******
Adding the Maven Surefire plugin and setting it up this way fixed the issue :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
<configuration>
<argLine>
--module-path lib/javafx-sdk-13.0.2/lib
--add-modules=javafx.controls
-javaagent:lib/aspectjweaver-1.9.5.jar
-javaagent:lib/spring-instrument-5.2.3.RELEASE.jar
</argLine>
</configuration>
</plugin>
Thanks!
You can set the jvm args in the surefire plugin. Use mvn test to run tests. Something like
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
<configuration>
<argLine>-Djava.security.policy=${basedir}/src/test/resources/java.policy</argLine>
</configuration>
</plugin>
</plugins>
More here http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#argLine

maven-surefire-plugin how to rerun failed TestNG tests? There is default rerun option for JUnit only

How can I run the failed TestNG tests with maven failsafe plugin?
I run my tests with 'mvn clean install'.
When I run the tests and I get failing tests, I need to rerun them.
I do not have a testng.xml file and I think that this is also an issue.
This my pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.17</version>
<configuration>
<parallel>methods</parallel>
<threadCount>${threads}</threadCount>
<systemProperties>
<browser>${browser}</browser>
<screenshotDirectory>${project.build.directory}/screenshots</screenshotDirectory>
<remoteDriver>${remote}</remoteDriver>
<gridURL>${seleniumGridURL}</gridURL>
<desiredPlatform>${platform}</desiredPlatform>
<desiredBrowserVersion>${browserVersion}</desiredBrowserVersion> <!--Set properties passed in by the driver binary downloader-->
<!--Set properties passed in by the driver binary downloader-->
<phantomjs.binary.path>${phantomjs.binary.path}</phantomjs.binary.path>
<webdriver.chrome.driver>${webdriver.chrome.driver}</webdriver.chrome.driver>
<webdriver.ie.driver>${webdriver.ie.driver}</webdriver.ie.driver>
<webdriver.opera.driver>${webdriver.opera.driver}</webdriver.opera.driver>
</systemProperties>
<includes>
<!-- All that is left now is to clean up the code in our basicTest class and change its name to BasicTestWD. You may have noticed that we added an <includes> configuration item to our POM. This is because Maven will use maven-surefireplugin to run files that have test at the start or end of their name. We don't want maven-surefire-plugin to pick up our tests; we want to use maven-failsafeplugin instead. -->
<include>**/*WD.java</include>
</includes>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.lazerycode.selenium</groupId>
<artifactId>driver-binary-downloader-maven-plugin</artifactId>
<version>1.0.7</version>
<configuration>
<rootStandaloneServerDirectory>
${project.basedir}/src/test/resources/selenium_standalone_binaries
</rootStandaloneServerDirectory>
<downloadedZipFileDirectory>${project.basedir}/src/test/resources/selenium_standalone_zips
</downloadedZipFileDirectory>
<customRepositoryMap>${project.basedir}/src/test/resources/RepositoryMap.xml
</customRepositoryMap>
<overwriteFilesThatExist>${overwrite.binaries}</overwriteFilesThatExist>
</configuration>
<executions>
<execution>
<goals>
<goal>selenium</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Is possible to run same test multiple times with Maven Surefire Plugin?

There is a config value rerunFailingTestsCount but I want to run a test method a configurable number of times even it is successful. Are there any options?
I don't think it is possible to configure maven-surefire-plugin to rerun passing tests.
However, you can configure the invocation count of a single test using the TestNG (not JUnit) #Test annotation:
#Test(invocationCount = 5)
public void testSomething() {
}
This will result in the testSomething method being tested 5 times.
If you don't want to go the TestNG route, you can refer to this answer for a solution with JUnit.
If you want to have it configurable by implementing the IInvokedMethodListener beforeInvocation method, something to the effect :
method.getTestMethod().setInvocationCount(Integer.parseInt(System.getProperty("configurablecount")));
System.getProperty can be replaced by however you want to configure it. You can also probably control which tests to set the invocation count to change by passing testnames.
Yes
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>phase-1</id>
<phase>test</phase>
<configuration>
<skip>false</skip>
<!-- Phase 1 configuration -->
</configuration>
<goals>
<goal>test</goal>
</goals>
</execution>
<execution>
<id>phase-2</id>
<phase>test</phase>
<configuration>
<skip>false</skip>
<!-- Phase 2 configuration -->
</configuration>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Jenkins and Jacoco Integration Test Coverage

I just added Jacoco on my maven dependencies to run integration tests. Then, I created an integration test to test my controller. For example, I tested my HTTP response codes, the headers and the response resources. After that, I created a profile on maven that starts an embedded tomcat. So, everytime I want to run my integration tests, I just put the profile on the maven goals. However, when I execute the build on Jenkins and Sonar reads the reports from Jacoco, the reports says that I have not tested my controller. The question is: How I tell Jacoco that I have passed through my Controllers, Services and Repositories?
Thanks to all!
Are you getting any Integration Coverage, or just 0%?
It can be quite tricky to set up Integration Test Coverage using maven and Sonar.
Check there is a jacoco file produced when the IT tests are run.
Check your POM set up compared to this...
<properties>
<!-- Jacoco Properties -->
<jacoco.version>0.7.4.201502262128</jacoco.version>
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
<sonar.core.codeCoveragePlugin>jacoco</sonar.core.codeCoveragePlugin>
<sonar.jacoco.itReportPath>${project.basedir}/target/jacoco-it.exec</sonar.jacoco.itReportPath>
<sonar.language>java</sonar.language>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<executions>
<execution>
<id>prepare-unit-test-agent</id>
<configuration>
</configuration>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
<phase>verify</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>prepare-it-test-agent</id>
<configuration>
<propertyName>jacoco.agent.argLine</propertyName>
<destFile>${sonar.jacoco.itReportPath}</destFile>
<append>true</append>
</configuration>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.12.4</version>
<configuration>
<argLine>${jacoco.agent.argLine}</argLine>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Link to GitHub example
BeyondCoding.net

Maven: plugin to fail a build if a string is found

During development, I have an habit of wrapping code that should not be in production inside "TODEL" tag. For example:
//TODEL - START
//used to test the crashing behavior
String s = null;
int i = s.length;
//TODEL - END
Is there a maven plugin that can fail a build in jenkins if I accidentally checkin a file that contains "TODEL"?
One thing you can do is to use maven checkstyle plugin . You can set up a rule and the make the build fail if it is not compliant to those rules.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<configuration>
<configLocation>my-checkstyle.xml</configLocation>
</configuration>
</plugin>
The configuration property maven.checkstyle.fail.on.violation.
Then mvn checkstyle:check. Or configure it to execute in a phase of your choice (compile or process-resources) by adding to the plugin configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<executions>
<execution>
<id>TODEL</id>
<configuration>
<configLocation>my-checkstyle.xml</configLocation>
</configuration>
<goals>
<goal>check</goal>
</goals>
<phase>validate</phase>
</execution>
</executions>
</plugin>
More info: http://maven.apache.org/plugins/maven-checkstyle-plugin

Categories