spock icon indicating copy to clipboard operation
spock copied to clipboard

[Feature Request] Support for reusable mocks

Open ankit-agrawal11 opened this issue 7 years ago • 1 comments

Issue description

I use Spock for testing my Grails applications. Currently, the DetachedMockFactory helps you to create mocks outside Specification, but stubbing is not supported with this feature.

Class FooServiceMocks {
    
    FooService getMockedObject() {
        FooService foo = new DetachedMockFactory().Mock(FooService, name: 'fooService')
        foo.someMethod(_) >> { return 'bar' } // Throws NullPointerException on >> operator
        
        return foo
    }
}

My goal is to define reusable mocks which can be used across multiple test files and override this at places if needed.

Creating a reusable mock is something which is available in many testing libraries. For example, Jest(JavaScript test runner by Facebook) provides a way to define mocks and stub out functions while creating the components.

Quoted from the document:

Manual mocks are defined by writing a module in a mocks/ subdirectory immediately adjacent to the module. For example, to mock a module called user in the models directory, create a file called user.js and put it in the models/mocks directory.

This feature helps the developer to create reusable mocks and inject it across multiple test cases and override them if needed. Here is a sample app which demonstrates the use of this feature.

Is there a way to implement something similar with Spock? The feature would really help with organized code, readability, and maintainability.

ankit-agrawal11 avatar May 17 '18 05:05 ankit-agrawal11

I can't locate the sources from the original plugin, but this looks like it may be it? https://github.com/mattmoss/grails-markdown

Can you confirm it's taglibs, etc are what you were using?

jdaugherty avatar Sep 03 '25 11:09 jdaugherty

hi @jdaugherty , it's the same plugin which we are using

weinellucian avatar Sep 03 '25 12:09 weinellucian

https://docs.grails.org/latest/guide/upgrading.html#_12_1 - all Grails Plugins will require updates to work with Grails 7. The common/primary ones have been completed, but for others try reaching out to the author. There are hundreds of plugins from the last 20 years.

If the author is no longer interested in maintaining the plugin, it could be moved to https://github.com/grails-plugins/ or https://github.com/gpc/, but there needs to be an end user willing to complete the updates and maintain the plugin. The update process takes 1 to a few hours and then going forward maintenance should be minor, unless there were historical issues with the plugin. https://slack.grails.org/ is the best place to coordinate moving plugins to one of those GitHub organizations.

jamesfredley avatar Sep 03 '25 13:09 jamesfredley

https://github.com/apache/grails-core/discussions/15044

jamesfredley avatar Sep 03 '25 14:09 jamesfredley