spockframework / spockframework/spock

possible bug in grails using spock+controller handleException method

Open
#467 1 comment 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 345

hi, 

I'm using grails 2.3.5 and spock.
i got a particular configuration, and i'm facing some strange behavior with spock tests.

in order to replicate the problem you can find a semplification on my project here:


https://github.com/tamershahin/spockTest.git

this is the situation: 
class TestController { 

 def index() { 
        ... 
        throw new Exception('dummy') 
  } 

 def indexObj(IndexCommand command) { 
       ... 
        throw new Exception('dummy') 
    } 

   def handleException(Exception e) { 
        if (Environment.current == Environment.DEVELOPMENT) { 
            log.debug 'Exception:: ', e 
        } 
    } 
} 
essentially, if i run a test with this code: 

    void "test with "() { 

        when: 'pass to the action a mocked commandObject' 
        def commandMock = mockCommandObject(IndexCommand) 
        commandMock.id = 1 
        controller.indexObj(commandMock) 

        then: "expected a Exception" 
        Exception ex = thrown() 
        assert ex 

    } 

test pass without any problem (wrong, the exception is not propagated to handleException)


if i run a test with this code: 

   void "test without "() { 

        when: 'call the action' 
        controller.index() 

        then: "expected a Exception" 
        Exception ex = thrown() 
        assert ex 

    } 

test will fail with this message: Expected exception java.lang.Exception, but no exception
was thrown. this should be the correct behavior. 

bref, if an action use commandObject the handleException is not executed. 
this only in spock testes. in normal application run everything is fine. 



Reported by tamer.shahin on 2014-02-03 09:31:11

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 with the linked spockTest.git reproduction and compare the two controller Spock tests: one calls indexObj with a mocked IndexCommand, while the other calls index directly. Reproduce the differing exception behavior and inspect how the controller's handleException method is reached; done means the command-object case behaves consistently with the direct action case.

Written by the indexing model from the issue text.

Assessment

Tech stack
groovy, java
Domain
backend, testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.