PowerMockito类强制转换异常

时间:2017-01-19 22:45:52

标签: java junit powermockito

我正在使用static method测试PowerMockito,但遗憾的是获得ClassCastException。请务必确定我是否遵循语法规则。

要测试的代码:

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

的Junit / PowerMockcode

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)

1 个答案:

答案 0 :(得分:0)

您可以在名为static List<IClientUserPrefDto> getClientPrefenceByEntityAndAttributeAsList(...) { return (List<IClientUserPrefDto>) DtoUtils.getClientPrefenceByEntityAndAttribute(... ... } 的{​​{1}}中创建新方法。

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

然后,你可以嘲笑它:

when

这是解决问题的方法。但在这种情况下,方法from django.contrib.gis.db import models from django.utils.translation import ugettext_lazy as _ from mptt.models import MPTTModel, TreeForeignKey class Region(MPTTModel): class Level: L1 = 1 L2 = 2 L3 = 3 L4 = 4 L5 = 5 choices = ( (L1, _("L1")), (L2, _("L2")), (L3, _("L3")), (L4, _("L4")), (L5, _("L5")), ) parent = TreeForeignKey('self', null=True, blank=True, related_name='children') layer = models.IntegerField(_('layer'), choices=Level.choices, db_index=True) code = models.CharField(_("name"), db_index=True, max_length=100) geom = models.MultiPolygonField(_('polygon')) def __str__(self): return self.code 肯定需要接收返回列表的方法。