简体   繁体   English

Mockito-存根超级(抽象)类方法

[英]Mockito - stub super (abstract) class method

I have DAO code that looks like this: 我有看起来像这样的DAO代码:

public abstract class GenericDAO <T, I> {
    public I upsert(T entity){
        //implementation
    }
}

public class MyEntityDAO extends GenericDAO <MyEntity, Integer> {
    public List<MyEntity> searchMyEntity(SearchParameters params){
        //domain specific entity search
    }
}

public class MyEntityService{
    private MyEntityDAO myEntityDAO;

    public Integer saveMyEntity(MyEntityVO vo){
        //transform vo to entity with business logic
        return myEntityDAO.upsert(myEntityInstance);
    }        

}

I want to test the saveMyEntity() method using Mockito but mockito is showing me an error message - "the method when(t) in the type mockito is not applicable for the arguments (void)". 我想使用Mockito测试saveMyEntity()方法,但是嘲笑向我显示了一条错误消息-“嘲笑类型中的when(t)方法不适用于参数(void)”。

This is the code: 这是代码:

Mockito.when(myEntityDAO.upsert(Matchers.any(MyEntity.class)))
        .thenReturn(1);

This is the code that works: 这是有效的代码:

Mockito.when(myEntityDAO.searchMyEntity(testSearchParams))
            .thenReturn(mockedListOfMyEntities);

It seems that because upsert() is in the superclass of MyEntityDAO and not in MyEntityDAO itself this issue is coming up. 似乎因为upsert()在MyEntityDAO的超类中,而不在MyEntityDAO本身中,所以出现了此问题。 Is there any way I can achieve this? 有什么办法可以实现? Is there any workaround or a different framework that supports this? 是否有任何变通办法或其他支持此目的的框架?

Resolved in the comments: 在评论中解决:

Seems like the issue was with the method being updated with void.. had to use Answer to modify the passed object to test the functionality. 似乎问题出在用void更新方法。必须使用Answer来修改传递的对象以测试功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM