使用“@CompileStatic”时无法模拟静态 Groovy 方法
我正在尝试模拟一个返回配置值的静态帮助器类(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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该问题是由
@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