Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • Jun 28 19:43
    leonard84 review_requested #1438
  • Jun 27 11:03
    codecov[bot] commented #1430
  • Jun 27 11:02
    codecov[bot] commented #1430
  • Jun 27 11:02
    codecov[bot] commented #1430
  • Jun 27 11:01
    codecov[bot] commented #1430
  • Jun 27 11:01
    codecov[bot] commented #1430
  • Jun 27 11:00
    codecov[bot] commented #1430
  • Jun 27 11:00
    codecov[bot] commented #1430
  • Jun 27 11:00
    codecov[bot] commented #1430
  • Jun 27 10:56
    codecov[bot] commented #1430
  • Jun 27 10:56
    leonard84 synchronize #1430
  • Jun 27 09:58

    github-actions[bot] on gh-pages

    Publish docs/2.2-SNAPSHOT (compare)

  • Jun 27 09:50

    github-actions[bot] on gh-pages

    Publish javadoc/2.2-SNAPSHOT (compare)

  • Jun 27 09:48

    leonard84 on master

    Add docs for `@Tag` (compare)

  • Jun 27 09:48
    leonard84 closed #1488
  • Jun 27 09:47
    codecov[bot] commented #1488
  • Jun 27 09:47
    codecov[bot] commented #1488
  • Jun 27 09:46
    codecov[bot] commented #1488
  • Jun 27 09:46
    codecov[bot] commented #1488
  • Jun 27 09:46
    codecov[bot] commented #1488
Boris Petrov
@boris-petrov

Hi all, I'm having troubles with additionalInterfaces. Using 2.1-M2. I don't understand why this doesn't call the method foo:

    class A {}
    class B {}
    interface C { B foo() }
...
        def a = Stub(A, additionalInterfaces: [C]) {
            foo() >> { println 2; new B() }
        }
        println(a instanceof C)
        def b = a.foo()
        println b.getClass()

This prints true and then class java.lang.Object. 2 is not printed. Why isn't this calling my stubbed method?!

26 replies
I hit this trying to debug another issue with additionalInterfaces where the method is called and I return a nice value but I get a NPE in org.spockframework.gentyref.GenericTypeReflector:73- java.lang.NullPointerException: Cannot invoke "Object.getClass()" because "type" is null.
balavivek143
@balavivek143

Hello Every one.. We are using spock core version 1.3-groovy-2.5 version and running few of our SpringBootTest using EmbeddedSpecRunner.groovy with RunListeners.

EmbeddedSpecRunner runner = new EmbeddedSpecRunner();
runner.setListeners(Collections.singletonList(new TextListener(System.out)));

When we are upgrading to 2.1-M2-groovy-3.0, we are seeing setListeners are not available ! also seeing in the source code, listener array variable got removed in the commit 693832c3cfaeebf349025002561acc556d7dbcae

Now how to add any run listener with EmbeddedSpecRunner ?

1 reply
Juliane Hochstein
@exwima
Hi everyone! As a part-time student in computer science, I wrote a short paper about Spock for university last term and really liked how it made me think about writing tests. :-) So somehow I volunteered to give a talk about Spock in a public lecture series at the city library of Stuttgart, Germany. Due to the pandemic, the talk will now happen online on this thursday, see https://www.cccs.de/events/202201-testframework-spock/ (text in German, like my talk). Maybe someone here is interested? Please note: I am really no expert in programming or unit testing and still a beginner at Spock myself, so the talk will only be a basic introduction for other people who might be interested in learning about Spock, too.
5 replies
Roland Tepp
@roland-tepp:matrix.org
[m]

Hi, I am getting a weird issue with Spock where as soon as I declare throws in my then: block, the test starts to fail with a NullPointerException.

If I remove the throws assertion, the test fails with the expected (UnsupportedOperationException) exception as it should.

Leonard Brünings
@leonard84
It is probably caused by some re-ordering caused by the throws special handling, but without any sample code I can't help you. You can however use https://gwc-experiment.appspot.com/ to look at the AST to see the transformed code and where the issue is.
Roland Tepp
@roland-tepp:matrix.org
[m]
Yeah. Quite possible. there's a ton of closure based model building DSL going on in the set'up phase and those set-up closures are the ones that throw an NPE.
sebastian-rembisz-jorsek
@sebastian-rembisz-jorsek
Do you have any plans do add module for Quarkus? Something similar to Spring module?
Leonard Brünings
@leonard84
Not really, I've no experience with Quarkus, but I would help out if Quarkus wants to add Spock support.
Jim Renwick
@j1mmi:matrix.org
[m]
Hi, this is a dumb question but I'm trying to find the import location for callRealMethod()- my IDE can't locate it and I can't see any import statements in the documentation...can someone point me to where the method lives?
1 reply
Marcin Grzejszczak
@marcingrzejszczak

Hi! I'm trying to bump Spring Cloud Contract to use latest Boot 3.0.0 and Framework 6.0.0. So all of my Spock Spring tests are failing because NPEs. That means that the tests have not had their Spring Context registered. I've started to debug things and apparently org.spockframework.spring.SpringExtension#isSpringSpec returns false now. So I've added temporarily the @ContextConfiguration annotation to make that method return true. I've started getting more problematic exceptions like this one

2022-01-21 11:05:46.899 ERROR 67468 --- [           main] o.s.boot.SpringApplication               : Application run failed

java.lang.NoClassDefFoundError: org/springframework/beans/factory/config/InstantiationAwareBeanPostProcessorAdapter
    at java.base/java.lang.ClassLoader.defineClass1(Native Method)
    at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1012)
    at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:150)
    at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:862)
    at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:760)
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:681)
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:639)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
    at org.spockframework.spring.mock.SpockContextCustomizer.customizeContext(SpockContextCustomizer.java:43)
    at org.springframework.boot.test.context.SpringBootContextLoader$ContextCustomizerAdapter.initialize(SpringBootContextLoader.java:294)
    at org.springframework.boot.SpringApplication.applyInitializers(SpringApplication.java:599)
    at org.springframework.boot.SpringApplication.prepareContext(SpringApplication.java:367)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:301)
    at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:124)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:123)
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:124)
    at org.springframework.cloud.contract.wiremock.WireMockTestExecutionListener.applicationContextBroken(WireMockTestExecutionListener.java:120)
    at org.springframework.cloud.contract.wiremock.WireMockTestExecutionListener.beforeTestClass(WireMockTestExecutionListener.java:43)
    at org.springframework.test.context.TestContextManager.beforeTestClass(TestContextManager.java:213)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)

As for this particular problem this is Spring's corresponding issue spring-projects/spring-framework#25165 . Anyways, the question is has anyone checked it there's a way to make Spcok + JUnit5 + Spring Framework 6 work together?

3 replies
Luís Gonçalves
@luisgoncalves

Hi all!
I'm upgrading Spock to v2 and there are tests that use JUnit 4 rules, so I've added the "JUnit 4 Module". One of the rules in use is TempusFugit's @Repeating, which internally evaluates JUnit's Statementmultiple times. This results in an error in Spock:

    java.util.NoSuchElementException
        at java.base/java.util.LinkedList.removeFirst(LinkedList.java:274)
        at org.spockframework.mock.runtime.MockController.leaveScope(MockController.java:76)

I wrote an extension that tries to do invocation.proceed()twice and I get the same error. Is this the expected behavior? I read somewhere that spec instances can only be used once... so I guess that's related?

If the above is by design, what would be an approach to implement something like TempusFugit's @Repeating / @Concurrent rules using "pure" Spock? Thanks in advance!

Renato Athaydes
@renatoathaydes
Hi @luisgoncalves :D long time no see.
I think you can replace the repeating annotation with something like where: i << (0..100)
1 reply
but @Concurrent we'll probably have to write ourselves.. that didn't work on Spock anyway, as far as I know...
racsogl
@racsogl:matrix.org
[m]

hello

I have this code to test

app = Optional.ofNullable(receiverCompany.getApplication()).orElse(getDefaultApp());

and my test is

given:
def application = new GenericApp()
def receiverCompanyDTO = new ReceiverCompanyDTO(
application: application
)
ApplicationService applicationService = new ApplicationServiceImpl( Mock (ApplicationsDAO))
when:
def result= applicationService.getByReceiverCompany(receiverCompanyDTO)
then:
result != null

always enters through orElse , I don’t know how to make for that the variable app to have the value of receiverCompanyDTO

Can we help me with this issue?

Thanks a lot

Alexander Kriegisch
@kriegaex
I am ready to help you, if you would please be so kind to provide a full MCVE. With fragments of a test using a handful of classes, all of which I cannot see, and even only a single line of code from the class under test, it is hard to say anything intelligent about your problem.
racsogl
@racsogl:matrix.org
[m]

:point_up: Edit: hello

I have this code to test
app = Optional.ofNullable(receiverCompany.getApplication()).orElse(getDefaultApp());

and my test is

given:
def application = new GenericApp()
def receiverCompanyDTO = new ReceiverCompanyDTO(
application: application
)
ApplicationService applicationService = new ApplicationServiceImpl( Mock (ApplicationsDAO))
when:
def result= applicationService.getByReceiverCompany(receiverCompanyDTO)
then:
result != null

always enters through orElse , I don’t know how to make for that the variable app to have the value of receiverCompanyDTO

Can we help me with this issue?

Thanks a lot

:point_up: Edit: hello

I have this code to test

app = Optional.ofNullable(receiverCompany.getApplication()).orElse(getDefaultApp());

and my test is

given:
def application = new GenericApp()
def receiverCompanyDTO = new ReceiverCompanyDTO(
application: application
)
ApplicationService applicationService = new ApplicationServiceImpl( Mock (ApplicationsDAO))
when:
def result= applicationService.getByReceiverCompany(receiverCompanyDTO)
then:
result != null

always enters through orElse , I don’t know how to make for that the variable app to have the value of receiverCompanyDTO

Can we help me with this issue?

Thanks a lot

:point_up: Edit: hello

I have this code to test

Where public class GenericApp extends Application {app = Optional.ofNullable(receiverCompany.getApplication()).orElse(getDefaultApp());

and my test is

given:
def application = new GenericApp()
def receiverCompanyDTO = new ReceiverCompanyDTO(
application: application
)
ApplicationService applicationService = new ApplicationServiceImpl( Mock (ApplicationsDAO))
when:
def result= applicationService.getByReceiverCompany(receiverCompanyDTO)
then:
result != null

always enters through orElse , I don’t know how to make for that the variable app to have the value of receiverCompanyDTO

Can we help me with this issue?

Thanks a lot

:point_up: Edit: hello

I have this code to test

Where public class GenericApp extends Application {
.......
}
@Override
public Application getByReceiverCompany(final ReceiverCompanyDTO receiverCompany) {
final Application app = Optional.ofNullable(receiverCompany).map(ReceiverCompanyDTO::getApplication)
.orElse(getDefaultApp());
return app;
}

and my test is

given:
def application = new GenericApp()
def receiverCompanyDTO = new ReceiverCompanyDTO(
application: application
)
ApplicationService applicationService = new ApplicationServiceImpl( Mock (ApplicationsDAO))
when:
def result= applicationService.getByReceiverCompany(receiverCompanyDTO)
then:
result != null

always enters through orElse , I don’t know how to make for that the variable app to have the value of receiverCompanyDTO

Can we help me with this issue?

Thanks a lot

Alexander Kriegisch
@kriegaex
Something seems to be wrong with the editing feature of whatever platform you use in order to edit messages here, because they get duplicated. I see the same message 3x now. BTW, would you mind using code blocks in GitHub markdown format (fenced blocks using three backticks)? Your posts are barely readable.
Whatever this matrix thing of yours does, it does not edit messages correctly. Maybe you want to use a web interface or a dedicated app for Gitter.
racsogl
@racsogl:matrix.org
[m]

:point_up: Edit: hello

I have this code to test

Where public class GenericApp extends Application {
.......
}
public class ApplicationService {
@Override
public Application getByReceiverCompany(final ReceiverCompanyDTO receiverCompany) {
final Application app = Optional.ofNullable(receiverCompany).map(ReceiverCompanyDTO::getApplication)
.orElse(getDefaultApp());
return app;
}
}

and my test is

            given:
        def application = new GenericApp()
        def receiverCompanyDTO = new ReceiverCompanyDTO(
                application: application
        )
        ApplicationService applicationService = new ApplicationServiceImpl( Mock (ApplicationsDAO))
    when:
        def result= applicationService.getByReceiverCompany(receiverCompanyDTO)
    then:
        result != null

always enters through orElse , I don’t know to make for that the variable app to have the value of receiverCompanyDTO

Can we help me with this issue?

Thanks a lot

sorry, I hope it is clearer now
Alexander Kriegisch
@kriegaex
Please do not require others to manually generate scaffolding code around your fragments. That is your job, with all due respect. An MCVE is something I can just copy and run.
The first rule not of Fight Club but of asking others for help, not just about programming: Help others to help you, make it as easy as possible for them.
racsogl
@racsogl:matrix.org
[m]

:point_up: Edit: hello

I have this code to test

Where

public class GenericApp extends Application {

}
public class ApplicationService {
@Override
public Application getByReceiverCompany(final ReceiverCompanyDTO receiverCompany) {
final Application app = Optional.ofNullable(receiverCompany).map(ReceiverCompanyDTO::getApplication)
.orElse(getDefaultApp());
return app;
}
}


and my test is

class ApplicationServiceSpec extends Specification {

def 'getByReceiverCompany ReceiverCompanyDTO -> application is not null'() {
    given:
        def application = new GenericApp()
        def receiverCompanyDTO = new ReceiverCompanyDTO(
                application: application
        )
        ApplicationService applicationService = new ApplicationServiceImpl( Mock (ApplicationsDAO))
    when:
        def result= applicationService.getByReceiverCompany(receiverCompanyDTO)
    then:
        result != null
}

}
```

always enters through orElse , I don’t know to make for that the variable app to have the value of receiverCompanyDTO

Can we help me with this issue?

Thanks a lot

:point_up: Edit: hello

I have this code to test

Where

public class GenericApp extends Application {

}
public class ApplicationService {
@Override
public Application getByReceiverCompany(final ReceiverCompanyDTO receiverCompany) {
final Application app = Optional.ofNullable(receiverCompany).map(ReceiverCompanyDTO::getApplication)
.orElse(getDefaultApp());
return app;
}
}


and my test is

class ApplicationServiceSpec extends Specification {

def 'getByReceiverCompany ReceiverCompanyDTO -> application is not null'() {
given:
def application = new GenericApp()
def receiverCompanyDTO = new ReceiverCompanyDTO(
application: application
)
ApplicationService applicationService = new ApplicationServiceImpl( Mock (ApplicationsDAO))
when:
def result= applicationService.getByReceiverCompany(receiverCompanyDTO)
then:
result != null
}

}
```

always enters through orElse , I don’t know to make for that the variable app to have the value of receiverCompanyDTO

Can we help me with this issue?

Thanks a lot

:point_up: Edit: hello

I have this code to test

Where

public class GenericApp extends Application {

}
public class ApplicationService {
@Override
public Application getByReceiverCompany(final ReceiverCompanyDTO receiverCompany) {
final Application app = Optional.ofNullable(receiverCompany).map(ReceiverCompanyDTO::getApplication)
.orElse(getDefaultApp());
return app;
}
}

and my test is

class ApplicationServiceSpec extends Specification {

def 'getByReceiverCompany ReceiverCompanyDTO -> application is not null'() {
given:
def application = new GenericApp()
def receiverCompanyDTO = new ReceiverCompanyDTO(
application: application
)
ApplicationService applicationService = new ApplicationServiceImpl( Mock (ApplicationsDAO))
when:
def result= applicationService.getByReceiverCompany(receiverCompanyDTO)
then:
result != null
}

}

always enters through orElse , I don’t know to make for that the variable app to have the value of receiverCompanyDTO

Can we help me with this issue?

Thanks a lot

Alexander Kriegisch
@kriegaex
Questions:
  • ApplicationService seems to override something, but does not implement any interfaces or extend another class. What is missing there?
  • Where are classes ReceiverCompanyDTO, ApplicationServiceImpl and ApplicationsDAO? Without them, the example does not compile.
  • GenericApp extends Application, but Application is missing.
2 replies
It would help if you created a new dummy project, copying all the stuff needed to reproduce the problem there. Don't post here before your MCVE actually compiles and runs.
racsogl
@racsogl:matrix.org
[m]

:point_up: Edit: hello

I have this code to test

Where

public class GenericApp extends Application {

}
public class ApplicationService {

public Application getByReceiverCompany(final ReceiverCompanyDTO receiverCompany) {
final Application app = Optional.ofNullable(receiverCompany).map(ReceiverCompanyDTO::getApplication)
.orElse(getDefaultApp());
return app;
}
}

and my test is

class ApplicationServiceSpec extends Specification {

def 'getByReceiverCompany ReceiverCompanyDTO -> application is not null'() {
given:
def application = new GenericApp()
def receiverCompanyDTO = new ReceiverCompanyDTO(
application: application
)
ApplicationService applicationService = new ApplicationServiceImpl( Mock (ApplicationsDAO))
when:
def result= applicationService.getByReceiverCompany(receiverCompanyDTO)
then:
result != null
}

}

always enters through orElse , I don’t know to make for that the variable app to have the value of receiverCompanyDTO

Can we help me with this issue?

Thanks a lot

Alexander Kriegisch
@kriegaex
Take your time, I am going to have a meal now. Gonna BBL. Maybe someone else is faster than I, but I am going to check this channel again later.
And please, please stop posting the same code 100x. Edit your messages or post when everything is complete. Thank you.
Leonard Brünings
@leonard84
I think the problem is that @racsogl:matrix.org is using Matrix, so the editing does not work.
Tip use https://gwc-experiment.appspot.com/ to create a runnable example and then use the Share as Link button to share your example here.
racsogl
@racsogl:matrix.org
[m]

:point_up: Edit: hello

I have this code to test

Where

public class GenericApp extends Application {

}
public class ReceiverCompanyDTO {


    Application application;

    public Application getApplication() {
        return application;
    }

    public void setApplication(Application application) {
        this.application = application;
    }
}
public interface ApplicationsDAO {
}
public interface ApplicationService {
Application getByReceiverCompany(ReceiverCompanyDTO receiverCompany);
}

public class ApplicationService implements ApplicationService{

    private final ApplicationsDAO applicationDao;
    public ApplicationServiceImpl(final ApplicationsDAO applicationDao) {
        this.applicationDao = applicationDao;
    }
        @Override
         public Application getByReceiverCompany(final ReceiverCompanyDTO receiverCompany) {
                    final Application app = Optional.ofNullable(receiverCompany).map(ReceiverCompanyDTO::getApplication)
                   .orElse(getDefaultApp());
                   return app;
        }
}

and my test is

class ApplicationServiceSpec extends Specification {

def 'getByReceiverCompany ReceiverCompanyDTO -> application is not null'() {
given:
def application = new GenericApp()
def receiverCompanyDTO = new ReceiverCompanyDTO(
application: application
)
ApplicationService applicationService = new ApplicationServiceImpl( Mock (ApplicationsDAO))
when:
def result= applicationService.getByReceiverCompany(receiverCompanyDTO)
then:
result != null
}

}

always enters through orElse , I don’t know to make for that the variable app to have the value of receiverCompanyDTO

Can we help me with this issue?

Thanks a lot

:point_up: Edit: hello

I have this code to test

Where

public class Application {
}
public class GenericApp extends Application {

}
public class ReceiverCompanyDTO {


    Application application;

    public Application getApplication() {
        return application;
    }

    public void setApplication(Application application) {
        this.application = application;
    }
}
public interface ApplicationsDAO {
}
public interface ApplicationService {
Application getByReceiverCompany(ReceiverCompanyDTO receiverCompany);
}

public class ApplicationService implements ApplicationService{

    private final ApplicationsDAO applicationDao;
    public ApplicationServiceImpl(final ApplicationsDAO applicationDao) {
        this.applicationDao = applicationDao;
    }
        @Override
         public Application getByReceiverCompany(final ReceiverCompanyDTO receiverCompany) {
                    final Application app = Optional.ofNullable(receiverCompany).map(ReceiverCompanyDTO::getApplication)
                   .orElse(getDefaultApp());
                   return app;
        }
}

and my test is

class ApplicationServiceSpec extends Specification {

def 'getByReceiverCompany ReceiverCompanyDTO -> application is not null'() {
given:
def application = new GenericApp()
def receiverCompanyDTO = new ReceiverCompanyDTO(
application: application
)
ApplicationService applicationService = new ApplicationServiceImpl( Mock (ApplicationsDAO))
when:
def result= applicationService.getByReceiverCompany(receiverCompanyDTO)
then:
result != null
}

}

always enters through orElse , I don’t know to make for that the variable app to have the value of receiverCompanyDTO

Can we help me with this issue?

Thanks a lot

I think it should now compile without any problems.
leonard84 thanks for the tip
Leonard Brünings
@leonard84
Change
.orElse(getDefaultApp())
to
.orElseGet(this::getDefaultApp)
the first one will always call that method, even if it is non-null
Alexander Kriegisch
@kriegaex
Sorry to be late, I just finished my daily business. @racsogl:matrix.org, I am happy that you decided to provide an MCVE and that your problem was solved by Leonard already, so I do not have to reply anymore. Only some additional explanation:
  • Look at your own code: in map(ReceiverCompanyDTO::getApplication) you used a method reference. That is the same pattern as @leonard84 suggested to you.
  • Direct method calls will be evaluated as method parameters, and ofNullable, map, orElse are nothing else but simple method calls. Therefore, you need to pass method references or lambdas (or closures in Groovy).
  • The reason you have to change orElse to orElseGet is explained in this tutorial. Under Java, .orElse(this::getDefaultApp) would not even compile. Under Groovy, it would, but it would return a closure, and this is not what you need. You want to evaluate the closure or lambda or method reference, but only if necessary. Please also not the performance impact explanation in the tutorial.
  • .orElseGet(this::getDefaultApp) is equivalent to the lambda expression .orElseGet(() -> getDefaultApp()).
Bottom line: Your problem is is the code under test, not in the Spock test at all.
BTW, throw new Exception(); only compiles under Groovy, not under Java, because it is a checked exception. There, you would have to either declare the exception or throw a RuntimeException.
Alexander Kriegisch
@kriegaex
If for whatever reason you need the side effect of orElse(getDefaultApp()) in your application and can change ApplicationServiceImpl.getDefaultApp() from private to protected in order to make it mockable and testable, you could get away with spying on ApplicationServiceImpl like this in order to avoid the exception:
    ApplicationService applicationService = Spy(ApplicationServiceImpl, constructorArgs: [Mock(ApplicationsDAO)]) {
      getDefaultApp() >> application
    }
racsogl
@racsogl:matrix.org
[m]

Hello @leonard84 , @kriegaex

I have reviewed your answers

Thank for your time.