doAnswer 和 ClassCastException
doAnswer and ClassCastException
我正在尝试存根方法调用
SpotApi.APIlistCandlesticksRequest req = spotApi.listCandlesticks(coinPair);
到return不同调用的不同值,其中:
@Mock private SpotApi.APIlistCandlesticksRequest
mockAPIlistCandlesticksRequest, mockAPIlistCandlesticksRequest2;
. . .
doAnswer(inv-> new Answer<SpotApi.APIlistCandlesticksRequest>() {
private int count = 0;
@Override
public SpotApi.APIlistCandlesticksRequest answer(InvocationOnMock invocationOnMock) {
count++;
if (count == 1) {
return mockAPIlistCandlesticksRequest;
} else if (count == 2) {
return mockAPIlistCandlesticksRequest2;
}
return null;
}
}).when(mockSpotApi).listCandlesticks(COINPAIR);
但它失败了
: class com.gateiobot.macd.MACDCalculationTest cannot be cast to class io.gate.gateapi.api.SpotApi$APIlistCandlesticksRequest (com.gateiobot.macd.MACDCalculationTest and io.gate.gateapi.api.SpotApi$APIlistCandlesticksRequest are in unnamed module of loader 'app')
非常感谢任何帮助。
问题是您正在制作 Answer
returns 的答案 returns SpotApi.APIlistCandlesticksRequest
。你看,构造 inv -> new Answer() { ...}
是一个返回 Answer
的函数。当作为 doAnswer
的参数给出时,根据 doAnswer
.
的约定,它隐式成为 Answer
功能接口的实现
只保留一个。如果你保留 labda,保持状态(count
)会更尴尬,所以你可以只保留内部 class:
doAnswer(new Answer<SpotApi.APIlistCandlesticksRequest>() { // NO inv ->
private int count = 0;
@Override
public SpotApi.APIlistCandlesticksRequest answer(InvocationOnMock invocationOnMock) {
count++;
if (count == 1) {
return mockAPIlistCandlesticksRequest;
} else if (count == 2) {
return mockAPIlistCandlesticksRequest2;
}
return null;
}
}).when(mockSpotApi).listCandlesticks(COINPAIR);
我正在尝试存根方法调用
SpotApi.APIlistCandlesticksRequest req = spotApi.listCandlesticks(coinPair);
到return不同调用的不同值,其中:
@Mock private SpotApi.APIlistCandlesticksRequest
mockAPIlistCandlesticksRequest, mockAPIlistCandlesticksRequest2;
. . .
doAnswer(inv-> new Answer<SpotApi.APIlistCandlesticksRequest>() {
private int count = 0;
@Override
public SpotApi.APIlistCandlesticksRequest answer(InvocationOnMock invocationOnMock) {
count++;
if (count == 1) {
return mockAPIlistCandlesticksRequest;
} else if (count == 2) {
return mockAPIlistCandlesticksRequest2;
}
return null;
}
}).when(mockSpotApi).listCandlesticks(COINPAIR);
但它失败了
: class com.gateiobot.macd.MACDCalculationTest cannot be cast to class io.gate.gateapi.api.SpotApi$APIlistCandlesticksRequest (com.gateiobot.macd.MACDCalculationTest and io.gate.gateapi.api.SpotApi$APIlistCandlesticksRequest are in unnamed module of loader 'app')
非常感谢任何帮助。
问题是您正在制作 Answer
returns 的答案 returns SpotApi.APIlistCandlesticksRequest
。你看,构造 inv -> new Answer() { ...}
是一个返回 Answer
的函数。当作为 doAnswer
的参数给出时,根据 doAnswer
.
Answer
功能接口的实现
只保留一个。如果你保留 labda,保持状态(count
)会更尴尬,所以你可以只保留内部 class:
doAnswer(new Answer<SpotApi.APIlistCandlesticksRequest>() { // NO inv ->
private int count = 0;
@Override
public SpotApi.APIlistCandlesticksRequest answer(InvocationOnMock invocationOnMock) {
count++;
if (count == 1) {
return mockAPIlistCandlesticksRequest;
} else if (count == 2) {
return mockAPIlistCandlesticksRequest2;
}
return null;
}
}).when(mockSpotApi).listCandlesticks(COINPAIR);