gpt4 book ai didi

java - PowerMockito 类转换异常

转载 作者:太空宇宙 更新时间:2023-11-04 11:51:44 30 4
gpt4 key购买 nike

我正在使用 PowerMockito 测试静态方法,但不幸的是得到了ClassCastException。注意完全确定我是否没有遵循语法规则。

待测试代码:

List<IClientUserPrefDto> proxyPrefs = (List<IClientUserPrefDto>) DtoUtils.getClientPrefenceByEntityAndAttribute(config.geteSignatureSecurityUser().getUserPreferences(), entity, attribute);

Junit/PowerMock代码

PowerMockito.when( ( List<IClientUserPrefDto>) DtoUtils.getClientPrefenceByEntityAndAttribute( anyListOf( IClientUserPrefDto.class ), any( String.class ), any( String.class ) ) ).thenReturn( list );

堆栈跟踪:

org.mockito.exceptions.misusing.WrongTypeOfReturnValue: 
ArrayList cannot be returned by getClientPrefenceByEntityAndAttribute()
getClientPrefenceByEntityAndAttribute() should return IClientUserPrefDto
***
If you're unsure why you're getting above error read on.
Due to the nature of the syntax above problem might occur because:
1. This exception *might* occur in wrongly written multi-threaded tests.
Please refer to Mockito FAQ on limitations of concurrency testing.
2. A spy is stubbed using when(spy.foo()).then() syntax. It is safer to stub spies -
- with doReturn|Throw() family of methods. More in javadocs for Mockito.spy() method.

at test.com.mercuryinsurance.esignature.integration.tag.TestUIConfigPreferenceTag.testDoStartTag(TestUIConfigPreferenceTag.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:66)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310)
at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:86)
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:94)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:84)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:122)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:106)
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

最佳答案

您可以在 DtoUtils 中创建一个名为 getClientPrefenceByEntityAndAttributeAsList 的新方法。

static List<IClientUserPrefDto> getClientPrefenceByEntityAndAttributeAsList(...) {
return (List<IClientUserPrefDto>) DtoUtils.getClientPrefenceByEntityAndAttribute(...
...
}

然后,你可以像这样 mock 它:

PowerMockito.when(DtoUtils.getClientPrefenceByEntityAndAttributeAsList(anyListOf(IClientUserPrefDto.class),any(String.class),any(String.class))).thenReturn(list);

这是解决该问题的方法。但在这种情况下,方法 when 肯定需要接收一个返回列表的方法。

关于java - PowerMockito 类转换异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41753095/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com