New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spock fails with insufficient invocation count when route cause was underneath exception #849

hajdukd opened this Issue Jun 5, 2018 · 5 comments


None yet
4 participants

hajdukd commented Jun 5, 2018

Assuming we use groovy and assuming we have such service method:

method(input){ Long val = Optional.ofNullable(input).map(Integer::longValue).orElse(doesntmatter);
return otherService.something(val);

Also assuming we expect that second service Mock will be invoked once (1 * otherService...).
When passing Long as "input" parameter i would expect exception:

java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer

But what i get is failed invocation count for otherService shadowing underneath exception.

Removing count check results in correct behaviour - exception being properly handled.

For me it looks like Spock completely ignores underneath exceptions in case of failed invocation count checks.


This comment has been minimized.


leonard84 commented Jun 30, 2018

Please provide a SSCCE and provide the other info that is suggested in the issue template.

This works fine:

class SuppressedExceptions extends Specification {

  def "Mock interaction violations don't hide exceptions"() {
    List mock = Mock()

    List foo = 1

    1 * mock.add(_)

Fails with

org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '1' with class 'java.lang.Integer' to class 'java.util.List'

	at org.spockframework.smoke.mock.SuppressedExceptions.Mock interaction violations don't hide exceptions(SuppressedExceptions.groovy:12)

This comment has been minimized.

hajdukd commented Jul 1, 2018

Ill provide an extensive example. Ps. The redirect does not work.


This comment has been minimized.


MartyIX commented Jul 1, 2018

@hajdukd Correct link is: - Short, Self Contained, Correct (Compilable), Example

@leonard84 leonard84 added SSCCE required and removed incomplete labels Jul 7, 2018


This comment has been minimized.

FrantaM commented Sep 21, 2018

import spock.lang.*

class Foo {
    def bar
    def baz
    void run() {
        int cce = bar

class MyFirstSpec extends Specification {
  def "let's try this!"() {
    Foo foo = new Foo() = "not int"
    foo.baz = Mock(List)


    1 * foo.baz.size()
 - let's try this!   FAILED

   Too few invocations for:
   1 * foo.baz.size()   (0 invocations)
   Unmatched invocations (ordered by similarity):
   at org.spockframework.mock.runtime.InteractionScope.verifyInteractions(
   at org.spockframework.mock.runtime.MockController.leaveScope(
   at MyFirstSpec.let's try this!(Script1.groovy:23)

Without the cardinality:

 - let's try this!   FAILED

   Expected exception of type 'java.lang.IllegalArgumentException', but got 'org.codehaus.groovy.runtime.typehandling.GroovyCastException'
   at org.spockframework.lang.SpecInternals.checkExceptionThrown(
   at org.spockframework.lang.SpecInternals.thrownImpl(
   at MyFirstSpec.let's try this!(Script1.groovy:24)
   Caused by: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'not int' with class 'java.lang.String' to class 'int'
   at MyFirstSpec.let's try this!(Script1.groovy:21)

@hajdukd hajdukd closed this Sep 21, 2018

@hajdukd hajdukd reopened this Sep 21, 2018


This comment has been minimized.

hajdukd commented Sep 21, 2018

Looks like @FrantaM added what you @leonard84 wanned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment