Difference between test and check tasks in Gradle - gradle

My build.gradle is as follows:
group 'groupName'
version 'version'
apply plugin: 'java'
apply plugin: 'idea'
sourceCompatibility = 1.8
repositories {
. . .
}
dependencies {
. . .
testCompile group: 'junit', name: 'junit', version: '4.12'
}
In Gradle when doing ./gradlew tasks I receive
Verification tasks
------------------
check - Runs all checks.
test - Runs the unit tests.
What is the difference between these two tasks? The output of ./gradlew check is identical to ./gradlew test.
andrewgazelka $ ./gradlew check
> Task :test FAILED
MathTest > testX FAILED
java.lang.AssertionError at MathTest.java:40
MathTest > testY FAILED
java.lang.AssertionError at MathTest.java:55
SimulatorTest > testZ FAILED
java.lang.IllegalArgumentException at SimulatorTest.java:71
30 tests completed, 3 failed
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':test'.
> There were failing tests. See the report at: file:///Users/andrewgazelka/IdeaProjects/RobotCode2018/build/reports/tests/test/index.html
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 2s
3 actionable tasks: 3 executed
andrewgazelka $ ./gradlew test
> Task :test FAILED
MathTest > testX FAILED
java.lang.AssertionError at MathTest.java:40
MathTest > testY FAILED
java.lang.AssertionError at MathTest.java:55
SimulatorTest > testZ FAILED
java.lang.IllegalArgumentException at SimulatorTest.java:71
30 tests completed, 3 failed
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':test'.
> There were failing tests. See the report at: file:///Users/andrewgazelka/IdeaProjects/RobotCode2018/build/reports/tests/test/index.html
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 1s
3 actionable tasks: 1 executed, 2 up-to-date
From what I understand, ./gradle test ⊆ ./gradle check. Is this correct?

The Gradle check task depends on the test task which means test is executed before check is run. The documentation states that check performs all verification tasks in the project, including test and also tasks plugins add to the project:
If you for example add the checkstyle plugin to your project you can either run its tasks checkstyleMain and checkstyleTest individually or execute a full project verification using check. In this case the tasks test, checkstyleMain and checkstyleTest would be run.
Whereas test always just executes your unit tests.

Related

Could not resolve all files for configuration `_classStructurekaptKotlin` caused by Execution failed for `StructureTransformAction`

Gradle multi-module project build fails with an unclear error. I run this command:
gradle :module:processor:integrationTest
(module:processor depends on module:processor-core, integrationTest is a custom Gradle task for running tests. I'm using kapt plugin as an annotation processor for Spring Boot configuration properties)
And I get this result:
> Task :module:processor-core:kaptGenerateStubsKotlin UP-TO-DATE
> Task :module:processor-core:kaptKotlin UP-TO-DATE
...
> Task :module:processor-core:jar SKIPPED
> Task :module:processor:kaptGenerateStubsKotlin
> Task :module:processor:kaptKotlin FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':module:processor:kaptKotlin'.
> Could not resolve all files for configuration ':module:processor:_classStructurekaptKotlin'.
> Failed to transform processor-core-SNAPSHOT.jar to match attributes {artifactType=class-structure, org.gradle.libraryelements=jar, org.gradle.usage=java-runtime}.
> Execution failed for StructureTransformAction: D:\dev\backend-project\module\processor-core\build\libs\processor-core-SNAPSHOT.jar.
> D:\dev\backend-project\module\processor-core\build\libs\processor-core-SNAPSHOT.jar (The system cannot find the path specified)
I don't understand why.
it was fixed by adding the following code to the build.gradle of processor-core module:
jar {
archiveBaseName = 'processor-core'
}
without these lines, I guess this step is disabled by Spring Boot, so the JAR file was not produced, and this led to the error.

Cannot run cucumber JUnit tests on Gradle

I am trying to run a Cucumber / Selenium project using IntelliJ Community edition and Gradle 5.5.1.
My folder structure is as follows:
ProjectRoot
|
src---main---java
|
src---test---java---packagename---stepdefinitions---Steps.java
|
-----resources---feature---application.feature
My TestRunner class is as follows:
#RunWith(Cucumber.class)
#CucumberOptions(
plugin = {"pretty", "json:cucumber-report.json"},
features = {"src/test/resources/feature"})
public class TestRunner {
}
When I try to run the TestRunner what I get is the following:
Testing started at 18:48 ...
> Task :cleanTest
> Task :compileJava UP-TO-DATE
> Task :processResources NO-SOURCE
> Task :classes UP-TO-DATE
> Task :compileTestJava
> Task :processTestResources UP-TO-DATE
> Task :testClasses
> Task :test FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':test'.
> No tests found for given includes: [org.fifthgen.scanmaltatesting.TestRunner](filter.includeTestsMatching)
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 1s
5 actionable tasks: 3 executed, 2 up-to-date
This is my build.gradle:
plugins {
id 'java'
}
group 'org.fifthgen'
version '1'
sourceCompatibility = 11
targetCompatibility = 11
repositories {
mavenCentral()
}
wrapper.gradleVersion = '5.5.1'
def cucumberVersion = '4.7.2'
def junitVersion = '5.5.2'
dependencies {
implementation 'org.seleniumhq.selenium:selenium-java:3.141.59'
testImplementation "io.cucumber:cucumber-java:${cucumberVersion}"
testImplementation "io.cucumber:cucumber-junit:${cucumberVersion}"
testImplementation "org.junit.jupiter:junit-jupiter-api:${junitVersion}"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${junitVersion}"
}
test {
useJUnitPlatform()
scanForTestClasses = false
}
When I run the test task using Gradle, I get
BUILD SUCCESSFUL in 0s
4 actionable tasks: 1 executed, 3 up-to-date
18:52:41: Task execution finished 'test'.
Without the Cucumber scenarios being run.
ˋRunWithˋ is a JUnit 4 mechanism. In order to use that with JUnit 5 platform mechanism you must include dependency on junit-vintage engine which allows running JUnit 4 tests on JUnit 5.
Alternatively you could change to the Cucumber engine for JUnit 5. I’m not sure though if it has already been released.
From the docs,
Cucumber is based on JUnit 4. If you’re using JUnit 5, remember to include junit-vintage-engine dependency, as well. For more information, please refer to JUnit 5 documentation.
Adding following dependencies made it work on IntelliJ:
testRuntimeOnly("org.junit.platform:junit-platform-launcher:1.7.2")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.7.2")
testRuntimeOnly("org.junit.vintage:junit-vintage-engine:5.7.2")
Check here for more details

gradle dependencyUpdates failure

I have "installed" or included ben manes - gradle versions plugin:
https://github.com/ben-manes/gradle-versions-plugin
The code below is the build.gradle (Project: rewards)
apply plugin: "com.github.ben-manes.versions"
buildscript {
ext.kotlin_version = '1.3.10'
repositories {
google()
jcenter()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "com.github.ben-manes:gradle-versions-plugin:0.20.0"
}
}
When I try to run the following gradle Task: gradle dependencyUpdates.
I get the following :
FAILURE: Build failed with an exception.
What went wrong:
Task 'gradle' not found in project ':app'. Some candidates are: 'bundle'.
Try:
Run gradle tasks to get a list of available tasks. Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
Get more help at https://help.gradle.org
BUILD FAILED in 0s
11:23:53: Tasks execution finished 'gradle dependencyUpdates'.
After further testing, I realised that the task that I need to run should not have the word gradle in front. The command line instruction should just be dependencyUpdates. Then click on okay and it will export the report.

Gradle build comes up with java.lang.NoClassDefFoundError

In a project I am using gradle, Java11, SpringBoot, Junit5. The application is launching without any problem and when I run the unit tests individually everything is fine.
However, when I run gradle->build task the following error appears:
Task :test FAILED
JwtTokenProviderTest > initializationError FAILED
java.lang.NoClassDefFoundError
Caused by: java.lang.ClassNotFoundException
1 test completed, 1 failed
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':test'.
More ever, I tried the following config in build.gradle
tasks.withType(Test) {
scanForTestClasses = false
include "**/*Test.class" // whatever Ant pattern matches your test class files
}
But it does not fix the problem.
Any idea?
Thanks

How to exclude classes in findbugs gradle plugin without using XML

I am trying to exclude few classes from findbugs analysis in a gradle setup
apply plugin: 'application'
apply plugin: 'findbugs'
....
tasks.withType(FindBugs) {
def excludeProtobufFiles = 'com/turn/protobufkafka/'
classes = classes.filter {
!it.path.contains(excludeProtobufFiles)
}
// Generate HTML output for findbugs
reports {
xml.enabled = false
html.enabled = true
}
}
With this change gradle build works fine but gradle check fails:
:findbugsMain FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':findbugsMain'.
> No classes configured for FindBugs analysis.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 1.58 secs
gradle check works, however, if I run gradle build first. Is there a way to exclude files in gradle for findbugs such that gradle check works without failure.
Without the excluded classes both gradle check and gradle build works without any issues.

Resources