Reputation: 725
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 bookmarksDao() {
...
}
@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);
The mockDao is not being injected into the Verticle.
Any ideas on why this might be the case?
UPDATE:
Tried removing the Mockito automatically creating classes by adding a setter method for the DAO on BookmarksVerticle
and then changed the setup method in the unit test as follows:
@Before
public void setUp(TestContext context) {
log.info("setting up...");
//MockitoAnnotations.initMocks(this);
mockDao = mock(BookmarkDao.class);
bmVerticle = new BookmarksVerticle();
bmVerticle.setBookmarkDao(mockDao);
vertx = Vertx.vertx();
DeploymentOptions options = new DeploymentOptions().setConfig(new JsonObject().put("http.port", port));
vertx.deployVerticle(bmVerticle, options, context.asyncAssertSuccess());
}
Even with this approach I am still getting NPE
UPDATE 2
I removed vertx and the VertxUnitRunner
from the mix by testing a method on BookmarksVerticle
that did not have any dependencies on vertx but used the DAO class.
public class BookmarksServiceTest {
@Mock(name = "BookmarkDao")
BookmarkDao mockDao;
@InjectMocks
BookmarksVerticle bmVerticle;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
public void test_retrieveBookmark() {
String id = "1";
when(mockDao.getBookmark(Matchers.anyString())).thenReturn(new Bookmark(id, "url", "Vert.x"));
Bookmark bm = bmVerticle.retrieveBookmark(id);
assertNotNull(bm);
assertEquals(id, bm.getBookmarkId());
assertEquals("Vert.x", bm.getBookmarkTitle());
}
}
This works great! It seems that the VertxUnitRunner
may be interfering with Mockito in some way.
Thanks
Upvotes: 5
Views: 4421
Reputation: 725
Good news is Mockito works with VertxUnitRunner
!
It turns out using Maven on the command line and from Eclipse was messing me up. Once I switched out the embedded maven to use the same maven install as I was using from the command line things started working.
Here are the details from another answer: Successful build in Maven still showing errors in Eclipse
Upvotes: 2
Reputation: 1626
Does BookmarksVerticle
have non-default constructors?
The documentation for @InjectMocks
states that field injection will not happen in that case.
See here: http://docs.mockito.googlecode.com/hg/1.9.5/org/mockito/InjectMocks.html
Upvotes: 1