英文:
Cannot cast object '20001' with class 'java.lang.Long' to class 'java.util.Optional'
问题
以下是您提供的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();
}
}
出现错误的部分如下:
<!-- language: lang-none -->
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)
...
请注意,以上内容仅为您提供的代码的翻译部分。如有任何翻译不准确或遗漏的地方,请随时指出。
英文:
I am writing groovy test for service here is my groovy code
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
}
}
Service class is
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();
}
}
getting error like this
<!-- language: lang-none -->
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)
专注分享java语言的经验与见解,让所有开发者获益!
评论