Need build.gradle with (CucumberJVM4 + Allure) for BDD Cucumber using java - java

I have a gradle project and I am trying to generate the Allure Reports using CucumberJVM 4. I have referred many blogs and their websites but no luck. Can anyone help me with sample GitHub Gradle project with BDD Cucumber using java along with Allure Reports + CucumberJVM ?
I've tried to follow the procedure as per this documentation but '.json' files and my step definitions are not getting executed in the gradle command.
Link: https://docs.qameta.io/allure/#_cucumber_jvm
Command : gradle clean allure or gradle clean allureReport
Below are the changes which i have in my build.gradle:
buildscript {
repositories {
jcenter()
mavenCentral()
mavenLocal()
}
dependencies{
classpath "io.qameta.allure:allure-gradle:2.8.1"
}
}
apply plugin: "io.qameta.allure"
allure {
version = '2.2.1'
autoconfigure = true
String allureJavaVersion = '2.0-BETA9'
aspectjweaver = true
boolean clean = true
resultsDir = file('testfolder/build/allure-results')
reportDir = file('build/reports/allure-report')
String configuration = 'testCompile'
useCucumberJVM {
version = '2.0-BETA12'
}
downloadLink = "https://dl.bintray.com/qameta/generic/io/qameta/allure/allure/2.2.1/allure-2.2.1.zip"
}
dependencies{
// Allure Reports
testCompile "io.qameta.allure:allure-cucumber4-jvm:2.10.0"
testCompile "io.qameta.allure:allure-cucumber-jvm:2.0-BETA16"
testCompile ("org.aspectj:aspectjweaver:1.8.10")
}
import io.qameta.allure.gradle.task.AllureReport
task allureAggregatedReport(type: AllureReport) {
resultsDirs = subprojects.allure.resultsDir
}

Here you go!! - https://github.com/Ragul-Dhandapani/BDD-Cucumber-Allure-Gradle
This project has build.grade (Cucumber JVM4 + Java + Allure + BDD Cucumber). Make sure your cucumber version <5.0. Because Allure Cucumber JVM 4 will not support the higher version of cucumber.

Well, to configure allure 2.0 on cradle you should to do 3 things add dependencies (also compile group), and allure module.
And it works.
After that json files will be configured in result directory. And you can configure allure report folder by allure CLI
See my work configuration
dependencies {
compile "org.seleniumhq.selenium:selenium-java:${selenium}"
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile 'info.cukes:cucumber-java:1.2.5'
compile 'info.cukes:cucumber-junit:1.2.5'
compile "org.testng:testng:${testNG}"
compile group: 'io.qameta.allure', name: 'allure-attachments', version: '2.0-BETA16'
}
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "io.qameta.allure:allure-gradle:2.3"
}
}
apply plugin: 'io.qameta.allure'
allure {
version = '2.4.1'
useCucumberJVM {
version = '2.0-BETA5'
}

Related

How to create a Dropwizard project with gradle?

I am on a Linux machine with Idea IntelliJ and I would like to create a Dropwizard project with gradle. With maven archetypes this job would be very easy.
mvn archetype:generate
-DarchetypeGroupId=io.dropwizard.archetypes
-DarchetypeArtifactId=java-simple
-DarchetypeVersion=[REPLACE ME WITH A VALID DROPWIZARD VERSION]
For gradle I am struggling to get going. As I checked out other post I was wondering if what I did was correct:
// Needed Instead of the Shade plug in
plugins {
id 'com.github.johnrengelman.shadow' version '2.0.1'
}
version '1.0-SNAPSHOT'
group 'org.example.sampleName'
description 'Sample Dropwizard REST '
apply plugin: 'java'
apply plugin: 'application'
repositories {
mavenCentral()
}
dependencies {
compile 'io.dropwizard:dropwizard-core:1.2.0'
testCompile 'junit:junit:4.12'
}
mainClassName = 'org.example.sampleName.SampleApplication'
run {
args = ['server', 'config.yml']
}
shadowJar {
mergeServiceFiles()
exclude 'META-INF/*.DSA', 'META-INF/*.RSA', 'META-INF/*.SF'
}
jar {
manifest {
attributes 'Main-Class': mainClassName
}
}
Then I just run gradle shadowJar and then run java -jar build/SampleApplication.jar server config.yml. Is this correct? And is the line testCompile 'junit:junit:4.12' enough for the unit test?
You could use this Yeoman generator:
https://www.npmjs.com/package/generator-dropwizard-gradle
If you have npm installed.
The line testCompile 'junit:junit:4.12' is sufficient, yes.
You can also search on github for examples, like this one:
https://github.com/kishaningithub/dropwizard-gradle

How to use jUnit 5 with Gradle

Running the IDEA IDE I want to add a gradle dependency for the jUnit v5.
Here is my build.gradle file, I used this answer as a guide.
apply plugin: 'java'
sourceCompatibility = 1.8
repositories { mavenCentral() }
apply plugin: 'org.junit.platform.gradle.plugin'
dependencies {
testCompile 'junit:junit:4.12'
compile 'junit:junit:4.12'
testRuntime("org.junit.vintage:junit-vintage-engine:4.12.0-M4")
testCompile("org.junit.jupiter:junit-jupiter-api:5.0.0-M4")
testRuntime("org.junit.jupiter:junit-jupiter-engine:5.0.0-M4")
// Enable use of the JUnitPlatform Runner within the IDE
testCompile("org.junit.platform:junit-platform-runner:1.0.0-M4")
compile ("org.junit.jupiter:junit-jupiter-api:5.0.0-M4")
}
sourceSets {
main {
java {
srcDir 'src'
}
}
}
junitPlatform {
details 'tree'
}
The problem here is that the jUnit4 annotations are resolved by import but all the v5 annotations are not resolved.
One example:
#ParameterizedTest
public void testExample() {
// My annotations is not resolved
}
What is the right way to add a jUnit5 dependency using gradle?
EDIT
I started a new gradle java project from scratch to get to the bottom of this.
Here is my current build.gradle.
group 'com.iay0361'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.8
repositories { mavenCentral() }
apply plugin: 'org.junit.platform.gradle.plugin'
dependencies {
testCompile group: 'org.junit.vintage', name: 'junit-vintage-engine', version: '4.12.0-RC3'
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.0.0-RC3'
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.0.0-RC3'
compile group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: '5.0.0-RC3'
testCompile group: 'org.junit.platform', name: 'junit-platform-runner', version: '1.0.0-RC3'
}
sourceSets {
main {
java {
srcDir 'src'
}
}
}
junitPlatform {
details 'tree'
}
I wrote the #Test annotation in a new class file under test after which it asked me to "add the 'jUnit5' to classpath
which I did and this time selected the Copy 'jUnit5' libraries to... instead of using the IDEA distributor.
Now it added these files in module:
The file is still RC2 but in build.gradle it is RC3.
There are also no jUnit jars in "External Library" directory
What am I missing, the problem is still that the IDE cannot resolve some jUnit5 annotations like #ParamiterizedTest.
Here is a quick sample on how to configure gradle with junit5. In your dependencies, remove the compile statement for the junit:4.12 artifact verison.
// If you also want to support JUnit 3 and JUnit 4 tests
testCompile("junit:junit:4.12")
In the buildscript() method, include these:
buildscript {
repositories { mavenCentral() }
dependencies { classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-RC3' }
}

Running Android unit tests with Spock framework

I am using:
Android Studio 2.1.3
Gradle 2.14.1 (I tried with 2.14 also)
OpenJDK version "1.8.0_91"
I want to write some Unit tests with Groovy and Spock for sample Android application.
I have already read about RoboSpock.
When I am trying to run simple test:
package a.b.regex
class TestSum extends spock.lang.Specification {
def "test adding some numbers"() {
when:
def a = 5 + 4
then:
a == 9
}
}
When I try to run this test in Android Studio I have an error:
Process finished with exit code 1
Class not found: "a.b.regex.TestSum"Empty test suite.
Configurations that I used:
1)
buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:2.1.3'
classpath 'org.codehaus.groovy:gradle-groovy-android-plugin:0.3.6'
}
}
apply plugin: 'groovyx.grooid.groovy-android'
// ...
dependencies {
testCompile 'org.robospock:robospock:1.0.0'
}
2)
buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:2.1.3'
classpath 'org.codehaus.groovy:groovy-android-gradle-plugin:1.0.0'
}
}
apply plugin: 'groovyx.android'
dependencies {
testCompile "org.codehaus.groovy:groovy-all:2.4.1"
testCompile "org.spockframework:spock-core:1.0-groovy-2.4"
testCompile 'org.codehaus.groovy:groovy:2.4.6:grooid'
}
From the console no tests are run at all.
With testing Java applications I have no problem.
Here is the project code where I want to use Spock: GitHub repository
Thankfully to Pieces I found the answer.
You should use the following configuration:
apply plugin: 'groovyx.android'
buildscript {
repositories {
jcenter() // or mavenCentral, etc.
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.3'
classpath 'org.codehaus.groovy:groovy-android-gradle-plugin:1.0.0'
}
}
testCompile 'org.codehaus.groovy:groovy:2.4.7:grooid'
testCompile('org.spockframework:spock-core:1.0-groovy-2.4') {
exclude group: 'org.codehaus.groovy'
exclude group: 'junit'
}
1)
This should work great other than you are using an outdated version of the groovy android plugin. The current version is 1.0.0. The error you are seeing is that you included your tests in androidTest source folder, when they should be included in the test source folder.
2)
You do not want groovy-all, and want to exclude that from the spock transitive dependencies as well.
This would look similar to
dependencies {
testCompile 'org.codehaus.groovy:groovy:2.4.7:grooid'
testCompile('org.spockframework:spock-core:1.0-groovy-2.4') {
exclude group: 'org.codehaus.groovy'
exclude group: 'junit'
}
}
Same as the problem with #1 you probably have the source under androidTest folder instead of the test folder.
The androidTest folder is for test that will run on the device, and the test folder if for tests that will run on your machines JVM.
If you reached here trying to configure for gradle 6.6 this will help you:
I stepped multiple times into this while trying to configure Spock in android having gradle 6.6, there has been multiple changes in gradle, so they made this plugin deprecated 'groovyx.android': https://github.com/groovy/groovy-android-gradle-plugin
Deprecated: This plugin has been deprecated in favor of Kotlin which has the full support of JetBrains and Google. The changes that go into each Android Plugin Version make it really hard for this plugin to keep up. As of Gradle 6.0 this plugin does not work.
Found out that gradle have a separate plugin for groovy support:
https://plugins.gradle.org/plugin/org.codehaus.groovy.android
This is the configuration for gradle 6.6 using groovy DSL for gradle
buildscript {
ext.kotlin_version = "1.3.72"
repositories {
google()
jcenter()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "com.android.tools.build:gradle:4.0.1"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "gradle.plugin.org.codehaus.groovy:groovy-android-gradle-plugin:3.0.0"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
And then in the app configuration:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: "org.codehaus.groovy.android"
def groovyVersion = "3.0.5"
def spockVersion = "2.0-M3-groovy-3.0"
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.1'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0'
testImplementation 'junit:junit:4.13'
testImplementation("org.codehaus.groovy:groovy:${groovyVersion}")
testImplementation("org.codehaus.groovy:groovy-all:${groovyVersion}")
testImplementation("org.spockframework:spock-core:${spockVersion}")
testImplementation("org.spockframework:spock-spring:${spockVersion}")
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

How to get gradle and cucumber working together?

Getting gradle to work with cucumber cleanly is something of a challenge. I want to get gradle build to compile and run the tests, but so far I've had no success.
build.gradle
plugins {
id "com.github.samueltbrown.cucumber" version "0.9"
}
apply plugin: 'java'
apply plugin: 'idea'
def JAVA_WEBSOCKET_VERSION = '1.2.1'
def CUCUMBER_VERSION = '1.2.4'
jar {
manifest {
attributes 'Implementation-Title': 'Java-WebSocket',
'Implementation-Version': JAVA_WEBSOCKET_VERSION
}
}
repositories {
jcenter()
}
dependencies {
testCompile "info.cukes:cucumber-java:$CUCUMBER_VERSION"
testCompile "info.cukes:cucumber-junit:$CUCUMBER_VERSION"
testCompile 'junit:junit:4.+'
}
task wrapper(type: Wrapper) {
gradleVersion = '2.9'
}
Currently I get many errors about the annotations (#Given, #Then, #After) that cucumber uses. What I want is to build the project cleanly without using JavaExec. Is this possible or is there a specific limitation to either gradle or cucumber that prevents this?
dependencies {
testCompile 'info.cukes:cucumber-jvm:1+'
testCompile 'info.cukes:cucumber-jvm-deps:1+'
testCompile 'info.cukes:cucumber-java:1+'
testCompile 'info.cukes:cucumber-junit:1+'
testCompile 'info.cukes:cucumber-core:1+'
}
I created another function to execute test
test {
ignoreFailures = true
// show standard out and standard error of the test JVM(s) on the console
testLogging.showStandardStreams = true
// set heap size for the test JVM(s)
minHeapSize = "128m"
maxHeapSize = "512m"
// set JVM arguments for the test JVM(s)
jvmArgs '-XX:MaxPermSize=256m'
// listen to events in the test execution lifecycle
beforeTest { descriptor ->
logger.lifecycle("Running test: " + descriptor)
}
// explicitly include or exclude tests( Add Package directly)
exclude "com/**/***/rest/junit**"
exclude "com/**/***/db/junit**"
reports.junitXml.enabled = false
reports.html.enabled = false
}
now Call this function from command line for test execution
task "forceTest" {
dependsOn "clean", "cleanTest", "test"
}
Please use the below gradle cucumber plugin in your build.gradle file
plugins {
id 'java'
id "com.github.samueltbrown.cucumber" version "0.9"
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
compile 'org.codehaus.groovy:groovy:2.4.7'
cucumberCompile 'info.cukes:cucumber-groovy:1.2.2'
}
Running gradle cucumber in the terminal will get you started

"Coverage information was not collected." when running 'gradle sonarRunner' after generating Jacoco reports

I've started with the 'java-gradle-simple' example from the Sonar GitHub repo:
https://github.com/SonarSource/sonar-examples/tree/master/projects/languages/java/gradle/java-gradle-simple
I've added the jacoco plugin to my build.gradle file in an empty to see unit test coverage results, and I'm seeing the warning:
Coverage information was not collected. Perhaps you forget to include
debug information into compiled classes?
I'm currently running version 4.3.2 of Sonar, and this is what I'm seeing after running 'gradle test jacocoTestReport sonarRunner':
My build.gradle file looks like this:
apply plugin: 'java'
apply plugin: 'sonar-runner'
apply plugin: 'jacoco'
allprojects {
ext.baseVersion = "0.1"
ext.snapshotVersion = true
group = "org.sonar.tests"
version = "$baseVersion" + (snapshotVersion ? "-SNAPSHOT" : "")
}
sonarRunner {
sonarProperties {
property "sonar.projectName", "Simple Java Gradle Project"
property "sonar.projectKey", "org.codehaus.sonar:example-java-gradle"
property "sonar.jacoco.reportPath", "${project.buildDir}/jacoco/test.exec"
property "sonar.binaries", "${project.buildDir}/classes"
property "sonar.sources", "src/main"
property "sonar.tests", "src/test"
property "sonar.java.coveragePlugin", "jacoco"
}
}
buildscript {
repositories { mavenCentral() }
dependencies { classpath 'org.ajoberstar:gradle-jacoco:0.1.0' }
}
test {
ignoreFailures = true
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.10'
}
repositories {
mavenCentral()
}
Is there some other configuration that I'm missing? Surprisingly, there doesn't seem to be much information on generating unit test coverage with Sonar using Gradle.
Since you are using jdk 8, you should add the following block to your gradle script:
jacoco {
toolVersion = "0.7.0.201403182114"
}
Also, you should make sure your sonar version supports jdk8

Categories