3

I'm using Mockito and want to do a hopefully simple thing. How do I mock a void method for a particular class? I tried ...

    CacheService cs = mock(CacheService.class);
    when(cs.startCache()).then( PopulateCache.addTestEntriesToCache() );

But I'm getting the compile error

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:testCompile (default-testCompile) on project cme-productplus-web: Compilation failure: Compilation failure:
[ERROR] \Documents and Settings\E18538\workspace\cme-productplus-web\src\test\java\com\cme\clearing\product\server\PopulateCacheServiceImpl.java:[32,65] 'void' type not allowed here
[ERROR] \Documents and Settings\E18538\workspace\cme-productplus-web\src\test\java\com\cme\clearing\product\server\PopulateCacheServiceImpl.java:[32,20] 'void' type not allowed here

My intention is instead of calling the normal code of CacheService.startCache, I want to call my own method, "PopulateCache.addTestEntriesToCache()". How can I do this?

Edit: Per the response given, I tried editing my class where I implement the mock, but the mock method (the doAnswer, presumably) isn't getting called ...

public class PopulateCacheServiceImpl extends RemoteServiceServlet implements PopulateCacheService {

/**
 * 
 */
private static final long serialVersionUID = 1L;

public Boolean initCache() { 
    boolean ret = false;
    try {
        setupMockCache();
        CacheService.getInstance().startCache();
        ret = true;
    } catch (Exception e) {
        e.printStackTrace(System.err);
        ret = false;
    }   // try
    return ret;
}   // initCache

private void setupMockCache() { 
    CacheService cs = mock(CacheService.class);
    try {
        doAnswer(new Answer<Object>() {
            public Object answer(InvocationOnMock invocation) throws Throwable {
                PopulateCache.addTestEntriesToCache();
                return null;  
            }
        }).when(cs).startCache();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}   // setupMockCache 

}

Thanks, - Dave

Dave
  • 15,639
  • 133
  • 442
  • 830

3 Answers3

3

You are making a mock for the CacheService, but you are still not returning it and using it anywhere. Instead, you are calling the real static CacheService.instance() method which will not return your mock. Make you setupMockCache() return the CacheService and use it directly rather than going through the instance() method.

Also in the question title/summary, you said "leave everything else the same". If you mean you want the rest of CacheService to behave the same as it normaly would, then perhaps you want a partial mock, which you can do with Mockito's spy() instead of mock().

Kevin Welker
  • 7,719
  • 1
  • 40
  • 56
2

Put the call to your cache in the anwser-method of this http://docs.mockito.googlecode.com/hg/latest/org/mockito/Mockito.html#12

    Mockito.doAnswer(new Answer<Object>() {
        public Object answer(InvocationOnMock invocation) throws Throwable {
            PopulateCache.addTestEntriesToCache()
            return null;  
        }
    }).when(cs).startCache();
ollins
  • 1,851
  • 12
  • 16
  • Thanks for this answer, but even with this code, the void method I'm trying to mock isn't getting called, as I verified through debugging. Is there anything else I'm missing? I included my code above. – Dave Feb 10 '12 at 22:24
0

Of course it doesn't work : in setupMockCache you are creating a the cache mock CacheService cs = mock(CacheService.class); on which you define the stub. But the cs instance is never passed. And in initCache you are calling the setup method, but you don't get the CacheService instance, right after you wrote this statement CacheService.getInstance().startCache(); that will certainly create a real CacheService instance and fo course it won't use the mocked instance.

I don't know what you want to do, this seems weird and wrong to mock partially a Cache in your production code! If I were you I would create my own set of classes that will return your custom cache backed by an inherited CacheService class if necessary (this class will explicitly overide the startCache method).

Hope that helps!

bric3
  • 40,072
  • 9
  • 91
  • 111