@PostConstruct 初始化程序在 Spock setup() 方法之前运行,无法及时模拟服务
我有一个服务 (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论