首页 > 解决方案 > 无法将具有类“java.lang.Long”的对象“20001”转换为类“java.util.Optional”

问题描述

我正在为服务编写 groovy 测试,这是我的 groovy 代码

class SharedListServiceSpec extends Specification {

    private static SharedListService sharedListService
    private static SharedListRepository sharedListRepository
    private static Optional<SharedList> sharedListDetails
    private static List<User> userDetails
    private static Long sharedListId
    Optional<User> userDetails
    private static String sharedListJsonResponse = "{\"sharedListId\":\"20001\",\"sharedListName\":\"Id Sequence3\",\"sharedListType\":\"SharedList\",\"userId\":\"\"100001,\"}"
    private static String message="Successful"

    def void setupSpec(){
        sharedListRepository=Mock()
        sharedListDetails = new ArrayList<>()
        sharedListService = new SharedListService(sharedListRepository)
        sharedListRepository.findById(*_) >> { sharedListId } >> sharedListJsonResponse

    }
def "test sharedList details when sharedListId is not null"() {
        given:
       sharedListId=20001l
        when:
        def sharedListResponse=sharedListService.getListDetail(sharedListId,userDetails)
        then:
        sharedListResponse.getResponseMetaData().message==message
    }
}

服务等级是

public class SharedListService implements ISharedListService{
    private final SharedListRepository sharedListRepository;

    @Override
    public SharedListResponse getListDetail(Long sharedListId, Optional<User> userDetails) {

        SharedListDTO sharedListDTO = SharedListDTO.builder().build();
        Optional<SharedList> sharedList = sharedListRepository.findById(sharedListId);
        if(sharedList.isPresent()){
            SharedList list = sharedList.get();
            if(Objects.nonNull(list)) {
                BeanUtils.copyProperties(list, sharedListDTO);
            }
        }
        return SharedListResponse.builder().sharedList(sharedListDTO).build();
    }
}

得到这样的错误

Cannot cast object '20001' with class 'java.lang.Long' to class 'java.util.Optional'
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '20001' with class 'java.lang.Long' to class 'java.util.Optional'
    at org.spockframework.mock.response.CodeResponseGenerator.doRespond(CodeResponseGenerator.java:42)
    at org.spockframework.mock.response.SingleResponseGenerator.respond(SingleResponseGenerator.java:31)
    at org.spockframework.mock.response.ResponseGeneratorChain.respond(ResponseGeneratorChain.java:45)
    at org.spockframework.mock.runtime.MockInteraction.accept(MockInteraction.java:76)
    at org.spockframework.mock.runtime.MockInteractionDecorator.accept(MockInteractionDecorator.java:46)
    at org.spockframework.mock.runtime.InteractionScope$1.accept(InteractionScope.java:41)
    at org.spockframework.mock.runtime.MockController.handle(MockController.java:39)
    at org.spockframework.mock.runtime.JavaMockInterceptor.intercept(JavaMockInterceptor.java:74)
    at org.spockframework.mock.runtime.DynamicProxyMockInterceptorAdapter.invoke(DynamicProxyMockInterceptorAdapter.java:34)
    at com.thermofisher.sharedlist.service.SharedListService.getListDetail(SharedListService.java:29)
    at com.thermofisher.sharedlist.service.SharedListServiceSpec.test sharedList details when sharedListId is not null(SharedListServiceSpec.groovy:40)

标签: javagroovy

解决方案


推荐阅读