2

In Grails 3.2.x and earlier I could do something like this in a spock unit test:

def myServiceMock = Mock(MyService) {
    someMethod() >> 42
}

Closure doWithSpring() {{ ->
    myService(InstanceFactoryBean, myServiceMock, MyService)
}}


def "some test"(){
    expect:
    service.myService.someMethod() == 42
}

This would enable the mock to be injected in collaborating classes.

See: http://docs.grails.org/3.2.4/guide/testing.html Under the "doWithSpring and doWithConfig callback methods, FreshRuntime annotation" section.

In Grails 3.3.2 it does not seem to work anymore. And the mention of it has been removed from the testing documentation.

Is there any way to accomplished that behaviour again?

Many thanks in advance!

/brian

brianjohnsen
  • 145
  • 8
  • Does your test class implement one of `GrailsUnitTest` traits? Because it is the in `GrailsUnitTest` that the wiring of existing beans takes place. – sbglasius Mar 08 '18 at 12:42

1 Answers1

4

Grails 3.3 comes with new testing framework.

Here you can find docs - https://testing.grails.org/latest/guide/index.html

To run test on grails 3.3. you can modify your code in this way:

def myServiceMock = Mock(MyService) {
    someMethod() >> 42
}

def setup() {
        defineBeans{
            myService(InstanceFactoryBean, myServiceMock, MyService)
        }
    }
Evgeny Smirnov
  • 2,886
  • 1
  • 12
  • 22