I've added a new feature to Stubrn: Instead of implementing an anonymous class
with methods or fields as shown
here, you can now pass
a Map<String,Object>
as a holder to stubFor()
.
It will take the key as the method name and the value as the return value of stubbed method. This will look like:
Stubbery stubbery = new Stubbery();
Map<String,Object> map = new HashMap<String,Object>();
map.put("findByName",Lists.newArrayList(new Entity("foo")));
EntityDAO stub = stubbery.stubFor(EntityDAO.class,map);
Controller sud = new Controller();
sud.setEntityDAO(stub);
Assert.assertEquals(sud.listEntities("foo").size(),1);
Yes, i agree, on the first look, this does not appear very concise, but if we assume now, that we have a Groovy Test, then we get something like
// this is Groovy
Stubbery stubbery = new Stubbery();
EntityDAO stub = stubbery.stubFor(EntityDAO.class, [findByName:[new Entity("foo")]]);
Controller sud = new Controller();
sud.setEntityDAO(stub);
Assert.assertEquals(sud.listEntities("foo").size(), 1);
This looks better. And the important fact here is: You can test Java classes
with this approach, hence it might be quite handy. And finally we added
another convenience method to the Stubbery:
stubFor(Class forclass,String methodName,Object returnValue)
which leads us to:
EntityDAO stub = stubbery.stubFor(
EntityDAO.class,"findByName",Lists.newArrayList(new Entity("foo")));
Controller sud = new Controller();
sud.setEntityDAO(stub);
Assert.assertEquals(sud.listEntities("foo").size(),1);
Stubrn lives on Github. You might also like to browse all articles on Stubrn.
1 comment:
Post a Comment