简体   繁体   中英

Mockito - stub super (abstract) class method

I have DAO code that looks like this:

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)".

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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