@PostConstruct 初始化程序在 Spock setup() 方法之前运行,无法及时模拟服务

发布于 2025-01-09 13:58:42 字数 3537 浏览 0 评论 0原文

我有一个服务 (A),我正在为其编写 Spock 单元测试。此服务使用 init() 方法,用 @PostConstruct 注释,该方法调用另一个服务 (B) 方法。

我的问题是 init() 方法似乎在 Spock setup() 方法之前被调用,所以我从 NullPointerException 得到一个 NullPointerException >init() 因为我的服务 B 是 null (尚未模拟)。我该如何解决这个问题?

要测试的服务 A:

class MasseJournaliseringService {

    BeskedModtagerService beskedModtagerService
    SagslagerIntegrationService sagslagerIntegrationService
    ExecutorService executorService

    private static String s9identity = ConfigHelper.hentConfig(ConfigNoegler.MASSEUDSENDELSESMODUL_FAGSYSTEM_ENTITETSID) as String
    private Principal udfoertAf
    List<Future<String>> futures = []

    @PostConstruct
    void init() {
        udfoertAf = sagslagerIntegrationService.opretPrincipal(s9identity) // NullPointerException!!
        int corePoolSize = ConfigHelper.hentConfig(ConfigNoegler.MSG_BROKER_MU_JOURNALISER_COREPOOLSIZE) as int
        int maxPoolSize = ConfigHelper.hentConfig(ConfigNoegler.MSG_BROKER_MU_JOURNALISER_POOLSIZE) as int
        int queueCapacity = ConfigHelper.hentConfig(ConfigNoegler.MSG_BROKER_MU_JOURNALISER_QUEUE_CAPACITY) as int
        int keepAliveTime = ConfigHelper.hentConfig(ConfigNoegler.MSG_BROKER_MU_JOURNALISER_KEEPALIVETIME) as int
        this.executorService = new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveTime, TimeUnit.SECONDS,
                new LinkedBlockingQueue<Runnable>(queueCapacity),
                new CustomizableThreadFactory(threadNamePrefix: 'mu-journ-'))
    }
}

Spock 单元测试:

package beskedfordeler.core.service.broker

import beskedfordeler.core.configuration.ConfigHelper
import beskedfordeler.core.configuration.ConfigNoegler
import beskedfordeler.core.helpers.MasseudsendelseLogHelper
import beskedfordeler.core.service.domain.BeskedModtagerService
import beskedfordeler.core.service.integration.SagslagerIntegrationService
import grails.testing.services.ServiceUnitTest
import org.grails.spring.beans.factory.InstanceFactoryBean
import spock.lang.Specification

import java.util.concurrent.ExecutionException
import java.util.concurrent.Future
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeoutException

class MasseJournaliseringServiceSpec extends Specification implements ServiceUnitTest<MasseJournaliseringService> {

    def setup() {
        GroovyMock(ConfigHelper, global: true)
        ConfigHelper.hentConfig(ConfigNoegler.BF_FAGSYSTEM_ENTITETSID) >> "FAGSYSTEM"
        ConfigHelper.hentConfig(ConfigNoegler.MYNDIGHED_ERST) >> "MYNDIGHED"
        ConfigHelper.hentConfig(ConfigNoegler.MASSEUDSENDELSESMODUL_FAGSYSTEM_ENTITETSID) >> "FAGSYSTEM_ID"
        service.sagslagerIntegrationService = Mock(SagslagerIntegrationService) // BeanCreationException caused by NullPointerException
        service.beskedModtagerService = Mock(BeskedModtagerService)
        ConfigHelper.hentConfig(ConfigNoegler.MSG_BROKER_MU_JOURNALISER_COREPOOLSIZE) >> 5
        ConfigHelper.hentConfig(ConfigNoegler.MSG_BROKER_MU_JOURNALISER_POOLSIZE) >> 5
        ConfigHelper.hentConfig(ConfigNoegler.MSG_BROKER_MU_JOURNALISER_QUEUE_CAPACITY) >> 10
        ConfigHelper.hentConfig(ConfigNoegler.MSG_BROKER_MU_JOURNALISER_KEEPALIVETIME) >> 60
    }

    // Test cases...
}

I have a service (A) that I'm writing a Spock unit-test for. This service uses an init()-method, annotated with @PostConstruct which calls another service (B) method.

My problem is that the init() method seems to be called before the Spock setup() method, so I get a NullPointerException from init() as my service B is null (not mocked yet). How can I solve this?

Service A to be tested:

class MasseJournaliseringService {

    BeskedModtagerService beskedModtagerService
    SagslagerIntegrationService sagslagerIntegrationService
    ExecutorService executorService

    private static String s9identity = ConfigHelper.hentConfig(ConfigNoegler.MASSEUDSENDELSESMODUL_FAGSYSTEM_ENTITETSID) as String
    private Principal udfoertAf
    List<Future<String>> futures = []

    @PostConstruct
    void init() {
        udfoertAf = sagslagerIntegrationService.opretPrincipal(s9identity) // NullPointerException!!
        int corePoolSize = ConfigHelper.hentConfig(ConfigNoegler.MSG_BROKER_MU_JOURNALISER_COREPOOLSIZE) as int
        int maxPoolSize = ConfigHelper.hentConfig(ConfigNoegler.MSG_BROKER_MU_JOURNALISER_POOLSIZE) as int
        int queueCapacity = ConfigHelper.hentConfig(ConfigNoegler.MSG_BROKER_MU_JOURNALISER_QUEUE_CAPACITY) as int
        int keepAliveTime = ConfigHelper.hentConfig(ConfigNoegler.MSG_BROKER_MU_JOURNALISER_KEEPALIVETIME) as int
        this.executorService = new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveTime, TimeUnit.SECONDS,
                new LinkedBlockingQueue<Runnable>(queueCapacity),
                new CustomizableThreadFactory(threadNamePrefix: 'mu-journ-'))
    }
}

Spock unit-test:

package beskedfordeler.core.service.broker

import beskedfordeler.core.configuration.ConfigHelper
import beskedfordeler.core.configuration.ConfigNoegler
import beskedfordeler.core.helpers.MasseudsendelseLogHelper
import beskedfordeler.core.service.domain.BeskedModtagerService
import beskedfordeler.core.service.integration.SagslagerIntegrationService
import grails.testing.services.ServiceUnitTest
import org.grails.spring.beans.factory.InstanceFactoryBean
import spock.lang.Specification

import java.util.concurrent.ExecutionException
import java.util.concurrent.Future
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeoutException

class MasseJournaliseringServiceSpec extends Specification implements ServiceUnitTest<MasseJournaliseringService> {

    def setup() {
        GroovyMock(ConfigHelper, global: true)
        ConfigHelper.hentConfig(ConfigNoegler.BF_FAGSYSTEM_ENTITETSID) >> "FAGSYSTEM"
        ConfigHelper.hentConfig(ConfigNoegler.MYNDIGHED_ERST) >> "MYNDIGHED"
        ConfigHelper.hentConfig(ConfigNoegler.MASSEUDSENDELSESMODUL_FAGSYSTEM_ENTITETSID) >> "FAGSYSTEM_ID"
        service.sagslagerIntegrationService = Mock(SagslagerIntegrationService) // BeanCreationException caused by NullPointerException
        service.beskedModtagerService = Mock(BeskedModtagerService)
        ConfigHelper.hentConfig(ConfigNoegler.MSG_BROKER_MU_JOURNALISER_COREPOOLSIZE) >> 5
        ConfigHelper.hentConfig(ConfigNoegler.MSG_BROKER_MU_JOURNALISER_POOLSIZE) >> 5
        ConfigHelper.hentConfig(ConfigNoegler.MSG_BROKER_MU_JOURNALISER_QUEUE_CAPACITY) >> 10
        ConfigHelper.hentConfig(ConfigNoegler.MSG_BROKER_MU_JOURNALISER_KEEPALIVETIME) >> 60
    }

    // Test cases...
}

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文