-1

我正在为 CardManager.java 类编写单元测试代码,

在执行单元测试代码时,我得到:java.lang.ClassCastException: java.util.LinkedList cannot be cast to java.util.ArrayList

CardManager.java

对于下面的行,我编写了单元测试代码的 when 条件。

ArrayList arr1= (ArrayList) getPanCardUtil().validatePanCard("" , "" , "" , "");

cardManagerTest.java

List<String> abc = new ArrayList<String>();

when(cardManager.validate("" , "" , "" , "").thenReturn(abc);

验证.java

public List<String> validate("" , "" , "" , "");
4

2 回答 2

1

我怀疑这条线

ArrayList arr1= (ArrayList) getPanCardUtil().validatePanCard("" , "" , "" , "");

应该改为

List arr1= getPanCardUtil().validatePanCard("" , "" , "" , "");

在您发布的代码中,没有对 a 的引用,LinkedList也没有对 method 的任何其他引用validatePanCard,但是如果您同时使用两者,则要避免向下转换,并尽可能将所有内容保持在接口级别。

如果您发布更完整的代码,则有可能找出真正的问题。

此外,告诉我们您使用什么模拟框架可能会有所帮助。我怀疑(仅从语法上)它是 Mockito。

于 2012-07-20T11:35:49.793 回答
0

From the code you provide its not clear what the method is cardManager.validate("","","","") is (in cardmanagerTest.java)?

I suspect that you have a method in CardManager.java which implements the method validate of Validate.java

public LinkedList<String> validate(String v, String v2, String v3, String v4) {}

And your test case is failing because it doesnt match the return type of this method, but does match the interface definition in validate.java. You should just return a List type.

public List<String> validate(String v, String v2, String v3, String v4) {}
于 2012-07-20T11:44:35.973 回答