元类构造函数重写不适用于@CompileStatic注释类中的方法

时间:2019-12-25 07:10:41

标签: java unit-testing groovy metaprogramming spock

当我们使用someClass.metaClass.constructor来表示由@CompileStatic注释的类的方法中可用的任何特定类(例如RESTClient)时,构造函数覆盖将完全无效。

当我们删除@CompileStatic注释时,此方法正常工作。我想念什么吗?

示例代码:

@CompileStatic
class FooClass {

    String getDataFromProvider() {
        String url = "https://www.example.com"
        RESTClient restClient = new RESTClient(url)

        HttpResponseDecorator response = restClient.post([:]) as HttpResponseDecorator
        return response
    }
}

测试用例:

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
    }
}

根据Groovy Lang文档:

  

MockForStubFor不能用于测试静态编译的类,例如使用@CompileStatic的Java类或Groovy类。要存根和/或模拟这些类,可以使用Spock或Java模拟库之一。

预期的行为

在这种情况下,RESTClient的构造函数重写应该在我们的测试用例中起作用,因为我们不想在每个测试用例中都使用第三方API。

实际行为

不幸的是,RESTClient并没有因为@CompileStatic注释而被嘲笑,它每次都会命中API。

环境信息

------------------------------------------------------------
Gradle 3.5
------------------------------------------------------------

Groovy:       2.4.10,
Ant:          Apache Ant(TM) version 1.9.6 compiled on June 29 2015,
JVM:          1.8.0_221 (Oracle Corporation 25.221-b11),
OS:           Mac OS X 10.15.2 x86_64

吉拉:https://issues.apache.org/jira/browse/GROOVY-9353

2 个答案:

答案 0 :(得分:2)

您是正确的@CompileStatic不能与元类操作一起使用。顾名思义,原因是所有这些都在编译时解析和绑定,因此没有元类查找,因此也没有办法覆盖它。

我建议研究IoC /依赖注入,以便您可以将模拟注入到代码中。使用经典单例使您的代码难以测试。

答案 1 :(得分:2)

Leonard Brünings的评论之后:

  

是的,@ CompileStatic将在编译时解析FooClass中RESTClient的构造函数,因此它将不会使用元类在运行时对其进行锁定。如果您想看看它的外观,建议您使用反编译器,例如ByteCode查看器,并查看生成的字节码。

我们针对两种情况反编译了生成的字节码:

使用@CompileStatic

public class FooClass implements GroovyObject {
    public FooClass() {
        MetaClass var1 = this.$getStaticMetaClass();
        this.metaClass = var1;
    }

    public String getDataFromProvider() {
        String url = "https://www.example.com";

        // Directly constructor is getting used
        RESTClient restClient = new RESTClient(url);

        HttpResponseDecorator response = (HttpResponseDecorator)ScriptBytecodeAdapter.asType(restClient.post(ScriptBytecodeAdapter.createMap(new Object[0])), HttpResponseDecorator.class);
        return (String)ShortTypeHandling.castToString(response);
    }
}

没有@CompileStatic

public class FooClass implements GroovyObject {
    public FooClass() {
        CallSite[] var1 = $getCallSiteArray();
        super();
        MetaClass var2 = this.$getStaticMetaClass();
        this.metaClass = var2;
    }

    public String getDataFromProvider() {
        CallSite[] var1 = $getCallSiteArray();
        String url = "https://www.example.com";

        // Here Groovy's metaprogramming is into play instead of directly calling constructor
        RESTClient restClient = (RESTClient)ScriptBytecodeAdapter.castToType(var1[0].callConstructor(RESTClient.class, url), RESTClient.class);

        HttpResponseDecorator response = (HttpResponseDecorator)ScriptBytecodeAdapter.asType(var1[1].call(restClient, ScriptBytecodeAdapter.createMap(new Object[0])), HttpResponseDecorator.class);
        return (String)ShortTypeHandling.castToString(response);
    }
}

因此@Leonard给出的答案是完全正确的。我们错过了这个简单的Java概念。

相关问题