I am using Serenity - JBehave framework. After creation of sample script, I am able to execute Junit runner class from eClipse however when I am trying to execute any of the below command from command prompt it is giving me error.
$gradle clean test aggregate
$gradle clean test
$gradle clean build
The error message is same in all cases, as below:
org.gradle.TestRunnerClass > initializationError FAILED
java.lang.RuntimeException
Caused by: java.lang.RuntimeException
Caused by: java.lang.IllegalArgumentException
Caused by: java.lang.ClassNotFoundException
1 test completed, 1 failed
:test FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':test'.
> There were failing tests. See the report at: file:///C:/$ /build/reports/tests/index.html
Below are the details:
Test Runner class:
package org.gradle;
import net.serenitybdd.jbehave.SerenityStories;
public class TestRunnerClass extends SerenityStories{}
Sample Step Definition class:
package org.gradle.stepDef;
import net.thucydides.core.annotations.Step;
import net.thucydides.core.annotations.Steps;
import org.jbehave.core.annotations.Given;
import org.jbehave.core.annotations.Then;
import org.jbehave.core.annotations.When;
public class StepDefSticky {
#Given("User is on Sticky note home page")
public void givenUserIsOnStickyNoteHomePage() {
System.out.println("I am in Given");
}
#When("User clicks on Add Note button")
public void whenUserClicksOnAddNoteButton() {
System.out.println("I am in When");
}
#Then("Sticky note pop up should get open")
public void thenStickyNotePopUpShouldGetOpen() {
System.out.println("I am in Then");
}
}
Please see the package structure carefully.
Below is the build.gradle I am using
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'net.serenity-bdd.aggregator'
apply plugin: 'com.jfrog.bintray'
sourceCompatibility = 1.8
version = '1.0'
def poiVersion = "3.10.1"
repositories {
maven { url "repoUrl" }
}
buildscript {
repositories {
maven { url "repoURL" }
}
dependencies {
classpath("net.serenity-bdd:serenity-gradle-plugin:1.0.47")
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:0.6'
classpath 'org.ajoberstar:gradle-git:0.12.0'
}
}
ext {
bintrayBaseUrl = 'https://api.bintray.com/maven'
bintrayRepository = 'maven'
bintrayPackage = 'serenity-cucumber'
projectDescription = 'Serenity Cucumber integration'
if (!project.hasProperty("bintrayUsername")) {
bintrayUsername = 'wakaleo'
}
if (!project.hasProperty("bintrayApiKey")) {
bintrayApiKey = ''
}
serenityCoreVersion = '1.0.47'
cucumberJVMVersion = '1.2.2'
}
dependencies {
testCompile('junit:junit:4.11')
testCompile('org.assertj:assertj-core:1.7.0')
testCompile('org.slf4j:slf4j-simple:1.7.7')
//JBehave jar files
testCompile 'net.serenity-bdd:core:1.0.47'
testCompile 'net.serenity-bdd:serenity-jbehave:1.0.21'
testCompile 'net.serenity-bdd:serenity-junit:1.0.47'
// Apache POI plugin for excel read
compile "org.apache.poi:poi:${poiVersion}"
compile "org.apache.poi:poi-ooxml:${poiVersion}"
compile "org.apache.poi:ooxml-schemas:1.1"
}
gradle.startParameter.continueOnFailure = true
uploadArchives {
repositories { flatDir { dirs 'repos' } }
}
task wrapper(type: Wrapper) { gradleVersion = '2.3' }
I have stored the .story file under the src/test/resources package.
Please help me to understand where I am making mistake. Thanks for your help on this.
Enable standard out and standard error in your build.gradle file:
test {
testLogging {
showStandardStreams = true
}
}
And to make sure all your stories run, add a TestSuite class:
#RunWith(Suite.class)
#SuiteClasses({ Story1.class, Story2.class})
public class TestSuite { }
Note: Story1 & Story2 are the names of the test runners to match a JBehave Gherkin files named Story1.story & Story2.story & step files names Story1Steps.java & Story2Steps.java according to Serenity naming conventions.
Related
When running gradle test I receive an error
Caused by: java.lang.IllegalArgumentException: Received a failure event for test with unknown id '16.3'. Registered test ids: '[16.1, 16.2, :app:test]
This error only occurs when tests include functionality added by system-rules. For example
#Test
void sysOutTest() {
System.out.print("hello world");
assertEquals("hello world", systemOutRule.getLog());
}
My build.gradle file looks like this:
plugins {
id 'application'
id 'jacoco'
}
repositories {
jcenter()
}
dependencies {
testImplementation 'com.github.stefanbirkner:system-rules:1.19.0'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.1.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.1.0'
testRuntimeOnly 'org.junit.vintage:junit-vintage-engine:5.1.0'
}
application {
// Define the main class for the application.
mainClass = 'package.App'
}
test {
useJUnitPlatform()
//scanForTestClasses false (This was in here and is commented out as originaly gradle was not detecting tests that were in the style of Junit4)
finalizedBy jacocoTestReport
}
run{
standardInput = System.in
}
jacocoTestReport {
reports {
html.enabled = true
csv.enabled = true
}
}
sourceSets {
test {
java {
srcDir 'test'
}
}
main {
java {
srcDir 'main'
}
}
}
To be clear, tests work fine when the file has no tests that use SystemRules.
I've (stupidly) already written all my tests using SystemRules, so I would prefer to find a way to make it work instead of starting from scratch.
Thanks for any help
I just needed to put public before my tests. All the other tests that didn't use System rules had public.
#Test
public void sysOutTest() {
System.out.print("hello world");
assertEquals("hello world", systemOutRule.getLog());
}
I am trying to migrate tasks from build.gradle to a plugin that do it.
In my build.gradle I do this:
plugins {
//id 'java'
id 'war'
//https://plugins.gradle.org/plugin/org.gretty
id 'org.gretty' version '3.0.1'
id "com.github.dkorotych.gradle-maven-exec" version "2.2.1"
}
apply plugin: 'maven'
....
....
prepareFrontEnd (type: MavenExec, dependsOn: build) {
goals 'vaadin:prepare-frontend'
}
task buildFrontEnd (type: MavenExec, dependsOn: prepareFrontEnd) {
goals 'vaadin:build-frontend'
}
Now I am moving this stuff to a plugins:
package com.github.mdre.hgvaadinplugin
import org.gradle.api.Plugin;
import org.gradle.api.Project;
//import com.github.dkorotych.gradle.maven.exec.MavenExec;
class HGVaadinPlugin implements Plugin<Project> {
#Override
void apply(Project project) {
println "Hybrid Gradle Vaaadin plugin."
project.plugins.apply('com.github.dkorotych.gradle-maven-exec')
// project.getPluginManager().apply('gradle-maven-exec-plugin')
project.task('prepareFrontEnd', type: MavenExec){
dependsOn build
doLast {
goal 'vaadin:prepare-frontend'
}
}
}
}
If I try to import the class MavenExec I get this error:
> Task :compileGroovy FAILED
startup failed:
/home/mdre/Proyectos/HGVaadinPlugin/src/main/groovy/com/github/mdre/hgvaadinplugin/HGVaadinPlugin.groovy: 5: unable to resolve class com.github.dkorotych.gradle.maven.exec.MavenExec
# line 5, column 1.
import com.github.dkorotych.gradle.maven.exec.MavenExec;
^
and if I comment the import line, I get this error in the project that use the plugin:
What went wrong:
A problem occurred evaluating root project 'VaadinFlowLab'.
Failed to apply plugin [id 'com.github.mdre.hgvaadinplugin']
No such property: MavenExec for class: com.github.mdre.hgvaadinplugin.HGVaadinPlugin
How could I do this?
Thanks.
Well, I fix it!
I forget to include the dependency class in the final jar. So all of I need was to add this to the build.config:
configurations {
// configuration that holds jars to include in the jar
extraLibs
}
dependencies {
....
....
//necesario para crear un fatJar
extraLibs "gradle.plugin.com.github.dkorotych.gradle.maven.exec:gradle-maven-exec-plugin:2.2.1"
configurations.compile.extendsFrom(configurations.extraLibs)
}
jar {
from {
configurations.extraLibs.collect { it.isDirectory() ? it : zipTree(it) }
}
}
and now it is working!!
I'm trying to configure Jacoco to exclude some classes from analysis but can't find any working example :(
I found some samples with afterEvaluate but no success
src/main/java/org/example/A.java:
package org.example;
class A {
}
src/main/java/org/example/B.java:
package org.example;
class B {
}
src/test/java/org/example/ExampleTest.java:
package org.example;
public class ExampleTest {
#org.junit.Test
public void test() {
new A();
new B();
}
}
build.gradle.kts:
plugins {
java
jacoco
}
repositories {
mavenCentral()
}
dependencies {
testCompile("junit:junit:4.12")
}
using Gradle 5.4.1 execution of gradle test jacocoTestReport produces following report
after addition to build.gradle.kts
tasks.withType<JacocoReport> {
classDirectories.setFrom(
sourceSets.main.get().output.asFileTree.matching {
exclude("org/example/B.class")
}
)
}
execution of the same command produces following report
Just to add to #Godin's awesome answer:
The way #Godin explained it, you would have to run gradle test jacocoTestReport which isn't bad but If you want jacoco to run when you run just with gradle test add this to your build.gralde.kts:
tasks.test {
finalizedBy("jacocoTestReport")
doLast {
println("View code coverage at:")
println("file://$buildDir/reports/jacoco/test/html/index.html")
}
}
I've managed to exclude this way:
tasks.jacocoTestReport {
classDirectories.setFrom(
files(classDirectories.files.map {
fileTree(it) {
exclude(
"com/example/integration/**",
"com/example/application/*Ext*"
)
}
})
)
}
Taken from here
I have a simple code which runs with TestNG, but I am unable to run the same with Gradle, as it says no main method is found, which is, well, not surprising since I am using annotations.
But in such a scenario, how to run the code if I must use Gradle.
Kindly note, I am very new to Gradle, and do not harbour much knowledge about the same.
Code:
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class tryTestNG
{
#BeforeClass
public void setup()
{
System.out.println("I am in Setup");
}
#Test
public void test()
{
System.out.println("I am in Test");
}
#AfterClass
public void tearDown()
{
System.out.println("I am in tearDown");
}
}
The above code runs perfectly with TestNG Library. However not with Gradle.
Here is my Gradle Build setup:
apply plugin: 'java'
apply plugin: 'application'
mainClassName = 'tryTestNG'
sourceCompatibility = 1.7
targetCompatibility = 1.7
version = '1.0'
repositories {
mavenCentral()
}
test {
useTestNG()
}
dependencies {
compile group: 'org.testng', name: 'testng', version: '6.9.10'
}
The Gradle returns that there is no Main Method.
Working Directory: /home/avirup/MyWorkspace/JavaWorkspace/TestNGGradle
Gradle User Home: /home/avirup/.gradle
Gradle Distribution: Gradle wrapper from target build
Gradle Version: 2.9
Java Home: /usr/lib/jvm/java-8-oracle
JVM Arguments: None
Program Arguments: None
Gradle Tasks: run
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:runError: Main method not found in class tryTestNG, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':run'.
> Process 'command '/usr/lib/jvm/java-8-oracle/bin/java'' finished with non-zero exit value 1
* 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: 0.514 secs
Thanks for your help.
There's no need to use application plugin to run tests.
The build.gradle should be:
apply plugin: 'java'
sourceCompatibility = 1.7
targetCompatibility = 1.7
version = '1.0'
repositories {
mavenCentral()
}
test {
useTestNG()
}
dependencies {
compile group: 'org.testng', name: 'testng', version: '6.9.10'
}
And the command: gradle test. Also, put tryTestNG under src/test/java and name it with capital letter.
Here is a demo.
Also mind that println statements from tests won't be visible in console. To view them navigate to test report. It's under: <project_dir>/build/reports/tests/index.html.
I'm trying to encapsulate android plugin in my own plugin, but when I'm trying to apply my plugin build fails with an exception:
A problem occurred evaluating root project 'myproj'.
> Failed to apply plugin [id 'com.mycomp.build']
> Failed to apply plugin [id 'android-library']
> Plugin with id 'android-library' not found.
Here is how I'm applying android plugin inside my own plugin's implementation:
// build.gradle
apply plugin: 'groovy'
version = '1.0'
group = 'com.mycomp'
dependencies {
compile gradleApi()
compile localGroovy()
}
// Build.groovy
package com.mycomp
import org.gradle.api.Plugin
import org.gradle.api.Project
class Build implements Plugin<Project> {
void apply(Project project) {
println 'Hello from com.mycomp.Build'
project.beforeEvaluate {
buildscript.configurations.classpath +=
'com.android.tools.build:gradle:1.0.0-rc1'
}
project.configure(project) {
buildscript.repositories.mavenCentral()
apply plugin: 'android-library'
}
}
}
For some reason a classpath is not being properly loaded, what am I doing wrong?
I guess that at the time you'd like to add the plugin dependencies for the build script have been already resolved, thus it won't work that way. You need to specify the plugin You'd like to apply as a script dependency itself.
It will work that way:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0-rc1'
}
}
apply plugin: 'groovy'
apply plugin: Build
version = '1.0'
group = 'com.mycomp'
dependencies {
compile gradleApi()
compile localGroovy()
}
import org.gradle.api.Plugin
import org.gradle.api.Project
class Build implements Plugin<Project> {
void apply(Project project) {
project.configure(project) {
apply plugin: 'android-library'
}
}
}
Now, android-plugin is found but it fails because of the fact that groovy plugin had been applied earlier and there's a conflict.
Use the project's PluginManager. For example, the war plugin pulls in the java plugin like this:
public class WarPlugin implements Plugin<Project> {
// ...
public void apply(final Project project) {
project.getPluginManager().apply(org.gradle.api.plugins.JavaPlugin.class);
// ...
}
// ...
}