Details
-
Bug
-
Status: Closed
-
Major
-
Resolution: Not A Problem
-
2.4.10
-
None
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
}
}
```