admin管理员组文章数量:1122846
I have a problem with mocking in the spock, my variabla systemTaskOptional is null, test throwing an error. Here is my code of test, and part of code with biznes logic, where is null throwing.
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()))panyAuthority(CompanyAuthoritySnapshot.builder()panyId(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));
}
}
I define service like this:
private TasksMapper tasksMapper = Spy()
private SystemTasksRepository systemTasksRepository = Mock()
private UserApiService userApiService = Mock()
@Subject
private TasksService tasksService = new RepositoryTasksService(tasksMapper, systemTasksRepository, userApiService)
This is stacktrace:
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)
I have a problem with mocking in the spock, my variabla systemTaskOptional is null, test throwing an error. Here is my code of test, and part of code with biznes logic, where is null throwing.
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));
}
}
I define service like this:
private TasksMapper tasksMapper = Spy()
private SystemTasksRepository systemTasksRepository = Mock()
private UserApiService userApiService = Mock()
@Subject
private TasksService tasksService = new RepositoryTasksService(tasksMapper, systemTasksRepository, userApiService)
This is stacktrace:
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)
Share
Improve this question
edited Nov 22, 2024 at 13:20
kriegaex
67.1k15 gold badges119 silver badges221 bronze badges
asked Nov 22, 2024 at 10:29
Karol WolnyKarol Wolny
671 silver badge6 bronze badges
12
|
Show 7 more comments
1 Answer
Reset to default 1Going out on a limb as you didn't provide the actual code where the exception happens, but I can already see that you need to Combine Mocking and Stubbing. Otherwise, your mocks will return null
for those calls.
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())
}
本文标签: javaSpock Mock Not WorkingTest Throws NullPointerExceptionStack Overflow
版权声明:本文标题:java - Spock Mock Not Working, Test Throws NullPointerException - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736304534a1932270.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
tasksService
properly. – talex Commented Nov 22, 2024 at 12:35RepositoryTasksService
, it causes the NPE, possibly due to accessing mock fields or calling mock methods returningnull
. But this is pure speculation. Karol, it would be so much easier for everyone with a minimal reproducer. Like this, the question is only attracting close votes. – kriegaex Commented Nov 23, 2024 at 2:25