mercredi 23 septembre 2015

Mocking a class that Verticle depends on

I am playing with the Vert.x 3 framework/library. I have written a simple Verticle that has object dependencies managed via Spring IoC.

Here is the Verticle snippet

public class BookmarksVerticle extends AbstractVerticle {
private static Logger log = LoggerFactory.getLogger(BookmarksVerticle.class);

@Resource
private BookmarkDao bookmarksDao;

Here is the Spring configuration snippet

@Bean 
public BookmarkDao bookmarkDao() {
    ...
}
@Bean 
public BookmarksVerticle bookmarkVerticle() {
     return new BookmarksVerticle();
}   

That is all working great. So wanted to write up some tests. I am using the vertx-unit testing and was trying to mock the DAO

Here is what I have

@RunWith(VertxUnitRunner.class)
public class BookmarksVerticleTest {

    int port = 8888;
    private Vertx vertx;

    @Mock(name = "BookmarkDao")
    BookmarkDao mockDao;
    @InjectMocks
    BookmarksVerticle bmVerticle;

    @Before
    public void init(TestContext context) {
        MockitoAnnotations.initMocks(this);

        vertx = Vertx.vertx();
        DeploymentOptions options = new DeploymentOptions().setConfig(new JsonObject().put("http.port", port));
        vertx.deployVerticle(bmVerticle, options, context.asyncAssertSuccess());
    }

However when I run the test I get NPE

SEVERE: NULL
java.lang.NullPointerException
    at vertx.pragprog.bookmarks.BookmarksVerticle.asynchRetrieveBookmark(BookmarksVerticle.java:169)
    at vertx.pragprog.bookmarks.BookmarksVerticle.lambda$1(BookmarksVerticle.java:88)
    at io.vertx.core.impl.ContextImpl.lambda$executeBlocking$14(ContextImpl.java:279)
    at io.vertx.core.impl.OrderedExecutorFactory$OrderedExecutor.lambda$new$161(OrderedExecutorFactory.java:91)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

The line that triggers the exception is where I access the DAO

        Bookmark bm = bookmarksDao.getBookmark(id);

Any ideas? Thanks

Aucun commentaire:

Enregistrer un commentaire