Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-9353

Metaclass constructor overriding not working for a method inside of @CompileStatic annotated class

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Not A Problem
    • Affects Version/s: 2.4.10
    • Fix Version/s: None
    • Component/s: groovy-jdk
    • Environment:
    • Flags:
      Important
    • Docs Text:
      Hide
      According to the Groovy Lang doc:
      ```
      MockFor and StubFor can not be used to test statically compiled classes e.g for Java classes or Groovy classes that make use of @CompileStatic. To stub and/or mock these classes you can use Spock or one of the Java mocking libraries.
      ```
      Show
      According to the Groovy Lang doc: ``` MockFor and StubFor can not be used to test statically compiled classes e.g for Java classes or Groovy classes that make use of @CompileStatic. To stub and/or mock these classes you can use Spock or one of the Java mocking libraries. ```

      Description

      When we use `someClass.metaClass.constructor for any specific class (like RESTClient) available inside the method of a class which is annotated with @CompileStatic, constructor overriding is not working at all.

      When we removed the `@CompileStatic` annotation, this works properly. Am I missing something?

       

      Sample Code:

       

      ```

      @CompileStatic

      class FooClass {

       String getDataFromProvider() {

              String url = "https://www.example.com"

                         RESTClient restClient = new RESTClient(url)         

                        HttpResponseDecorator response = restClient.post([:]as HttpResponseDecorator  

              return response

                }

      }
      ```

      And the test case:

      ```
       
      import groovyx.net.http.HttpResponseDecorator

      import groovyx.net.http.RESTClient

      import spock.lang.Specification

      class FooContentSpec extends Specification {

           void "test getDataFromProvider method"() {

              given: "Rest url"

              String restURL = "https://www.example.com"

              and: "Mock RESTClient"

              RESTClient mockedRestClient = Mock(RESTClient)

              // THIS IS NOT WORKING

              RESTClient.metaClass.constructor = { Object url ->

                assert restURL == url

                return mockedRestClient

                        }

              mockedRestClient.metaClass.post = { Map<String, ?> args ->                        return ""

             }

              when: "We hit the method"

              HttpResponseDecorator response = Content.getDataFromProvider()

                         then: "We should get status 200" 

              response.statusCode==200

           }

      }
      ```

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              viplavsoni Viplav Soni
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: