How Do I write unit tests for a Confluence server plugin with caching?

Hi!

We have a small Confluence plugin that includes a macro that is inserted repeatedly into pages, so I decided to cache the values. I am following this tutorial how-do-i-cache-data-in-a-plugin and it worked as expected.

The project already had unit tests, which now no longer run. Usually we use Mockito to mock up the missing objects that are provided by the Confluence instance (PageManager, EntityManager, etc) and we write Mockito.when().thenReturn() for them.

But now after adding the CacheManager to the implementation, I can't see how to mock these objects up, and I can't simply ignore them from the test...

public BlablaImpl(CacheManager cacheManager) {
    this.cacheManager=cacheManager;
    cache = cacheManager.getCache(
            Blabla.class.getName() + ".cache",
            new BlablaCacheLoader(),
            new CacheSettingsBuilder().expireAfterAccess(24, TimeUnit.HOURS).build());  
}
  private class BlaBlaCacheLoader implements CacheLoader<String, String>{
    @Override
    public String load(@NotNull String thingToBeCached) {
            return getUncachedBlabla(thingToBeCached);
    }
  }
//... calls elsewhere...
return cache.get("bla");

I need to mock cacheManager so the test can call getCache(). CacheManager's constructor depends on an inner CacheLoader class that I cannot even reach from the test (nor turn into an outer class). And I need to mock the cache object so I can call cache.get(), but Cache is a whole interface, that'll take a while. laugh

MockitoAnnotations.initMocks(cacheManager);
cache = new Cache(...); // ??
Mockito.when(cacheManager.getCache(
  Blabla.class.getName() + ".cache",         
  new BlablaImpl().new BlablaCacheLoader(),  // ???
  new CacheSettingsBuilder().build())        
).thenReturn(cache);

Am I missing something obvious here how to write these tests?

1 answer

Maybe the answer is... if I depend on so many objects in a unit test, then it's no longer a unit test, but an integration test. And I should not be using JUnit/Mockito for it...

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Tuesday in Marketplace Apps

If you’re an Atlassian app developer, you’ll want to know about Atlas Camp!

This September 6-7, hundreds of Atlassian App developers will flock to Barcelona Spain to build skills, discover product roadmaps, meet face-to-face with the Atlassian team, and learn how to extend t...

108 views 0 2
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you