Q:

JUnit testing Spymemcached client

I have a class which wraps around spymemcached client and I would like to write some JUnit tests to test if the getValue and addKey methods work or not.
The problem is that the spymemcached server is not reachable from the test server so I guess some mocking is required here.
My simplified class looks something like this:
public class SpyMemcachedClientWrapper {

    private MemcachedClient spyMemcachedClient;

    public SpyMemcachedClientWrapper()
        spyMemcachedClient = new MemcachedClient(new ConnectionFactoryBuilder().build(),
                        AddrUtil.getAddresses("192.168.1.122:11211"));
    }

    public Object getValue(Object key) {
        Object obj = null;
        Future<Object> f = null;

        f = spyMemcachedClient.asyncGet(key);

        try {
            obj = f.get(2000, TimeUnit.MILLISECONDS);
        } catch (TimeoutException ex) {
            f.cancel(false);
        }

        return obj;
    }

    public boolean addKey(Object key, Object value) {
        OperationFuture<Boolean> t = spyMemcachedClient.set(key, 0, value);
        return t.getStatus().isSuccess();
    }
}

Can you please give me some hints or best practice advise how can I write the SpyMemcachedClientWrapperTest for this class? I am thinking that I could use Mockito or EasyMock (Mockito is preferable but if EasyMock is better here, thats fine also) but so far I could not figure out what's the usually preferred way here to write the tests in this scenario.
An example  would be appreciated also.
Many thanks,

A:

I think the problem is that you're asking yourself the wrong question. What are you trying to test? These are ordered from least to most likely.

Are you trying to test that the SpyMemcached library works properly?

If that's true, you shouldn't be testing your wrapper.

Are you trying to test that your classes call the proper methods of SpyMemcachedClientWrapper?

If that's true, you should be testing the other classes, and providing a mock of SpyMemcachedClientWrapper.

Are you trying to test that SpyMemcachedClientWrapper calls the correct methods of MemcachedClient in various scenarios?

First of all, the reason you are struggling to test this is that you are calling new inside the constructor of your wrapper. This is where dependency injection comes into play. In other words, the main conclusion to take away from this answer is:
You don't actually need SpyMemcached to test this behavior.
The behavior has nothing to do with SpyMemcached! All you need is the interface. However, because you call new in the constructor, you can't actually get at this code to provide, say, a mock of MemcachedClient. This article is an absolute, must read for you.
However, I will briefly sum up the conclusions from that article, and how they apply to your situation. You can either:

Not preferred Use PowerMock to override the behavior of the new operator. This way you can replace the creation of the MemcachedClient object with a mock and call verify methods on it.
Preferred Refactor your constructor to use dependency injection, and then simply pass a mock to the wrapper in the constructor. This will allow you to call verify as appropriate.

