Use Groovy Category implicitly in all instance methods of class

前端 未结 2 1832
我寻月下人不归
我寻月下人不归 2020-12-31 06:38

I have simple Groovy category class which adds method to String instances:

final class SampleCategory {

    static String withBraces(String self) {
                 


        
相关标签:
2条回答
  • 2020-12-31 07:14

    You can use mixin to apply the category directly to String's metaClass. Assign null to the metaClass to reset it to groovy defaults. For example:

    @Before void setUp() { 
        String.mixin(SampleCategory)
    }
    
    @After void tearDown() {
        String.metaClass = null
    }
    
    @Test
    void shouldDoThat() {
        assert 'that'.withBraces() == '(that)'
    }
    
    0 讨论(0)
  • 2020-12-31 07:28

    Now you have the option to use extension modules instead of categories: http://mrhaki.blogspot.se/2013/01/groovy-goodness-adding-extra-methods.html

    On the plus side Intellij will recognize the extensions. I've just noticed that it doesn't even need to be a separate module as suggested by the link, just add META-INF/services/org.codehaus.groovy.runtime.ExtensionModule to the project:

    # File: src/main/resources/META-INF/services/org.codehaus.groovy.runtime.ExtensionModule
    moduleName = module
    moduleVersion = 1.0
    extensionClasses = SampleExtension
    

    The extension class is pretty much defined like a normal category:

    class SampleExtension {
        static String withBraces(String self) {
            "($self)"
        }
    }
    

    Can be used like:

    def "Sample extension"() {
        expect: 'this'.withBraces() == '(this)'
    }
    

    If you are using Spock there is a @Use annotation that can be used on the specifications. The drawback with that is that Intellij will not recognize it.

    0 讨论(0)
提交回复
热议问题