Java Mockito ClassCastException - 无法转换模拟

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28462611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 06:17:09  来源:igfitidea点击:

Mockito ClassCastException - A mock cannot be cast

javamockingmockito

提问by user2844485

I have a method in the class AppleProcessorwhich I would like to test:

我在类中有一个AppleProcessor我想测试的方法:

public void process(Fruit fruit) {
    if(fruit.getType() == Fruit.APPLE) {
        fruitBasket.add(((AppleFruit) fruit).getApple());
    }
    else {
        // do something else
    }
}

Note that Fruit is an interface with the method getType()which AppleFruit implements and also has a getApple()method.

请注意, Fruit 是getType()AppleFruit 实现的方法的接口,并且还有一个getApple()方法。

My test looks like:

我的测试看起来像:

@Mock
FruitBasket fruitBasket;

@Mock
Fruit fruit;

@Mock
AppleFruit apple;

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
}

@Test
public void testAnAppleIsProcessed() {
    AppleProcessor appleProcessor = new AppleProcessoer();
    when(fruit.getType()).thenReturn(Fruit.APPLE);
    when(((AppleFruit) fruit).getApple()).thenReturn(apple);

    appleProcessor.process(fruit);

    verify(fruitBasket).add(isA(Apple.class));
}

However I get the following error:

但是我收到以下错误:

java.lang.ClassCastException: package.fruit.Fruit$$EnhancerByMockitoWithCGLIB$$b8254f54 cannot be cast to package.fruit.AppleFruit

java.lang.ClassCastException: package.fruit.Fruit$$EnhancerByMockitoWithCGLIB$$b8254f54 cannot be cast to package.fruit.AppleFruit

which comes from this line in the test

来自测试中的这一行

when(((AppleFruit) fruit).getApple()).thenReturn(apple);

when(((AppleFruit) fruit).getApple()).thenReturn(apple);

Would anyone know how to resolve this so I can test my code?

有谁知道如何解决这个问题,以便我可以测试我的代码?

采纳答案by JB Nizet

When you say

当你说

@Mock
Fruit fruit;

You tell Mockito: the fruitvariable should be an instance of Fruit. Mockito will dynamically create a class which implements Fruit(this class is Fruit$$EnhancerByMockitoWithCGLIB$$b8254f54), and create an instance of this class. There's no reason for this class to be an instance of AppleFruit, since you didn't tell Mockito that the object had to be of type AppleFruit.

你告诉 Mockito:fruit变量应该是Fruit. Mockito 会动态创建一个实现的类Fruit(这个类是Fruit$$EnhancerByMockitoWithCGLIB$$b8254f54),并创建这个类的一个实例。没有理由让这个类成为 的实例AppleFruit,因为您没有告诉 Mockito 该对象必须是 AppleFruit 类型。

Declare it as AppleFruit, and it will be of type AppleFruit.

将其声明为AppleFruit,它将是 类型AppleFruit

回答by Aninda Bhattacharyya

Your mock object is enhanced by Mockito and it is not same as your class, so you can't type cast.

您的模拟对象由 Mockito 增强,它与您的类不同,因此您不能进行类型转换。

回答by Siba Prasad Tripathy

You can instruct mockito to return an object of subclass type, for a method which returns a super-class object. Then you won't need to tell mockito to cast the object.

对于返回超类对象的方法,您可以指示 mockito 返回子类类型的对象。然后你不需要告诉 mockito 来投射对象。