Hello everyone. I have a problem similar to RealForce1024 but I already have the Jupiter dependency corretly setup. I'm trying to create a Spec but TestEngine with ID 'spock' failed to discover tests
.
Can anyone give me some light on this?
I'm using IntelliJ and this is my build.gradle
file:
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'
implementation 'org.codehaus.groovy:groovy:3.0.10'
testImplementation platform("org.spockframework:spock-bom:2.1-groovy-3.0")
testImplementation "org.spockframework:spock-core"
testRuntimeOnly 'net.bytebuddy:byte-buddy:1.12.8'
}
test {
useJUnitPlatform()
}
Hello team 😃
I am facing below problem while writing test using SPOCK
I would like to know what knowledge gap I have, to understand why its not working
I have a main class called ClassUnderTest
@AllArgsConstructor
class ClassUnderTest {
MyCache cache;
def doSomething(String key, String value){
cache.add(key, value);
}
}
A Facade spec called MyFacade.
class MyFacade extends Specification{
MyCache cache = new MyCache()
def ClassUnderTest getInstance(){
return new ClassUnderTest(cache, Mock(OtherDependency))
}
def boolean isPresentInCache(String someKey){
return null != cache.get(someKey)
}
}
and test spec called ClassUnderTestSpec.
class ClassUnderTestSpec extends Specification{
MyFacade facade = new MyFacade()
def 'check if key is added to cache'(){
given:'get the instance from facade'
ClassUnderTest instance = facade.getInstance()
when: ' we call do something'
instance.doSomething('myKey', 'myValue')
then: 'cache should have myKey'
facade.isPresentInCache('myKey')
}
}
my main goal is to test if ClassUnderTest is populating the cache correctly.
To abstract it out I am using facade. So that my test remains unchanged if the cache class changes say from MyCache to any other class.
So in test I want
Somehow even though facade has cache instance and same MyCache instance is injected in ClassUnderTest, the test doesn't pass 😐️
Thanks for your time and help in advance. 😅
PS: I am a Java developer. Know Groovy only just by writing SPOCK tests
mvn -Dspock.iKnowWhatImDoing.disableGroovyVersionCheck=true
has this error: Unknown lifecycle phase ".iKnowWhatImDoing.disableGroovyVersionCheck=true". You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]mvn "-Dspock.iKnowWhatImDoing.disableGroovyVersionCheck=true" test
mvn "-Dspock.iKnowWhatImDoing.disableGroovyVersionCheck=true" test
with no issues about dependencies. If I run a basic spock test then it fails with the same error: Could not instantiate global transform class org.spockframework.compiler.SpockTransform specified at jar:file:/C:/Users/alessandro.morsiani/.m2/repository/org/spockframework/spock-core/2.1-groovy-3.0/spock-core-2.1-groovy-3.0.jar!/META-INF/services/org.codehaus.groovy.transform.ASTTransformation because of exception java.lang.NullPointerException: Cannot invoke "java.net.URL.toString()" because the return value of "java.security.CodeSource.getLocation()" is null
2.2-groovy-3.0-SNAPSHOT
from https://oss.sonatype.org/content/repositories/snapshots/org/spockframework/
org.codehaus.mojo.groovy:groovy-mojo-support:jar:1.0-beta-3
Hi, I've observed strange behavior when working with static fields in Spock.
The initialization order is different than I'd get with ~similiar example in java. Minimal example is as follows (not idiomatic groovy, but that's not the point):
class Foo {
public static List<Object> getList(){
print("Getting list")
return new ArrayList<>();
}
}
class MySpec extends Specification {
static {
print("Static block")
}
static List<Object> = Foo.getList()
def "test"(){
expect:
1 == 1
}
}
I would expect the order of messages to be:
But it's totally the other way. Is it something groovy-specific, or some spock lifecycle magic? It works as expected in java. Could you please give me some explanation, or point me to related docs.