I have this root build.gradle
repositories {
jcenter()
}
subprojects {
apply plugin: 'java'
group 'me.someone'
version '1.0.0'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
jcenter()
mavenCentral()
}
dependencies {
testImplementation 'junit:junit:4.12'
}
}
Then I have this child build.gradle
plugins {
id 'java-library'
id 'eclipse'
id "org.springframework.boot" version "2.0.1.RELEASE"
id 'io.spring.dependency-management' version "1.0.5.RELEASE"
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
compile project(':foo-jar')
testImplementation('org.springframework.boot:spring-boot-starter-test')
testImplementation group: 'org.mockito', name: 'mockito-core', version: '2.18.3'
}
sourceSets {
main {
java {
srcDir 'src/main/java'
}
}
test {
java.srcDir file('src/int/java')
}
itest {
java {
srcDir file('src/itest/java')
}
//resources.srcDir 'src/itest/resources'
}
}
test {
testLogging {
showStandardStreams = true
events "passed", "skipped", "failed"
exceptionFormat = 'full'
}
}
task itest(type: Test) {
testLogging {
showStandardStreams = true
events "passed", "skipped", "failed"
exceptionFormat = 'full'
}
itest.mustRunAfter test
}
check.dependsOn itest
bootRun {
main = 'me.someone.BarServiceApplication'
}
The issue is unit test runs twice but not the integration test. Why is unit test running twice but not integration test? My understanding is when I provided the source folder of integration test, it should run the integration test as well.
Your task itest needs to have its testClassesDirs configured, that is why it is currently running your unit tests twice. It might also need the classpath configured.
And you should have a look at the Gradle documentation on how to do integration tests for all the details.
Related
I can't resolve the groovy.json.JsonSlurper in the build.gradle file with Intellij, does anyone know how to fix it?
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}
test {
useJUnitPlatform()
}
task sample() {
doLast {
sample();
}
}
import groovy.json.JsonSlurper
def sample(){
def json = new JsonSlurper().parseText('{"a":"b"}')
println(json);
}
You missing the needed dependency.
Add org.codehaus.groovy:groovy-json:3.0.9 in dependencies section so ti will look like
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
implementation 'org.codehaus.groovy:groovy-json:3.0.9'
}
And then you can test with CLI as gradle sample or ./gradlew sample and it will return {a=b}
I am beating against a wall for a whole week. I can't really see the reason why it doesn't work. I have a project where I want to run my Java project with the following build.gradle config:
plugins {
id 'java'
id 'org.springframework.boot' version '2.6.3'
id("io.spring.dependency-management") version "1.0.11.RELEASE"
id 'org.web3j' version "4.8.4"
}
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
mavenCentral()
jcenter()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
group = 'com.suplab'
sourceSets {
main {
solidity {
srcDir "contracts"
}
}
test {
solidity {
srcDir "contracts"
}
}
}
ext {
web3jVersion = '4.8.4'
}
npmInstall {
enabled false
}
dependencies {
implementation "org.web3j:core:$web3jVersion"
implementation "org.web3j:web3j-evm:$web3jVersion"
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.hibernate:hibernate-core:5.6.5.Final'
}
I use gradle version 6.8, since it was specified by developer of the plugin. When I try to build this project from the parent, root folder that is the root of the project, it fails with following error:
A problem occurred configuring project ':api'.
> Failed to notify project evaluation listener.
> Task with name 'resolveSolidity' not found in project ':api'.
> Could not create task ':api:generateTestContractWrappers'.
> Task with name 'compileTestSolidity' not found in project ':api'.
I can't figure it out. I checked it against the build that being generated with web3j-cli, and it's the same for the configuration property (both configs have it blank). So, I don't see any reason WHY it fails. Here's the webj3-cli generated build file:
plugins {
id 'java'
id 'org.jetbrains.kotlin.jvm' version '1.3.61'
id 'application'
id "com.github.johnrengelman.shadow" version "5.2.0"
id 'org.web3j' version '4.8.4'
}
group 'org.web3j'
version '0.1.0'
sourceCompatibility = 1.8
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
mavenCentral()
jcenter()
}
web3j {
generatedPackageName = 'org.web3j.generated.contracts'
excludedContracts = ['Mortal']
}
ext {
web3jVersion = '4.8.4'
logbackVersion = '1.2.3'
}
dependencies {
implementation "org.web3j:core:$web3jVersion",
"ch.qos.logback:logback-core:$logbackVersion",
"ch.qos.logback:logback-classic:$logbackVersion"
implementation "org.web3j:web3j-unit:$web3jVersion"
implementation "org.web3j:web3j-evm:$web3jVersion"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'
}
jar {
manifest {
attributes(
'Main-Class': 'org.web3j.Web3App',
'Multi-Release':'true'
)
}
}
application {
mainClassName = 'org.web3j.Web3App'
}
test {
useJUnitPlatform()
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
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
}
The generated protobuf class is under generated-sources as expected.
But it has references to com.google.protobuf, for example below code. And I get compilation error saying com.google.protobuf not found.
public static void registerAllExtensions(
com.google.protobuf.ExtensionRegistryLite registry) {
}
The below is my build.gradle file.
plugins {
id 'org.springframework.boot' version '2.2.2.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
id 'com.google.protobuf' version '0.8.10'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
sourceSets {
main {
proto {
srcDir 'src/main/proto'
}
java {
// include self written and generated code
srcDirs 'src/main/java', 'generated-sources/main/java'
}
}
// remove the test configuration - at least in your example you don't have a special test proto file
}
protobuf {
// Configure the protoc executable
protoc {
// Download from repositories
artifact = 'com.google.protobuf:protoc:3.0.0'
}
generateProtoTasks.generatedFilesBaseDir = 'generated-sources'
generateProtoTasks {
// all() returns the collection of all protoc tasks
all().each { task ->
// Here you can configure the task
}
// In addition to all(), you may get the task collection by various
// criteria:
// (Java only) returns tasks for a sourceSet
ofSourceSet('main')
}
}
I think the problem is that the protobuf library is not showing up in the external libraries of my intellij project. Is there a way to make it work with gradle?
Working gradle file:
plugins {
id 'org.springframework.boot' version '2.2.2.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
id 'com.google.protobuf' version '0.8.10'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'com.google.protobuf:protobuf-java:3.11.1'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
sourceSets {
main {
proto {
srcDir 'src/main/proto'
}
java {
// include self written and generated code
srcDirs 'src/main/java', 'generated-sources/main/java'
}
}
// remove the test configuration - at least in your example you don't have a special test proto file
}
protobuf {
// Configure the protoc executable
protoc {
// Download from repositories
artifact = 'com.google.protobuf:protoc:3.6.0'
}
generateProtoTasks.generatedFilesBaseDir = 'generated-sources'
generateProtoTasks {
// all() returns the collection of all protoc tasks
all().each { task ->
// Here you can configure the task
}
// In addition to all(), you may get the task collection by various
// criteria:
// (Java only) returns tasks for a sourceSet
ofSourceSet('main')
}
}
build.gradle:
plugins {
id 'java'
id 'application'
id 'org.openjfx.javafxplugin' version '0.0.8'
}
mainClassName = 'mvc.Main'
repositories {
mavenCentral()
}
dependencies {
testImplementation('org.junit.jupiter:junit-jupiter-api:5.6.0-M1')
testRuntime('org.junit.jupiter:junit-jupiter-engine:5.6.0-M1')
testCompile('org.mockito:mockito-core:3.1.0')
testCompile('com.athaydes.automaton:Automaton:1.3.2')
testCompile("org.testfx:testfx-core:4.0.16-alpha")
testCompile("org.testfx:testfx-junit:4.0.15-alpha")
testImplementation('org.hamcrest:hamcrest:2.2')
implementation 'com.github.cliftonlabs:json-simple:3.1.0'
}
applicationDefaultJvmArgs = [ "-Djdk.gtk.version=2"]
sourceCompatibility = 11
targetCompatibility = 11
test {
useJUnitPlatform()
dependsOn 'cleanTest'
testLogging {
events "passed", "skipped", "failed"
}
}
javafx {
modules = ['javafx.controls', 'javafx.fxml']
version = '11.0.2'
}
My project structure is the usual:
src/main/java/mvc/Main.java
src/test/java/mvc/MainTest.java
When I run gradle test or ./gradlew test tests it doesn't execute any tests, not sure what's wrong.
testCompile is no longer a suppored command, you should use testImplementation instead.