Spock Mock 不工作,测试抛出 NullPointerException

问题描述 投票:0回答:1

我在 spock 中进行模拟时遇到问题,我的 variabla systemTaskOptional 为 null,测试抛出错误。这是我的测试代码,以及带有业务逻辑的部分代码,其中抛出空值。

    def "should get system tasks and check if done - mark as success" () {
        given:
            systemTasksRepository.findByUserIdAndDoneFalse(_) >> getSystemTasksAll()
            userApiService.getById(_) >> UserSnapshot.builder().storeAuthorities(Set.of(StoreAuthoritySnapshot.builder().role(StoreRole.OWNER).build())).companyAuthority(CompanyAuthoritySnapshot.builder().companyId(1L).build()).build()
            systemTasksRepository.findByUserId(_) >> getSystemTasksAll()
            systemTasksRepository.findByUserIdAndByTaskCode(_,_) >> Optional.of(SystemTask.builder().build())
        when:
            List<SystemTaskSnapshot> snapshots = tasksService.getSystemTasksAndCheck(1L)
        then:
            snapshots.size() == 2
            1 * systemTasksRepository.findByUserIdAndByTaskCode(1L, RepositoryTasksService.SystemTaskTypes.CREATE_STORE)
            1 * systemTasksRepository.findByUserIdAndByTaskCode(1L, RepositoryTasksService.SystemTaskTypes.CREATE_COMPANY)
    }

        if (notDoneSystemTasks.contains(SystemTaskTypes.CREATE_COMPANY)) {
            CompanyAuthoritySnapshot authoritySnapshot = userApiService.getById(userId).getCompanyAuthority();
            if (Objects.nonNull(authoritySnapshot) && Objects.nonNull(authoritySnapshot.getCompanyId())) {
                Optional<SystemTask> systemTaskOptional = systemTasksRepository.findByUserIdAndByTaskCode(userId, SystemTaskTypes.CREATE_COMPANY); <- this is null
                systemTaskOptional.ifPresent(systemTask -> systemTask.setDone(true));
            }
        }

我这样定义服务:

private TasksMapper tasksMapper = Spy()
private SystemTasksRepository systemTasksRepository = Mock()
private UserApiService userApiService = Mock()

@Subject
private TasksService tasksService = new RepositoryTasksService(tasksMapper, systemTasksRepository, userApiService)

这是堆栈跟踪:

Connected to the target VM, address: '127.0.0.1:51363', transport: 'socket'
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.codehaus.groovy.vmplugin.v7.Java7$1 (file:/Users/developer/.m2/repository/org/codehaus/groovy/groovy/2.5.8/groovy-2.5.8.jar) to constructor java.lang.invoke.MethodHandles$Lookup(java.lang.Class,int)
WARNING: Please consider reporting this to the maintainers of org.codehaus.groovy.vmplugin.v7.Java7$1
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

java.lang.NullPointerException
    at com.nvt.pupil.tasks.service.service.RepositoryTasksService.checkIfDone(RepositoryTasksService.java:53)
    at com.nvt.pupil.tasks.service.service.RepositoryTasksService.getSystemTasksAndCheck(RepositoryTasksService.java:38)
    at com.nvt.pupil.tasks.service.service.RepositoryTasksServiceSpec.should get system tasks and check if done - mark as success(RepositoryTasksServiceSpec.groovy:65)
java spring groovy spock
1个回答
0
投票

因为您没有提供发生异常的实际代码而冒险,但我已经可以看到您需要结合模拟和存根。否则,您的模拟将为这些调用返回

null

    def "should get system tasks and check if done - mark as success" () {
        given:
            systemTasksRepository.findByUserIdAndDoneFalse(_) >> getSystemTasksAll()
            userApiService.getById(_) >> UserSnapshot.builder().storeAuthorities(Set.of(StoreAuthoritySnapshot.builder().role(StoreRole.OWNER).build())).companyAuthority(CompanyAuthoritySnapshot.builder().companyId(1L).build()).build()
            systemTasksRepository.findByUserId(_) >> getSystemTasksAll()
        when:
            List<SystemTaskSnapshot> snapshots = tasksService.getSystemTasksAndCheck(1L)
        then:
            snapshots.size() == 2
            1 * systemTasksRepository.findByUserIdAndByTaskCode(1L, RepositoryTasksService.SystemTaskTypes.CREATE_STORE) >> Optional.of(SystemTask.builder().build())
            1 * systemTasksRepository.findByUserIdAndByTaskCode(1L, RepositoryTasksService.SystemTaskTypes.CREATE_COMPANY) >> Optional.of(SystemTask.builder().build())
    }
© www.soinside.com 2019 - 2024. All rights reserved.