I am using Play framework 2.4 and have created a custom wrapper class around a redis hash using the play-redis plugin.
I have an interface:
@ImplementedBy(RedisStoreProvider.class)
public interface IStoreProvider {
long countStore(String storeKey);
}
And its implementation:
public class RedisStoreProvider implements IStoreProvider {
@Inject JedisPool jedisPool;
public long countStore(String storeKey) {
Jedis j = jedisPool.getResource();
try {
return j.hlen(storeKey);
} finally {
jedisPool.returnResource(j);
}
}
}
When I inject the IStoreProvider into a controller it displays fine:
public class Application extends Controller {
@Inject IStoreProvider sp;
public Result index() {
return ok(index.render("Store card: " + sp.countStore("foo")));
}
}
Displays "Store card: 0" (as the redis hash is empty)
Yet when I am trying to test this, the JedisPool is null. I am using the following to test:
public class RedisStoreProviderTest {
@Inject IStoreProvider stProv;
@Test
public void cardStoreTest(){
running(fakeApplication(), () -> {
assertEquals("Expected 0", 0, stProv.cardStore("foo"));
});
}
}
I have tried both with/without the fakeApplication, and both injecting and instantiating the IStoreProvider in the test.
Does Play DI require a injected variable to be in a controller? How can I have unit-tests for classes that require DI?