使用“@CompileStatic”时无法模拟静态 Groovy 方法

发布于 2025-01-13 04:58:53 字数 2124 浏览 1 评论 0原文

我正在尝试模拟一个返回配置值的静态帮助器类(ConfigHelper)。这之前在许多其他测试中都有效,但由于某种原因在我最新的 Spock 测试中失败了。

我正在测试的服务如下:

<redacted>
import grails.plugin.cache.Cacheable
import groovy.transform.CompileStatic

import javax.annotation.PostConstruct

@CompileStatic
class AssetsIntegrationService extends ExternalDiagnosticService {

    static String urlTilErstLogo() {
        return ConfigHelper.hentConfig(ConfigNoegler.ERST_LOGO_URL)
    }

    @Cacheable(value = 'erst-logo')
    String hentErstLogoSomBase64Streng() {
        String url = urlTilErstLogo()  // returns null
        String url2 = ConfigHelper.hentConfig(ConfigNoegler.ERST_LOGO_URL) // returns null
        return getByteArrayFromImageURL(url)
    }

    private static String getByteArrayFromImageURL(String url) {
        URL imageUrl = new URL(url)
        URLConnection ucon = imageUrl.openConnection()
        InputStream is = ucon.getInputStream()
        ByteArrayOutputStream baos = new ByteArrayOutputStream()
        byte[] buffer = new byte[1024]
        int read = 0
        while ((read = is.read(buffer, 0, buffer.length)) != -1) {
            baos.write(buffer, 0, read)
        }
        baos.flush()
        return Base64.getEncoder().encodeToString(baos.toByteArray())
    }
}

我的 spock 测试:

<redacted>
import grails.testing.gorm.DataTest
import grails.testing.services.ServiceUnitTest
import spock.lang.Specification

class AssetsIntegrationServiceSpec extends Specification implements DataTest, ServiceUnitTest<AssetsIntegrationService>{

    def setup() {
        GroovyMock(ConfigHelper, global: true)
        ConfigHelper.hentConfig(ConfigNoegler.ERST_LOGO_URL) >> "https://url.com"
    }

    def "hentErstLogoSomBase64Streng"() {
        when:
            String res = service.hentErstLogoSomBase64Streng()
        then:
            noExceptionThrown()
    }
}

似乎我的模拟被忽略了,因为 ConfigHelper.hentConfig(ConfigNoegler.ERST_LOGO_URL) 在我的服务中只是返回 null 。但是,如果我在服务中设置断点并尝试从 IntelliJ 表达式求值器调用 ConfigHelper.hentConfig(ConfigNoegler.ERST_LOGO_URL),则会返回正确的模拟 url!

I'm trying to mock a static helper class (ConfigHelper) which returns config values. This has worked before in many other tests, but fails in my newest Spock test for some reason.

The service I'm testing is the following:

<redacted>
import grails.plugin.cache.Cacheable
import groovy.transform.CompileStatic

import javax.annotation.PostConstruct

@CompileStatic
class AssetsIntegrationService extends ExternalDiagnosticService {

    static String urlTilErstLogo() {
        return ConfigHelper.hentConfig(ConfigNoegler.ERST_LOGO_URL)
    }

    @Cacheable(value = 'erst-logo')
    String hentErstLogoSomBase64Streng() {
        String url = urlTilErstLogo()  // returns null
        String url2 = ConfigHelper.hentConfig(ConfigNoegler.ERST_LOGO_URL) // returns null
        return getByteArrayFromImageURL(url)
    }

    private static String getByteArrayFromImageURL(String url) {
        URL imageUrl = new URL(url)
        URLConnection ucon = imageUrl.openConnection()
        InputStream is = ucon.getInputStream()
        ByteArrayOutputStream baos = new ByteArrayOutputStream()
        byte[] buffer = new byte[1024]
        int read = 0
        while ((read = is.read(buffer, 0, buffer.length)) != -1) {
            baos.write(buffer, 0, read)
        }
        baos.flush()
        return Base64.getEncoder().encodeToString(baos.toByteArray())
    }
}

And my spock test:

<redacted>
import grails.testing.gorm.DataTest
import grails.testing.services.ServiceUnitTest
import spock.lang.Specification

class AssetsIntegrationServiceSpec extends Specification implements DataTest, ServiceUnitTest<AssetsIntegrationService>{

    def setup() {
        GroovyMock(ConfigHelper, global: true)
        ConfigHelper.hentConfig(ConfigNoegler.ERST_LOGO_URL) >> "https://url.com"
    }

    def "hentErstLogoSomBase64Streng"() {
        when:
            String res = service.hentErstLogoSomBase64Streng()
        then:
            noExceptionThrown()
    }
}

It seems like my mock is ignored, because ConfigHelper.hentConfig(ConfigNoegler.ERST_LOGO_URL) simply returns null in my service. However, if I set a breakpoint in the service and try calling ConfigHelper.hentConfig(ConfigNoegler.ERST_LOGO_URL) from the IntelliJ expression evaluator, the correct mocked url is retured!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

复古式 2025-01-20 04:58:53

该问题是由 @CompileStatic 引起的,只需将其删除,或将其替换为 @TypeChecked 即可。

这与我已经在这里解释过的问题相同: https://stackoverflow.com/a/59481265/2145769

The problem is caused by @CompileStatic just remove it, or replace it by @TypeChecked.

It is the same problem I've explained here already: https://stackoverflow.com/a/59481265/2145769

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文