Monday, 15 February 2010

java - Guava LoadingCache: How to unit test ListenableFuture within CacheLoader -



java - Guava LoadingCache: How to unit test ListenableFuture within CacheLoader -

i'm using guava loadingcache this:

@inject private executorservice executorservice; private loadingcache<cachekey, responseentity<string>> constructcache() { homecoming cachebuilder.newbuilder() .maximumsize(100) .expireafterwrite(15, timeunit.minutes) .build( new cacheloader<cachekey, responseentity<string>>() { @override public responseentity<string> load(cachekey cachekey) throws exception { homecoming loadresource(cachekey); } @override public listenablefuture<responseentity<string>> reload(final cachekey cachekey, responseentity<string> old) { listenablefuturetask<responseentity<string>> task = listenablefuturetask.create(new callable<responseentity<string>>() { @override public responseentity<string> call() throws exception { // line here! homecoming loadresource(cachekey); } }); executorservice.execute(task); homecoming task; } } ); } private responseentity<string> loadresource(cachekey cachekey) { // ... // external phone call // ... }

i can test every line except one. how unit test line return loadresource(cachekey) within runnable task? right i'm mocking executorservice in order create sure asynchronous refresh taking place.

instead of mocking executorservice, can utilize samethreadexecutor task executed.

java unit-testing junit mockito guava

No comments:

Post a Comment