无法将类为’java.lang.Long’的对象’20001’转换为类为’java.util.Optional’的对象。

huangapple 未分类评论45阅读模式
英文:

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&lt;SharedList&gt; sharedListDetails
    private static List&lt;User&gt; userDetails
    private static Long sharedListId
    Optional&lt;User&gt; userDetails
    private static String sharedListJsonResponse = &quot;{\&quot;sharedListId\&quot;:\&quot;20001\&quot;,\&quot;sharedListName\&quot;:\&quot;Id Sequence3\&quot;,\&quot;sharedListType\&quot;:\&quot;SharedList\&quot;,\&quot;userId\&quot;:\&quot;\&quot;100001,\&quot;}&quot;
    private static String message=&quot;Successful&quot;

    def void setupSpec(){
        sharedListRepository=Mock()
        sharedListDetails = new ArrayList&lt;&gt;()
        sharedListService = new SharedListService(sharedListRepository)
        sharedListRepository.findById(*_) &gt;&gt; { sharedListId } &gt;&gt; sharedListJsonResponse

    }
def &quot;test sharedList details when sharedListId is not null&quot;() {
        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&lt;User&gt; userDetails) {

        SharedListDTO sharedListDTO = SharedListDTO.builder().build();
        Optional&lt;SharedList&gt; 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 &#39;20001&#39; with class &#39;java.lang.Long&#39; to class &#39;java.util.Optional&#39;
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object &#39;20001&#39; with class &#39;java.lang.Long&#39; to class &#39;java.util.Optional&#39;
	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)

huangapple
  • 本文由 发表于 2020年4月7日 20:21:16
  • 转载请务必保留本文链接:https://java.coder-hub.com/61079918.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定