无邪的四季豆 · 笔试输入C++_c++ ...· 4 天前 · |
任性的跑步鞋 · SqlConnection Class ...· 4 天前 · |
痴情的机器猫 · C# .NET CORE .NET6 ...· 2 天前 · |
才高八斗的洋葱 · 亲测实现:获取日期时间各种格式的:yyyy- ...· 11 小时前 · |
谈吐大方的拐杖 · Jquery(20)JQgrid处理json ...· 5 小时前 · |
睡不着的抽屉 · 「脱光的彭于晏」X「脱缰的姜文」,《邪不压正 ...· 5 月前 · |
胡子拉碴的刺猬 · 伊利前董事长郑俊怀否认与潘刚失联传言有关,希 ...· 1 年前 · |
豪情万千的吐司 · 贾敏恕:服务音乐人,街声的选择是“不赌博” ...· 1 年前 · |
温柔的椰子 · 开局就被动无敌_开局就被动无敌漫画_开局就被 ...· 1 年前 · |
我不明白为什么在这里抛出类强制转换异常:
我有一个以字符串的变量作为参数的方法。
public List<A> getSomething(final String a, final String b, final Date c, final String d, final String e, final String... f) throws MyException {
return valuesFromDB(a, b, c, d, e, f);
}
我已经模拟了方法的实现。
public List<A> getSomethingUnitTest(final String a, final String b, final Date c, final String d, final String e, final String... f) throws MyException {
return valuesFromCSV(a, b, c, d, e, f);
}
用Mockito进行单元测试
@ExtendWith(MockitoExtension.class)
class AlgoTest {
@Test
void testExecute() throws MyException {
when(myRealService.getSomething(anyString(), anyString(), any(Date.class), anyString(), anyString(), any()))
.thenAnswer(i -> myMockedService.getSomethingUnitTest(i.getArgument(0), i.getArgument(1), i.getArgument(2), i.getArgument(3), i.getArgument(4), i.getArgument(5)));
Output output = algo.execute();
assertNotNull(output);
}
当我运行测试时,会引发类强制转换异常
java.lang.ClassCastException: class java.lang.String cannot be cast to class [Ljava.lang.String; (java.lang.String and [Ljava.lang.String; are in module java.base of loader 'bootstrap')
at com.cmystuff.alghorithm.AlgoTest.lambda$2(AlgoTest.java:82)
at org.mockito.internal.stubbing.StubbedInvocationMatcher.answer(StubbedInvocationMatcher.java:40)
at org.mockito.internal.handler.MockHandlerImpl.handle(MockHandlerImpl.java:99)
at org.mockito.internal.handler.NullResultGuardian.handle(NullResultGuardian.java:29)
at org.mockito.internal.handler.InvocationNotifierHandler.handle(InvocationNotifierHandler.java:33)
at org.mockito.internal.creation.bytebuddy.MockMethodInterceptor.doIntercept(MockMethodInterceptor.java:82)
at org.mockito.internal.creation.bytebuddy.MockMethodInterceptor.doIntercept(MockMethodInterceptor.java:56)
at org.mockito.internal.creation.bytebuddy.MockMethodInterceptor$DispatcherDefaultingToRealMethod.interceptSuperCallable(MockMethodInterceptor.java:141)
任何加注:
<String[]> String[] org.mockito.ArgumentMatchers.any()
Matches anything, including nulls and varargs.
getArgument文档:
<String[]> String[] org.mockito.invocation.InvocationOnMock.getArgument(int index)
Returns casted argument at the given index. Can lookup in expanded arguments form getArguments().
This method is preferred over getArgument(int, Class) for readability.
Please readthe documentation of getArgument(int, Class) for an overview of situations whenthat method is preferred over this one.
我也尝试使用anyString(),但是我得到了相同的类强制转换异常
@ExtendWith(MockitoExtension.class)
class AlgoTest {
@Test
void testExecute() throws MyException {
when(myRealService.getSomething(anyString(), anyString(), any(Date.class), anyString(), anyString(), anyString()))
.thenAnswer(i -> myMockedService.getSomethingUnitTest(i.getArgument(0), i.getArgument(1), i.getArgument(2), i.getArgument(3), i.getArgument(4), i.getArgument(5)));
Output output = algo.execute();
assertNotNull(output);
}
这个问题似乎与一个值的varargs有关.如果我为varargs参数传递两个值
@ExtendWith(MockitoExtension.class)
class AlgoTest {
@Test
void testExecute() throws MyException {
when(myRealService.getSomething(anyString(), anyString(), any(Date.class), anyString(), anyString(), anyString(), anyString()))
.thenAnswer(i -> myMockedService.getSomethingUnitTest(i.getArgument(0), i.getArgument(1), i.getArgument(2), i.getArgument(3), i.getArgument(4), i.getArgument(5), i.getArgument(6)));
Output output = algo.execute();
assertNotNull(output);
}
这个会很好的。
发布于 2022-10-12 10:46:25
方法在运行时将varargs表示为以数组为其最后一个参数的方法。在源代码中可以将参数作为单独的值传递这一事实是语法上的一种选择。
代码的问题是
i.getArgument(index)
返回每个单独的值-它不会将varargs分组到数组中。
这与不正确的类型推断结合在一起--如果使用i.getArgument(5)调用i.getArgument(5),编译器不正确地假设它需要转换为String[],因此您的代码相当于:
when(myRealService.getSomething(
anyString(),
anyString(),
any(Date.class),
anyString(),
anyString(),
any())
).thenAnswer(i -> {
String[] varargs = i.getArgument(5); // incorrect, Note that code works fine if you use String -> the compiler knows that you are using varargs syntax
return myMockedService.getSomething(
i.getArgument(0),
i.getArgument(1),
i.getArgument(2),
i.getArgument(3),
i.getArgument(4),
varargs
});
例如,您称之为:
myRealService.getSomething("0", "1", new Date(), "3", "4", "5", "6", "7");
的新数组中。
when(myRealService.getSomething(
anyString(),
anyString(),
any(Date.class),
anyString(),
anyString(),
any())
).thenAnswer(i -> {
String[] varargFromInvocation = Arrays.copyOfRange(i.getArguments(), 5, i.getArguments().length, String[].class);
return myMockedService.getSomething(
i.getArgument(0),
i.getArgument(1),
i.getArgument(2),
i.getArgument(3),
i.getArgument(4),
varargFromInvocation);