私はMockitoを使用していて、うまく簡単なことをしたいと思います。どのように私は特定のクラスのvoidメソッドを模擬するのですか?私が試した...voidメソッドを模擬し、それ以外のものはすべてmockitoを使用してそのまま残すことができますか?
CacheService cs = mock(CacheService.class);
when(cs.startCache()).then(PopulateCache.addTestEntriesToCache());
しかし、私は
[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
私の意図はCacheService.startCacheの通常のコードを呼び出すのではなく、あるコンパイルエラーを取得しています、私は、私自身のメソッドを呼び出したいです」 PopulateCache.addTestEntriesToCache() "これどうやってするの?
編集:...与えられた応答パーが、私はモックを実装する私のクラスを編集しようとしたが、モックメソッド(doAnswerは、おそらく)と呼ばれる取得されていない
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
}
感謝、 - デイブ
可能性のある複製http://stackoverflow.com/questions/2276271/how-to-make-mock-to-void-methods-with-mockito – Gray
「ニャニャヤヤ、あなたは空法です!」 – yshavit