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

Open
hajdukd opened this Issue Jun 5, 2018 · 5 comments

Comments

Projects
None yet
4 participants
@hajdukd

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.

@leonard84

This comment has been minimized.

Member

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"() {
    given:
    List mock = Mock()

    when:
    List foo = 1
    mock.add("foo")

    then:
    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)
@hajdukd

This comment has been minimized.

hajdukd commented Jul 1, 2018

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

@MartyIX

This comment has been minimized.

Contributor

MartyIX commented Jul 1, 2018

@hajdukd Correct link is: http://www.sscce.org/ - Short, Self Contained, Correct (Compilable), Example

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

@FrantaM

This comment has been minimized.

FrantaM commented Sep 21, 2018

import spock.lang.*

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

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

    when:
    foo.run()

    then:
    thrown(IllegalArgumentException)
    1 * foo.baz.size()
  }
}
MyFirstSpec
 - let's try this!   FAILED

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

Without the cardinality:

    then:
    thrown(IllegalArgumentException)
    foo.baz.size()
MyFirstSpec
 - 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(SpecInternals.java:79)
   at org.spockframework.lang.SpecInternals.thrownImpl(SpecInternals.java:66)
   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 Foo.run(Script1.groovy:8)
   at MyFirstSpec.let's try this!(Script1.groovy:21)

@hajdukd hajdukd closed this Sep 21, 2018

@hajdukd hajdukd reopened this Sep 21, 2018

@hajdukd

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