SpringBoot2.4.4 Junit5 with JaCoco test coverage is not shown - spring-boot

i am using junit5 with springboot 2.4.4 version. I have integrated jacoco for the test coverage. However my test gets passed but coverage is not shown in it.
Here i am mention a demo class of the concept what i am implemented
public class Calculator {
public int sum() {
return sumPrivate();
}
private int sumPrivate() {
return 100;
}
}
And this is test class of it
public class CalculatorTest {
#Test
public void test() {
assertNotNull(new Calculator().sum());
}
}
i have debugged my class private method gets called but not shown in jacoco code coverage.
And here is my plugin configuration
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>unit-test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<skip>${maven.test.skip}</skip>
<argLine>${argLine}</argLine>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
</excludes>
</configuration>
</execution>
<execution>
<id>integration-test</id>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<skip>${skipITs}</skip>
<argLine>${argLine}</argLine>
<includes>
<include>**/*IntegrationTest.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.6</version>
<configuration>
<skip>${maven.test.skip}</skip>
<destFile>${basedir}/target/coverage-reports/jacoco-unit.exec</destFile>
<dataFile>${basedir}/target/coverage-reports/jacoco-unit.exec</dataFile>
<output>file</output>
<append>true</append>
</configuration>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
<phase>verify</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>

I am still facing this issue right now. I used spring-boot-maven-plugin and some of the classes were coverage were detected. But still it is not a solution!
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<jvmArguments>${argLine}</jvmArguments>
</configuration>
<executions>
<execution>
<id>start-spring-boot</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop-spring-boot</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>

You need to specify goal check after test in-order to check coverage.
If you also need to generate report then goal is report
Here are all the possible goals. Got to the end of page.
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>coverage-validation</id>
<goals>
<goal>check</goal>
</goals>
<phase>test</phase>
<configuration>
<rules>
<rule>
<limits>
<limit>
<counter>INSTRUCTION</counter>
<value>COVEREDRATIO</value>
<minimum>0.90</minimum>
</limit>
</limits>
</rule>
</rules>
<haltOnFailure>true</haltOnFailure>
</configuration>
</execution>
</executions>

Related

swagger-codegen-maven-plugin is not generating data-time

I am using <dateLibrary>java11-localdatetime</dateLibrary> but it is generating the code with java.util.Date and ignoring the data type format: date-time.
I have to use typeMapping to replace the 'util date' to LocalDate as shown in the below
configuration:
<plugin>
<groupId>io.swagger.codegen.v3</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>3.0.27</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/spec.yaml</inputSpec>
<language>spring</language>
<output>${project.basedir}</output>
<modelPackage>com.generated.model</modelPackage>
<apiPackage>com.generated.api</apiPackage>
<generateModels>true</generateModels>
<generateModelDocumentation>false</generateModelDocumentation>
<generateApis>true</generateApis>
<generateApiDocumentation>false</generateApiDocumentation>
<generateApiTests>false</generateApiTests>
<generateSupportingFiles>false</generateSupportingFiles>
<configOptions>
<hideGenerationTimestamp>true</hideGenerationTimestamp>
<dateLibrary>java11-localdatetime</dateLibrary>
</configOptions>
<importMappings>
<importMapping>Date=java.time.LocalDate</importMapping>
</importMappings>
<typeMappings>
<typeMapping>Date=LocalDate</typeMapping>
</typeMappings>
</configuration>
</execution>
</executions>
</plugin>
This has been resolved by using the following configurations:
<configOptions>
<hideGenerationTimestamp>true</hideGenerationTimestamp>
<dateLibrary>java8-localdatetime</dateLibrary>
<interfaceOnly>true</interfaceOnly>
<defaultInterfaces>false</defaultInterfaces>
</configOptions>
Useful links:
swagger-codegen default interface

Netbeans 11 - Maven and JavaFX - javafx.application does not exist

This is a new project, created clicking New project -> Java with Maven -> JavaFX Application
As the project is crated, all lines of code related to javafx gerate an error "package javafx.application does not exist".
During the creation of the project I have been asked to unstall the javafx plugin, so I did.
Here is my MainApp.class
package com.mycompany.mavenproject1;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class MainApp extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("/fxml/Scene.fxml"));
Scene scene = new Scene(root);
scene.getStylesheets().add("/styles/Styles.css");
stage.setTitle("JavaFX and Maven");
stage.setScene(scene);
stage.show();
}
/**
* The main() method is ignored in correctly deployed JavaFX application.
* main() serves only as fallback in case the application can not be
* launched through deployment artifacts, e.g., in IDEs with limited FX
* support. NetBeans ignores main().
*
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Here is the pom file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>mavenproject1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>mavenproject1</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<mainClass>com.mycompany.mavenproject1.MainApp</mainClass>
</properties>
<organization>
<!-- Used as the 'Vendor' for JNLP generation -->
<name>Your Organisation</name>
</organization>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>unpack-dependencies</id>
<phase>package</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<excludeScope>system</excludeScope>
<excludeGroupIds>junit,org.mockito,org.hamcrest</excludeGroupIds>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<id>unpack-dependencies</id>
<phase>package</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>${java.home}/../bin/javafxpackager</executable>
<arguments>
<argument>-createjar</argument>
<argument>-nocss2bin</argument>
<argument>-appclass</argument>
<argument>${mainClass}</argument>
<argument>-srcdir</argument>
<argument>${project.build.directory}/classes</argument>
<argument>-outdir</argument>
<argument>${project.build.directory}</argument>
<argument>-outfile</argument>
<argument>${project.build.finalName}.jar</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>default-cli</id>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>${java.home}/bin/java</executable>
<commandlineArgs>${runfx.args}</commandlineArgs>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilerArguments>
<bootclasspath>${sun.boot.class.path}${path.separator}${java.home}/lib/jfxrt.jar</bootclasspath>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<additionalClasspathElements>
<additionalClasspathElement>${java.home}/lib/jfxrt.jar</additionalClasspathElement>
</additionalClasspathElements>
</configuration>
</plugin>
</plugins>
</build>
</project>

Integrating security realm through Wildfly Maven plugin

It's kinda the same question as this one, but the answer didn't work for me. Also I don't know how he got the output of jboss-cli in his stacktrace, that would help a lot for debugging.
Anyway I'm trying to deploy a security realm in Wildfly 10 through Maven. It works fine when doing it through the console (with the commented lines in the code below) but not with Maven Wildfly plugin.
<configuration>
<!--add security domain-->
<before-deployment>
<commands>
<!--<command>./subsystem=security/security-domain=walbangSecureRealm:add(cache-type="default")</command>-->
<!--<command>cd ./subsystem=security/security-domain=walbangSecureRealm</command>-->
<!--<command>./authentication=classic:add(login-modules=[{code="Database",flag="required",module-options={dsJndiName="java:/walbangDbDS",principalsQuery="select password from usercredential uc where uc.username = ?",rolesQuery="select role, 'Roles' from users u where u.username = ?",unauthenticatedIdentity="guest"}}])</command>-->
<command>/subsystem=security/security-domain=walbangSecureRealm:add(cache-type=default)</command>
<command>/subsystem=security/security-domain=walbangSecureRealm/authentication=classic:add(login-modules=[{"code"=>"Database","flag"=>"required","module-options"=>[("dsJndiName"=>"java:/walbangDbDS"),("principalsQuery"=>"select password from usercredential uc where uc.username = ?"),("rolesQuery"=>"select role, 'Roles' from users u where u.username = ?"),("unauthenticatedIdentity"=>"guest")]}])</command>
<command>reload</command>
</commands>
</before-deployment>
</configuration>
The error in the stacktrace is :
12:18:54,236 ERROR [org.jboss.as.controller.management-operation] (management-handler-thread - 2) WFLYCTL0013: Operation ("deploy") failed - address: ([("deployment" => "walbang-0.0.1-SNAPSHOT.war")]) - failure description: {"WFLYCTL0180: Services with missing/unavailable dependencies" => [
"jboss.deployment.unit.\"walbang-0.0.1-SNAPSHOT.war\".component.ManageMatchServiceImpl.CREATE is missing [jboss.security.security-domain.walbangSecureRealm]",...
Here is more info of what I have in Maven:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>true</failOnMissingWebXml>
</configuration>
</plugin>
<!--wildfly plugin-->
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<version>1.1.0.Alpha8</version>
<executions>
<!-- Deploy the JDBC driver -->
<execution>
<id>deploy-driver</id>
<phase>package</phase>
<configuration>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<name>mysql-connector</name>
</configuration>
<goals>
<goal>deploy-artifact</goal>
</goals>
</execution>
<!-- Add a data source -->
<execution>
<id>add-datasource</id>
<phase>package</phase>
<configuration>
<address>subsystem=datasources,data-source=java:/walbangDbDS</address>
<resources>
<resource>
<enableResource>true</enableResource>
<properties>
<connection-url>jdbc:mysql://localhost:3306/forumcs</connection-url>
<jndi-name>java:/walbangDbDS</jndi-name>
<enabled>true</enabled>
<enable>true</enable>
<user-name>root</user-name>
<password>123456</password>
<driver-name>mysql-connector</driver-name>
<use-ccm>false</use-ccm>
</properties>
</resource>
</resources>
</configuration>
<goals>
<goal>add-resource</goal>
</goals>
</execution>
<!-- Deploy the application on install -->
<execution>
<id>deploy</id>
<phase>install</phase>
<goals>
<goal>deploy</goal>
</goals>
</execution>
</executions>
<configuration>
<!--add security domain-->
<before-deployment>
<commands>
<!--<command>./subsystem=security/security-domain=walbangSecureRealm:add(cache-type="default")</command>-->
<!--<command>cd ./subsystem=security/security-domain=walbangSecureRealm</command>-->
<!--<command>./authentication=classic:add(login-modules=[{code="Database",flag="required",module-options={dsJndiName="java:/walbangDbDS",principalsQuery="select password from usercredential uc where uc.username = ?",rolesQuery="select role, 'Roles' from users u where u.username = ?",unauthenticatedIdentity="guest"}}])</command>-->
<command>/subsystem=security/security-domain=walbangSecureRealm:add(cache-type=default)</command>
<command>/subsystem=security/security-domain=walbangSecureRealm/authentication=classic:add(login-modules=[{"code"=>"Database","flag"=>"required","module-options"=>[("dsJndiName"=>"java:/walbangDbDS"),("principalsQuery"=>"select password from usercredential uc where uc.username = ?"),("rolesQuery"=>"select role, 'Roles' from users u where u.username = ?"),("unauthenticatedIdentity"=>"guest")]}])</command>
<command>reload</command>
</commands>
</before-deployment>
</configuration>
</plugin>
</plugins>

Get Code Coverage for GWT project using Jacoco surefire

I am having GWT maven project and want to find code coverage.
I am getting the report with coverage as 0%.
pom.xml
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>gwt-maven-plugin</artifactId>
<version>2.7.0</version>
<executions>
<execution>
<configuration>
<extraJvmArgs>-Xmx512m</extraJvmArgs>
</configuration>
<goals>
<goal>compile</goal>
<goal>generateAsync</goal>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.6</version>
<configuration>
<argLine>${jacocoArgs}</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.jboss.errai</groupId>
<artifactId>jacoco-gwt-maven-plugin</artifactId>
<version>0.5.4.201202141554</version>
<configuration>
<snapshotDirectory>${project.build.directory}/test-classes</snapshotDirectory>
<propertyName>jacocoArgs</propertyName>
</configuration>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
<phase>package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
GWTTestCase
public class GwtTestCovergeSample extends GWTTestCase {
public String getModuleName() {
return "com.test.sample.CovergeSampleJUnit";
}
public void testFieldVerifier() {
}
public void testGreetingService() {
GreetingServiceAsync greetingService = GWT.create(GreetingService.class);
ServiceDefTarget target = (ServiceDefTarget) greetingService;
target.setServiceEntryPoint(GWT.getModuleBaseURL() + "CovergeSample/greet");
delayTestFinish(10000);
greetingService.greetServer("GWT User", new AsyncCallback<String>() {
public void onFailure(Throwable caught) {
System.out.println("GwtTestCovergeSample.testGreetingService().new AsyncCallback() {...}.onFailure()");
fail("Request failure: " + caught.getMessage());
}
public void onSuccess(String result) {
assertTrue(result.startsWith("Hello, GWT User!"));
finishTest();
}
});
}
}
CoverageSampleJUnit.gwt.xml
<?xml version="1.0" encoding="UTF-8"?>
<module rename-to="CovergeSample">
<inherits name='com.test.sample.CovergeSample' />
<servlet path="/CovergeSample/greet" class="com.test.sample.server.GreetingServiceImpl" />
</module>
In target folder, I can see site folder is getting created with correct report index.html file.
When I open index.html file coverage report is 0%.
Someone please help.

assign output of exec-maven-plugin to variable

I want to use exec-maven-plugin to get git 'revision', so I'm using following configuration:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<id>gitVersion</id>
<phase>validate</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>git</executable>
<workingDirectory>./</workingDirectory>
<arguments>
<argument>rev-list</argument>
<argument>master</argument>
<argument>--count</argument>
</arguments>
</configuration>
</plugin>
but I hit a problem - how do I assign output to any variable available in other plugins/livecycles?
(I was able to get it done using gmaven-plugin and executing groovy script, but I find it a bit of overkill/less elegant)
EDIT:
for reference, working solution in groovy:
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<providerSelection>2.0</providerSelection>
<properties>
<script>git rev-list master --count</script>
</properties>
<source>
def command = project.properties.script
def process = command.execute()
process.waitFor()
def describe = process.in.text.trim()
println "setting revision to: " + describe
project.properties.setProperty('gitVersion',describe)
</source>
</configuration>
</execution>
</executions>
</plugin>
Just for clarity, here's the solution using groovy:
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<providerSelection>2.0</providerSelection>
<properties>
<script>git rev-list master --count</script>
</properties>
<source>
def command = project.properties.script
def process = command.execute()
process.waitFor()
def describe = process.in.text.trim()
println "setting revision to: " + describe
project.properties.setProperty('gitVersion',describe)
</source>
</configuration>
</execution>
</executions>
</plugin>

Resources