pitest not able to locate junit test - java

My gradle pitest is not able to give me the right results. It looks like it is not able to locate my test files.
I have the following build.gradle file:
apply plugin: "java" apply plugin: "maven" apply plugin: "info.solidsoft.pitest"
group = "myorg" version = 1.0
repositories {
mavenCentral() }
sourceSets.all { set ->
def jarTask = task("${set.name}Jar", type: Jar) {
baseName = baseName + "-$set.name"
from set.output
}
artifacts {
archives jarTask
} }
sourceSets {
api
impl main{ java { srcDir 'src/api/java' srcDir 'src/impl/java' } } test { java { srcDir 'src/test/java' } } }
buildscript {
repositories {
mavenCentral()
//Needed only for SNAPSHOT versions
//maven { url "http://oss.sonatype.org/content/repositories/snapshots/" }
}
dependencies {
classpath 'info.solidsoft.gradle.pitest:gradle-pitest-plugin:1.1.6'
} }
dependencies {
apiCompile 'commons-codec:commons-codec:1.5'
implCompile sourceSets.api.output
implCompile 'commons-lang:commons-lang:2.6'
testCompile 'junit:junit:4.9'
testCompile sourceSets.api.output
testCompile sourceSets.impl.output
runtime configurations.apiRuntime
runtime configurations.implRuntime }
jar {
from sourceSets.api.output
from sourceSets.impl.output }
pitest { println sourceSets.main
targetClasses = ['doubler.*'] targetTests = ['doubler.*'] verbose="on" }
THe output is stored in the correct folder. And when I run gradle test, it also runs fine.

Some additional information about this issue was supplied in the pitest user group.
https://groups.google.com/forum/#!topic/pitusers/8C7BHh-Vb6Y
The tests being run look like this.
#Test
public void testIt2() {
assert new DoublerImpl().testIt(1) == 2;
}
Pitest is correctly reporting that these tests provide 0% coverage of the class. There is no coverage because the assert keyword has been used.
Unless the -ea flag is set in the JVM running the tests assertions are disabled. There is basically hidden if block around this code generated by the compiler
#Test
public void testIt2() {
if (assertionsEnabled) {
assert new DoublerImpl().testIt(1) == 2;
}
}
As assertions are not enabled no code is executed.
To fix the issue use the built in JUnit assertions instead.
http://junit.sourceforge.net/javadoc/org/junit/Assert.html

Related

java 9 modules with gradle not working: error: module not found: <module name>

I made a minimalist java gradle project to test java 9 modules.
I made only 1 module and tried testing depending on a third party module. But I keep getting the error when I try to build or run the project:
module not found: commons.validator
here are my files:
Demo.java"
public class Demo {
public static void main(String[] args) {
boolean result = EmailValidator.getInstance().isValid("abcd");
System.out.println(result);
}
}
module-info.java:
module auth.server.main {
requires commons.validator;
}
build.gradle:
plugins {
id 'java'
}
repositories {
mavenCentral()
}
dependencies {
// for email validation
implementation 'commons-validator:commons-validator:1.7'
}
/*this is to let gradle infer the locations of the modules*/
java {
modularity.inferModulePath.set(true)
}
Whats wrong and how to solve it?
This might easy way.
plugins {
id 'java'
id "org.javamodularity.moduleplugin" version "1.1.1"
}
repositories {
mavenCentral()
}
dependencies {
implementation 'commons-validator:commons-validator:1.7'
}
Check it out : https://github.com/java9-modularity/gradle-modules-plugin

Offline instrumentation with JaCoCo and Gradle

I have into the same problem quite a lot of people have here, which is getting proper code coverage information when using Jacoco/Gradle and Powermock.
I have read all the various threads here and in other places and I have successfully managed to create a task (for Gradle 6.4) that does offline instrumentation of my project's classes. For reference the code that does this is the following:
task instrumentClasses(dependsOn: [ classes, project.configurations.jacocoAnt ]) {
inputs.files classes.outputs.files
File outputDir = new File(project.buildDir, 'instrumented')
outputs.dir outputDir
doFirst {
project.delete(outputDir)
ant.taskdef(
resource: 'org/jacoco/ant/antlib.xml',
classpath: project.configurations.jacocoAnt.asPath,
uri: 'jacoco'
)
def instrumented = false
jacocoOfflineSourceSets.each { sourceSetName ->
if (file(sourceSets[sourceSetName as String].output.classesDirs.singleFile.absolutePath).exists()) {
def instrumentedClassedDir = "${outputDir}/${sourceSetName}"
ant.'jacoco:instrument'(destdir: instrumentedClassedDir) {
fileset(dir: sourceSets[sourceSetName as String].output.classesDirs.singleFile, includes: '**/*.class')
}
//Replace the classes dir in the test classpath with the instrumented one
sourceSets.test.runtimeClasspath -= sourceSets[sourceSetName as String].output.classesDirs
sourceSets.test.runtimeClasspath += files(instrumentedClassedDir)
instrumented = true
}
}
if (instrumented) {
//Disable class verification based on https://github.com/jayway/powermock/issues/375
test.jvmArgs += '-noverify'
}
}
}
Now, for the most part this seems to work alright. I have successfully verified that my classes are now properly instrumented and I'm seeing a Jacoco produced report which has the correct information. Problem is though that my SonarQube server still lists the classes in question as non covered. Regarding this I have no idea as to what I need to do to resolve it.
For reference I am using the following version of the sonarqube plugin:
"org.sonarqube" version "2.7"
And my CI runs the Gradle task in the following manner:
- ./gradlew jacocoTestReport sonarqube ${SONAR_GRADLE_EXTRA_PARAMS} -Dsonar.projectKey=${CI_PROJECT_ID} -Dsonar.host.url=${SONAR_URL} -Dsonar.login=${SONAR_LOGIN} -Dsonar.branch.name=${CI_COMMIT_REF_NAME};
I do get that it must be some configuration issue with either SonarQube or the way I run the Gradle task but I am not really sure as to what is the culprit.
If you are able to generate the aggregated jacoco report (aggregated from all source sets), then you can simply specify that in your sonarqube task while running (and sonar will just pick the exact coverage info that jacoco calculated)
./gradlew sonarqube -Dsonar.host.url=https://sonarcloud.io -Dsonar.login=XXXX -Dsonar.organization=XXXXX -Dsonar.coverage.jacoco.xmlReportPaths=build/jacoco-report.xml
FYI I am creating the aggregated report at build/jacoco-report.xml
Below is my gradle configuration (might be useful for you)
plugins {
id 'org.springframework.boot' version '2.3.1.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
id 'jacoco'
id "org.sonarqube" version "2.8"
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_11
repositories {
mavenCentral()
}
sourceSets {
intTest {
compileClasspath += sourceSets.main.output + sourceSets.test.output
runtimeClasspath += sourceSets.main.output + sourceSets.test.output
}
}
configurations {
intTestImplementation.extendsFrom testImplementation
intTestRuntimeOnly.extendsFrom testRuntimeOnly
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
useJUnitPlatform()
testLogging.showStandardStreams = true //To print logs
}
task integrationTest(type: Test) {
testClassesDirs = sourceSets.intTest.output.classesDirs
classpath = sourceSets.intTest.runtimeClasspath
shouldRunAfter test
testLogging.showStandardStreams = true //To print logs
}
jacocoTestReport {
executionData(file("${project.buildDir}/jacoco/test.exec"), file("${project.buildDir}/jacoco/integrationTest.exec"))
reports {
xml.enabled true
csv.enabled false
xml.destination file("${buildDir}/jacoco-report.xml")
html.destination file("${buildDir}/jacocoHtml")
}
mustRunAfter(test, integrationTest) // integration tests are required to run before generating the report
}
jacocoTestCoverageVerification {
executionData(file("${project.buildDir}/jacoco/test.exec"), file("${project.buildDir}/jacoco/integrationTest.exec"))
violationRules {
rule {
limit {
counter = 'INSTRUCTION'
minimum = 0.94
}
limit {
counter = 'BRANCH'
minimum = 1.0
}
}
}
}
check.dependsOn(integrationTest, jacocoTestCoverageVerification)
tasks.withType(Test) {
finalizedBy jacocoTestReport
}

Using Scala class in Java test with Gradle

I have a project with the following files:
src/main/scala/package/Class.scala
src/test/java/package/ClassTest.java
When I run ./gradlew clean build test I get an error in :compileTestJava - it says "error: cannot find symbol" because apparently ClassTest.java cannot find Class.scala
I have the following in build.gradle which I think should work but doesn't:
sourceSets {
test {
java { srcDirs += ['src/main/scala', 'src/test/java'] }
}
}
I also have the following plugins for what it is worth:
plugins {
id 'java'
id 'scala'
id 'java-library'
id 'maven-publish'
id 'jacoco'
}
The only other things in build.gradle are repositories and dependencies
I have simulated your scenario locally. The below works with Gradle 6.5 and 'java' and 'scala' plugins. My main srcDirs are slightly different from yours but I do not think that is material.
build.gradle:
sourceSets
{
main
{
scala {
srcDirs = ['app/glue', 'app/controllers', 'app/models']
}
}
test {
java {
srcDirs = ['test/java']
}
scala {
srcDirs = ['test/scala']
}
}
}
Sample JUnit test which refers to a scala case class Form which is defined in the 'main' source set:
public class TestScalaFromJava
{
#Test
public void testNothing() { }
#Test
public void testScalaClassInJava1() {
Firm firm = new Firm(1, "hello", "mello", "yellow");
}
}
I can then run either of the below and they work:
./gradlew compileTestJava
./gradlew clean build test

Building a project and a Spock test suite with gradle

I'm trying to build a Springboot web application that creates a WAR and also a suite of tests using the Spock framework but every time I try to run gradle build the console will execute the tests but will not create the web app WAR although it mentions a file in the web app source code saying a deprecation warning so I know it is at least doing something with it. Is there some kind of configuration I'm missing from my build.gradle or concept about how sourceSets work? I can compile the web app fine and the tests when I do them separately with their own build.gradle files but I was hoping that the code below could build the WAR and execute the tests in the same file.
plugins {
id 'java'
id 'maven-publish'
id 'war'
id 'groovy'
}
repositories {
mavenLocal()
maven {
url = 'https://repo.spring.io/libs-release'
}
maven {
url = 'http://repo.maven.apache.org/maven2'
}
}
sourceSets{
main {
java { srcDir "src\\TestingWebAppSpringBoot\\src\\main\\java" }
resources {srcDir "src\\TestingWebAppSpringBoot\\src\\main\\resources" }
groovy { srcDirs = [] }
}
test{
groovy {
srcDirs = ["src\\test_pack\\com\\example\\diag\\test\\spock\\main", "src\\test_pack\\com\\example\\diag\\test\\spock\\test" ]
}
}
}
test{
reports.html.destination = file("build\\spockTestResults")
}
dependencies {
compile 'org.springframework.boot:spring-boot-starter-thymeleaf:2.0.0.RELEASE'
compile 'org.springframework.boot:spring-boot-starter-web:2.0.0.RELEASE'
compile 'org.springframework.boot:spring-boot-starter-security:2.0.0.RELEASE'
compile 'org.codehaus.groovy:groovy-all:2.4.14'
providedCompile 'javax.servlet:javax.servlet-api:3.1.0'
testCompile (
fileTree(dir: 'build\\server\\_archives\\_release', include: '*.jar'),
fileTree(dir: 'build\\common\\_archives\\_release', include: '*.jar'),
'net.sourceforge.htmlunit:htmlunit:2.26',
"org.codehaus.groovy:groovy-all:2.4.5",
"org.spockframework:spock-core:1.0-groovy-2.4"
)
}
group = 'com.something.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
publishing {
publications {
maven(MavenPublication) {
from(components.java)
}
}
}
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}

AspectJ + Gradle configuration

I'd like to use AspectJ in Gradle project (it's not an Android project - just a simple Java app).
Here is how my build.gradle looks like:
apply plugin: 'java'
buildscript {
repositories {
maven {
url "https://maven.eveoh.nl/content/repositories/releases"
}
}
dependencies {
classpath "nl.eveoh:gradle-aspectj:1.6"
}
}
repositories {
mavenCentral()
}
project.ext {
aspectjVersion = "1.8.2"
}
apply plugin: 'aspectj'
dependencies {
//aspectj dependencies
aspectpath "org.aspectj:aspectjtools:${aspectjVersion}"
compile "org.aspectj:aspectjrt:${aspectjVersion}"
}
The code compiles, however the aspect seems to not be weaved. What could be wrong?
I have been struggled with this for a while, so this the config I use and works well.
In your configuration do this.
configurations {
ajc
aspects
aspectCompile
compile{
extendsFrom aspects
}
}
In your dependencies use the following configuration. Spring dependencies are not needed if you are not using spring fwk.
dependencies {
//Dependencies required for aspect compilation
ajc "org.aspectj:aspectjtools:$aspectjVersion"
aspects "org.springframework:spring-aspects:$springVersion"
aspectCompile "org.springframework:spring-tx:$springVersion"
aspectCompile "org.springframework:spring-orm:$springVersion"
aspectCompile "org.hibernate.javax.persistence:hibernate-jpa-2.1-api:$hibernateJpaVersion"
}
compileJava {
sourceCompatibility="1.7"
targetCompatibility="1.7"
//The following two lines are useful if you have queryDSL if not ignore
dependsOn generateQueryDSL
source generateQueryDSL.destinationDir
dependsOn configurations.ajc.getTaskDependencyFromProjectDependency(true, "compileJava")
doLast{
ant.taskdef( resource:"org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties", classpath: configurations.ajc.asPath)
ant.iajc(source:"1.7", target:"1.7", destDir:sourceSets.main.output.classesDir.absolutePath, maxmem:"512m", fork:"true",
aspectPath:configurations.aspects.asPath,
sourceRootCopyFilter:"**/.svn/*,**/*.java",classpath:configurations.compile.asPath){
sourceroots{
sourceSets.main.java.srcDirs.each{
pathelement(location:it.absolutePath)
}
}
}
}
}
I dont use the plugin I use the ant and aspectj compiler to do that, probably there will be an easy way
Just want to add the so called "official" plugin for AspectJ mentioned by Archie.
Here's some gradle script example on how to do it:
apply plugin: 'java'
sourceCompatibility = '1.8'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
if (!hasProperty('mainClass')) {
ext.mainClass = 'com.aspectz.Main'
}
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "gradle.plugin.aspectj:gradle-aspectj:0.1.6"
//classpath "gradle.plugin.aspectj:plugin:0.1.1"
//classpath "gradle.plugin.aspectj:gradle-aspectj:0.1.1"
}
}
ext {
aspectjVersion = '1.8.5'
}
apply plugin: "aspectj.gradle"
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.10'
compile("log4j:log4j:1.2.16")
compile("org.slf4j:slf4j-api:1.7.21")
compile("org.slf4j:slf4j-log4j12:1.7.21")
compile("org.aspectj:aspectjrt:1.8.5")
compile("org.aspectj:aspectjweaver:1.8.5")
}
However, it seems that it only supports Java 8 and above. As when you use java 7 to build it, it got error :
java.lang.UnsupportedClassVersionError: aspectj/AspectJGradlePlugin : Unsupported major.minor version 52.0
Looks like there is a new "official" gradle plugin for AspectJ:
https://plugins.gradle.org/plugin/aspectj.gradle
Unfortunately the documentation is minimal. I haven't tried it myself.
This plugin worked for me (gradle 5.6):
plugins {
id 'java'
id "io.freefair.aspectj.post-compile-weaving" version "4.1.4"
}
A bit ugly, but short and does not require additional plugins or configurations.
Works for me:
Add aspectjweaver dependency ti Gradle build script. Then in the task you need it find the path to its jar and pass as javaagent in jvmArgs:
dependencies {
compile "org.aspectj:aspectjweaver:1.9.2"
}
test {
group 'verification'
doFirst {
def weaver = configurations.compile.find { it.name.contains("aspectjweaver") }
jvmArgs = jvmArgs << "-javaagent:$weaver"
}
}
Add aop.xmlfile to the resources\META-INF\ folder with the specified Aspect class:
<aspectj>
<aspects>
<aspect name="utils.listener.StepListener"/>
</aspects>
</aspectj>
Example of an aspect:
package utils.listener
import org.aspectj.lang.JoinPoint
import org.aspectj.lang.annotation.*
import org.aspectj.lang.reflect.MethodSignature
#Aspect
#SuppressWarnings("unused")
public class StepListener {
/* === Pointcut bodies. Should be empty === */
#Pointcut("execution(* *(..))")
public void anyMethod() {
}
#Pointcut("#annotation(org.testng.annotations.BeforeSuite)")
public void withBeforeSuiteAnnotation() {
}
}
Adding this in my build.gradle file worked for me.
project.ext {
aspectjVersion = '1.8.12'
}
apply plugin: "aspectj.gradle"
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "gradle.plugin.aspectj:gradle-aspectj:0.1.6"
}
}
Doing a gradle assemble injected the code defined in the aspect.

Categories

Resources