aliakhtar / spock

Automatically exported from code.google.com/p/spock
0 stars 0 forks source link

possible bug in grails using spock+controller handleException method #345

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
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. 

Original issue reported on code.google.com by tamer.sh...@gmail.com on 3 Feb 2014 at 9:31

GoogleCodeExporter commented 8 years ago
I do not think this is a Spock issue.  I think this is a problem with incorrect 
expectations being expressed in the test.  See 
http://grails.1312388.n4.nabble.com/possible-bug-using-spock-controller-handleEx
ception-method-td4653717.html.

Original comment by j...@jeffandbetsy.net on 3 Feb 2014 at 4:05