java - 无法将具有类“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)
解决方案
推荐阅读
- spring-boot - 如何修复在同一 tomcat 实例上运行的 Spring Boot UI 微服务中的“空白页”
- javascript - 使用谷歌标签管理器将网页浏览量发送到虚拟 URL 到谷歌分析
- c# - 如何以管理员身份运行 CMD,然后在 C# 中使用参数运行 exe
- asp.net-mvc - 如何将视图模型中列表中的一项绑定到控制器?
- r - 将数据框中的多列连接/粘贴在一起
- python - Gremlin Python - 如何从 valueMap 获取 ID
- python - 如何在python中模仿http请求
- r - 如何通过合并一些其他矩阵来填充空矩阵
- bash - bash 在使用 ls 找到文件之前打印 x 个文件
- xpath - 是否可以获取节点名称为文本的子节点?