spockframework / spockframework/spock

thrown check doesn't handle checked exceptions with mocks

Open
#420 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Module-Core Type-Defect
Dominant language
Java
Stars
3.6k
Forks
483
PR merge metrics
No merged PRs in 30d

Description

Originally reported on Google Code with ID 298

In a Mock throw a checked exception

   1 * repository.findOne(42l) >> {throw new NotFoundException("oh no!")}

In the "then" clause use the thrown check to verify the exception

   thrown(NotFoundException)

The result is a test failure because groovy wraps the exception with UndeclaredThrowableException.
I think thrown should check for this exception type and check if the type matches the
"undeclaredThrowable" property of UndeclaredThrowableException.

Exception:

Expected exception com.fl.rest.NotFoundException, but got java.lang.reflect.UndeclaredThrowableException
    at spock.lang.Specification.thrown(Specification.java:232)
    at ...###Test.read a bean(###ControllerTest.groovy:47)
Caused by: java.lang.reflect.UndeclaredThrowableException
    at ...###Controller.readBean_aroundBody0(###Controller.java:32)
    at ...###Controller.readBean_aroundBody1$advice(###Controller.java:31)
    at ...###Controller.readBean(###Controller.java:1)
    at ...###ControllerTest.read a bean(###ControllerTest.groovy:43)
Caused by: ....NotFoundException: oh no!
    at ...###ControllerTest.read a bean_closure3(###ControllerTest.groovy:46)
    at groovy.lang.Closure.call(Closure.java:412)
    at org.spockframework.util.GroovyRuntimeUtil.invokeClosure(GroovyRuntimeUtil.java:130)
    at org.spockframework.mock.CodeResultGenerator.generateSingle(CodeResultGenerator.java:38)
    at org.spockframework.mock.SingleResultGenerator.generate(SingleResultGenerator.java:28)
    at org.spockframework.mock.ResultGeneratorChain.generate(ResultGeneratorChain.java:46)
    at org.spockframework.mock.MockInteraction.accept(MockInteraction.java:60)
    at org.spockframework.mock.MockInteractionDecorator.accept(MockInteractionDecorator.java:41)
    at org.spockframework.mock.InteractionScope$1.accept(InteractionScope.java:38)
    at org.spockframework.mock.MockController.dispatch(MockController.java:42)
    at org.spockframework.mock.DefaultMockFactory$1.invoke(DefaultMockFactory.java:70)
    ... 4 more



What version of Spock and Groovy are you using?
0.6-groovy-1.8

Please provide any additional information below. You can also assign
labels.

It's a simple change to the thrown method of Specification that would make working
with checked exceptions & mocks a lot easier.

Reported by otatop@otatop.com on 2013-02-11 16:37:19

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at spock.lang.Specification.thrown, shown at Specification.java:232, and reproduce the checked-exception mock case described in the issue. Done means thrown recognizes the underlying NotFoundException instead of reporting only UndeclaredThrowableException, with regression coverage for this scenario.

Written by the indexing model from the issue text.

Assessment

Tech stack
groovy, java
Domain
testing
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.