## Where communities thrive

• Join over 1.5M+ people
• Join over 100K+ communities
• Free without limits
##### Activity
• Dec 08 09:38
leonard84 commented #138
• Dec 07 21:19

leonard84 on master

Add single space into CannotCre… (compare)

• Dec 07 21:19
leonard84 closed #1402
• Dec 07 21:19
codecov[bot] commented #1402
• Dec 07 21:16
leonard84 commented #1402
• Dec 07 21:16
leonard84 auto_merge_enabled #1402
• Dec 07 20:34
chochos commented #138
• Dec 07 15:43
T45K synchronize #1402
• Dec 07 14:01
T45K opened #1402
• Dec 05 22:39
codecov[bot] commented #1382
• Dec 05 22:38
codecov[bot] commented #1382
• Dec 05 22:38
codecov[bot] commented #1382
• Dec 05 22:38
codecov[bot] commented #1382
• Dec 05 22:37
codecov[bot] commented #1382
• Dec 05 22:37
codecov[bot] commented #1382
• Dec 05 22:33
codecov[bot] commented #1382
• Dec 05 22:33
leonard84 synchronize #1382
• Dec 05 11:58
codecov[bot] commented #1382
• Dec 05 11:57
codecov[bot] commented #1382
• Dec 05 11:57
codecov[bot] commented #1382
Nick Bensema
@unbibium
i do wish i could change the order of loops so that the 1,2,3 column is the outer loop, but this will get me most of the way
Alexander Kriegisch
@kriegaex
You should not rely on the order of features or order of iterations within a feature. But would something like this do?
[[1,2,3], [4,5,6]].combinations()
# [[1, 4], [2, 4], [3, 4], [1, 5], [2, 5], [3, 5], [1, 6], [2, 6], [3, 6]]

[[1,2,3], [4,5,6]].combinations().sort()
# [[1, 4], [1, 5], [1, 6], [2, 4], [2, 5], [2, 6], [3, 4], [3, 5], [3, 6]]
For illustration:
[3, [1], [2], 1, [1,2], [1,1], [1,2,3], [1,2], 2, [1,2,2], [3]].sort()
# [1, 2, 3, [1], [2], [3], [1, 1], [1, 2], [1, 2], [1, 2, 2], [1, 2, 3]]
Alexander Kriegisch
@kriegaex
Otherwise you can call sort with a custom comparator, if you think it is worth the effort.
woodgiraffe
@woodgiraffe
hi, I'm looking for a way to pass a list of closures that are expected to all throw the same type of exception and verify this behavior without writing feature methods for every single one, I'd welcome any pointers that can put me in the right direction
Alexander Kriegisch
@kriegaex
Did you search stackoverflow.com? I answered similar questions there before, even very recently.
Simon Temple
@simon-temple

Hi all, I have a newbie question about Script bindings... I am running a test via the EmbeddedSpecRunner()

import spock.util.EmbeddedSpecRunner
def esr = new EmbeddedSpecRunner()
esr.runClass(SpockTestClass.class)

This is my groovy Script class. Before I run my Script I bind an instance of a class called myDsl to it.
I would like to use myDsl from within SpockTestClass but cannot figure out a way to supply it via the EmbeddedSpecRunner
I have tried creating a @Singleton and referencing it from within the Specification but I'm guessing classloaders stop me seeing this externally created .instance
How can I supply a pre-built Object(s) for use within a Specification?

Alexander Kriegisch
@kriegaex
@simon-temple, maybe someone more "groovy" than I can make sense of this snippet out of context, but as far as I am concerned, I would be glad to see the full picture. Could you please provide the test and, if necessary to run it, also a class under test? I just need the tiniest versions of both which reproduce your problem, then I can look into it. I would like to understand what you mean exactly by "bind an instance". Bind how? Bind where?
Simon Temple
@simon-temple

@kriegaex, thank you for looking at this... the code snippet is an instance of the Groovy Script class which has a set/get Binding method (https://docs.groovy-lang.org/latest/html/api/groovy/lang/Script.html) Before I run the Script I add an object called myDsl via a Binding (https://docs.groovy-lang.org/latest/html/api/groovy/lang/Binding.html). For example, myDsl has a log() method so I can do this:

import spock.util.EmbeddedSpecRunner
def esr = new EmbeddedSpecRunner()
esr.runClass(SpockTestClass.class)

However I cannot access the Binding of the Script from within my SpockTestClass as I cannot find a way to pass any pre-instantiated object into a test when using the EmbeddedSpecRunner(). I think the Binding is perhaps confusing my question. A simpler question might be :

import spock.util.EmbeddedSpecRunner

def esr = new EmbeddedSpecRunner()

def myMap = [:]
myMap['hello'] = 'world'

esr.runClass(SpockTestClass.class)

The test class looks like:

import spock.lang.*

class SpockTestClass extends Specification {

def "hello must be world"() {
given:
def value = myMap['hello']

expect:
value == 'world'
}
}

The results are:

groovy.lang.MissingPropertyException: No such property: myMap for class: io.platform6.app.core.scripts.rd_spockmain.SpockTestClass
at SpockTestClass.hello must be world(SpockTestClass:10)

How can I supply a pre-built Object(s) for use within a Specification?

Alexander Kriegisch
@kriegaex

Ugh, that's ugly, depending on a binding not present in the spec itself during feature execution. But I understand what you are trying to do. I know that my recommendation not to do it like that, because the test also cannot run stand-alone like that and forever depends on the calling script, does not solve your problem. The Groovy guys like Leonard and Björn probably know if and how this can be achieved.

My recommendation would be to fetch the values you need in a where: block, maybe using a custom data provider. You can read them from a file, a database, a website or wherever.

Simon Temple
@simon-temple
Yes I take your point, relying on external side-effects is not a good unit testing approach. However, I'm trying to push the use of Spock a little further and introduce its use as a functional testing tool. To do this I'd like to execute the tests within a provided environment (with some pre instantiated objects). Perhaps I'm trying to use Spock in a way it was not designed for?
Simon Temple
@simon-temple

BTW: I found an example using Spring:

class SampleSpringSpec extends Specification {
@Autowired
private WebController webController
def "when context is loaded then controller bean is created"() {
expect: "the WebController is created"
webController != null
}
}

Maybe I need to look at developing our version of the Autowire annotation ?

Alexander Kriegisch
@kriegaex
The Spock manual has chapters on annotation-driven as well as global Spock extensions. They are quite powerful. I am not an expert extension developer, but have used both before and can help a bit, if you have any problems. I am still struggling to understand what you are trying to achieve, because you are mostly explaining how you want to do it, instead of explaining what you want to achieve. Why would using Spock as a "functional testing tool" require you to work like this, and what does the term mean for you? I think we are having an XY problem here.
Alexander Kriegisch
@kriegaex
Besides, did you try to extend EmbeddedSpecification and compile your tests on the fly from strings? That way you could easily wire in values into the source code, instead of binding variables. Maybe that is enough for your purposes, I am not sure. There are dozens of exaples in Spock's test harness, e.g. in the smoke tests section. One random example would be:
https://github.com/spockframework/spock/blob/master/spock-specs/src/test/groovy/org/spockframework/smoke/condition/DiffedObjectRendering.groovy
Ligun
@ligun

Hi, I test my library with Spock Framework.
I could built the test a few days ago, but yesterday suddenly it doesn't work normally.
gradle test returns success, but the report file is written as no test item.

buildf file is blow

plugins {
id 'groovy'
}

archivesBaseName = 'project'
group = 'group'
version = '1.0'

repositories {
mavenCentral()
}

dependencies {
implementation 'org.codehaus.groovy:groovy:3.0.8'
testImplementation 'org.spockframework:spock-core:2.0-groovy-3.0'
}

I haven't changed anything.
It used to work fine.

Ligun
@ligun

I resolved it.
I forgot that I changed spock version to 2.0.
It seems to add the setting test { userJUnitPlatform() } to build.gradle.
After changed, it succeed to discover the test under test directory.

Do I have to add this settings when I use Spock 2.0?

Alexander Kriegisch
@kriegaex

@ligun, the Spock manual's Getting started section links to an example project which contains both Maven and Gradle configurations. If you look at the Gradle config, you are going to find useJUnitPlatform() there.

The Spock 2.0 release notes also state in the very first line of "highlights":

Spock is now a test engine based on the JUnit Platform

Ligun
@ligun
I understood now.
Alexander Kriegisch
@kriegaex
Very welcome. 🙂 Happy testing!
gaurav77
@gaurav77:matrix.org
[m]
Hello everyone. I am new to Spock and am trying to find if there are any active ore up-to-date extension projects that integrate Spock with gherkin feature files/directories, for e.g creating/validating Spock tests against gherkin files. I found two projects like these, but they are quite old and are not actively maintained: https://pease.github.io/ and https://github.com/davidmc24/spock-gherkin-poc
Alexander Kriegisch
@kriegaex
None that I am aware of, but actually I never felt the need or did any research, because Spock is a BDD tool already. I do admit that I never used Gherkin before, simply because I met Spock first and have been happy with it ever since. What is your use case?
12 replies
gaurav77
@gaurav77:matrix.org
[m]
Thanks Alexander. Interesting perspective. Cut out the extra step. Have the non-technical people write the test spec themselves. What plug-in did you use with jira. Was it XRay?
Slightly different topic: what do you think are the most powerful advantages of Spock over Cucumber which also follows the ‘given, when, then ‘ approach?
gaurav77
@gaurav77:matrix.org
[m]
Sure, Alexander. Thanks for your help.
MAXIM GOLOTA
@golotamaxim
Hello everyone. I try to test my service class as subject with some collaborators. In my service class some method supposes to use Util class with static method. How can I mock this static method and provide different results for different test cases?
3 replies
stansvec
@stansvec:matrix.org
[m]
Anyone else experiences: java.lang.Exception: No runnable methods when running a spock test in the newest IDEA version 2021.2?
Alexander Kriegisch
@kriegaex
@stansvec:matrix.org, no. I think it would be helpful for you to be more specific.
stansvec
@stansvec:matrix.org
[m]

I upgraded IDEA to the newest version and then when I run any spock test (whole class or individual methods) I get this error in the test console:

java.lang.Exception: No runnable methods

at org.junit.runners.BlockJUnit4ClassRunner.validateInstanceMethods(BlockJUnit4ClassRunner.java:191)
at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:128)
at org.junit.runners.ParentRunner.validate(ParentRunner.java:416)
at org.junit.runners.ParentRunner.<init>(ParentRunner.java:84)
at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:65)
at org.junit.internal.builders.JUnit4Builder.runnerForClass(JUnit4Builder.java:10)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:50)
at com.intellij.rt.junit.IdeaTestRunner\$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)

Was just wondering if this is somehow specific to my setup or a new general bug

Alexander Kriegisch
@kriegaex
When I said "be more specific", I was actually talking about an MCVE rather than a call stack without any code.
Alexander Kriegisch
@kriegaex
@stansvec:matrix.org: I just saw a similar problem in IDEA 2021.2 when running some tests in a Spock 2 project. Some tests run in IDEA, others do not. I cannot see a pattern yet. In Maven, all tests run,so it is an IDEA problem. What puzzles me is the JUnit 4 stuff in the stack trace. Spock 2 tests run on the JUnit 5 platform.
Alexander Kriegisch
@kriegaex
I created https://youtrack.jetbrains.com/issue/IDEA-275517. Let us wait and see what the JetBrains people say.
Alexander Kriegisch
@kriegaex
BTW, I noticed so many bugs and breaking changes in 2021.2, I think it is the worst update in a long time. I think, I created 3 different IDEA issues in the last 24 hours alone.
stansvec
@stansvec:matrix.org
[m]
Thanks for taking care of this!
Hope it won't take too long for them to fix it
I've noticed declining stability/quality with every new IDEA release for a while and it's getting quite worrying
Also found more issues with the newest one (no spock related)..
Alexander Kriegisch
@kriegaex

I have a problem with this kind of test:

package de.scrum_master.stackoverflow

import de.jodamob.kotlin.testrunner.OpenedClasses
import de.jodamob.kotlin.testrunner.OpenedPackages
import de.jodamob.kotlin.testrunner.SpotlinTestRunner
import org.junit.runner.RunWith
import org.spockframework.util.VersionNumber
import spock.lang.IgnoreIf
import spock.lang.Specification
import spock.lang.Unroll

/**
* See https://stackoverflow.com/q/48391716/1082681
* See https://github.com/dpreussler/kotlin-testrunner
*/
@RunWith(SpotlinTestRunner)
@OpenedClasses(FinalClass)
//@OpenedPackages("de.scrum_master.stackoverflow")
@IgnoreIf({
// de.jodamob.kotlin.testrunner.NoMoreFinalsClassLoader uses Javassist which tries to make
// protected java.lang.ClassLoader.defineClass accessible. Due to JEP-396, this is forbidden since JDK 16.
VersionNumber.parse(jvm.javaSpecificationVersion).major >= 16
})
class AnotherClassSpotlinRunnerTest extends Specification {
def "use SpotlinRunner to stub final method in final class"() {
given:
FinalClass finalClass = Stub() {
finalMethod() >> "mocked"
}

expect:
new AnotherClass().doSomething(finalClass) == "mocked"
}
}

The problem is that @IgnoreIf is evaluated too late for this case, because the error already occurs in the SpotlinTestRunner. I am looking for a way to keep this test in my Spock 1.3 playground project for reference, because up to JDK 15 is runs fine. Is there a way to stop a Spock (JUnit 4) test from running based on a condition, before the runner specified by @RunWith is excecuted?

Alexander Kriegisch
@kriegaex
Sorry, I made a copy & paste error with my previous post. Again:

I tried several things before, such as

Both are executed too late in the lifecycle, i.e. they are unable to prevent the SpotlinTestRunner from being initialised.

Actually, the runner has not been updated since version 0.3.1 in 2016, i.e. it knows nothing about JEP-396 and strong JDK encapsulation. The best solution would be for it to be upgraded by its author. My workaround for now is to replace the runner by a delegate, which I named JEP396AwareSpotlinTestRunner. It either passes through every method call to a SpotlinTestRunner or makes sure that all test executions are ignored in order to get correct test reporting.

package de.scrum_master.testing

import de.jodamob.kotlin.testrunner.SpotlinTestRunner
import org.junit.runner.Description
import org.junit.runner.manipulation.Filter
import org.junit.runner.manipulation.NoTestsRemainException
import org.junit.runner.manipulation.Sorter
import org.junit.runners.model.InitializationError
import org.spockframework.runtime.Sputnik

class JEP396AwareSpotlinTestRunner extends Sputnik {
private static final int javaMajor = Integer.parseInt(System.properties.getProperty("java.version").split("[.]")[0])
public static final boolean hasJEP396 = javaMajor >= 16

private static final Filter ignoreAllFilter = new Filter() {
@Override
boolean shouldRun(Description description) { return false }

@Override
String describe() { return "ignores all tests" }
}

private final Sputnik sputnik

JEP396AwareSpotlinTestRunner(Class<?> clazz) throws InitializationError {
super(clazz)
sputnik = hasJEP396 ? this : new SpotlinTestRunner(clazz)
}

@Override
void filter(Filter filter) throws NoTestsRemainException {
if (hasJEP396)
super.filter(ignoreAllFilter)
else
sputnik.filter(filter)
}

@Override
void run(RunNotifier notifier) {
if (hasJEP396)
super.run(notifier)
else
sputnik.run(notifier)
}

@Override
Description getDescription() {
if (hasJEP396)
return super.getDescription()
else
return sputnik.getDescription()
}

@Override
void sort(Sorter sorter) {
if (hasJEP396)
super.sort(sorter)
else
sputnik.sort(sorter)
}
}

The Spock spec still needs an @IgnoreIf or @Requires in addition to @RunWith(JEP396AwareSpotlinTestRunner), but otherwise no changes are required. It can even utilise a constant JEP396AwareSpotlinTestRunner.hasJEP396 for its own condition:

package de.scrum_master.stackoverflow

import de.jodamob.kotlin.testrunner.OpenedClasses
import de.jodamob.kotlin.testrunner.OpenedPackages
import de.scrum_master.testing.JEP396AwareSpotlinTestRunner
import org.junit.runner.RunWith
import spock.lang.IgnoreIf
import spock.lang.Specification

/**
* See https://stackoverflow.com/q/48391716/1082681
* See https://github.com/dpreussler/kotlin-testrunner
*/
@RunWith(JEP396AwareSpotlinTestRunner)
@IgnoreIf({ JEP396AwareSpotlinTestRunner.hasJEP396 })
@OpenedClasses(FinalClass)
//@OpenedPackages("de.scrum_master.stackoverflow")
class AnotherClassSpotlinRunnerTest extends Specification {
def "use SpotlinRunner to stub final method in final class"() {
given:
FinalClass finalClass = Stub() {
finalMethod() >> "mocked"
}

expect:
new AnotherClass().doSomething(finalClass) == "mocked"
}
}
Alexander Kriegisch
@kriegaex

Thanks all for listening to me talking to myself. But if I ask the community a question and find an answer, I think it is only fair to share it.

Peter Guo
@mushanzi1216
hello anyone knows how to mock a generic class
Alexander Kriegisch
@kriegaex
What I mean is: Your question is not unlike "Does anyone know how to drive a car?" and then someone saying "yeah, sure". It would be the correct answer, but not helpful to the person asking.
treethingy
@treethingy
Hi, I'm looking for a sophisticated way to modify the way "Condition not satisfied:" output is rendered. I'm validating XML agains XSD and Schematron. I've created a hamcrest matcher and implemented "describeMismatch". Is there a better way to do it?
3 replies
liubo404
@liubo404
Error:Groovyc: While compiling tests of iceim: Could not instantiate global transform class org.spockframework.compiler.SpockTransform specified at jar:file:/home/ben/.m2/repository/org/spockframework/spock-core/2.0-groovy-3.0/spock-core-2.0-groovy-3.0.jar!/META-INF/services/org.codehaus.groovy.transform.ASTTransformation  because of exception java.lang.NullPointerException
how to resolve this error, any ideas, thanks a lot
I google for an hour, nothing useful can help~
the maven dependency below:
    <dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<version>2.0-groovy-3.0</version>
<scope>test</scope>
</dependency>

<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.12.1</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compileTests</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<useFile>false</useFile>
<includes>
<include>**/*Spec.java</include>
</includes>
</configuration>
</plugin>

@xialugui
@liubo404 试试显示指定groovy版本
<!-- https://mvnrepository.com/artifact/org.codehaus.groovy/groovy-all -->
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>3.0.8</version>
<type>pom</type>
</dependency>
liubo404
@liubo404
@xialugui 好象还是不太行, 多谢~
Alexander Kriegisch
@kriegaex

@xialugui, groovy-all is not necessary for Spock 2.0.

@liubo404 , it is hard to reason about a partial POM without any code. Please be so kind as to share a reproducer on GitHub. This is called an MCVE. It helps us to understand and analyse your problem and you to get the answers you need.