Need(le) for Speed - Effective Unit Testing for Java EE

1,313 views

Published on

Writing good concise tests for application components can be quite cumbersome. Especially, when they have a lot of dependencies to other beans and you do not want to manually write mock objects to fulfill all of them. On the other hand, you do not want to run too many slow integration tests.

We still see the need for a lightweight framework that allows to test components in isolation where their dependencies are resolved in an arbitrary manner. Sometimes you might want to use mock objects for dependencies, sometimes you would like to link your component to the real world.

With the Needle framework you can accomplish those goals in very comfortable way. Objects under test will get their dependencies injected automatically. The Needle core is extensible, it allows you to add your own injection points at run time. You are free to supply implementations yourself or rely on the mock objects provided by Needle. Needle will thus minimize the setup code and maximize the speed of a unit test.

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,313
On SlideShare
0
From Embeds
0
Number of Embeds
24
Actions
Shares
0
Downloads
9
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Need(le) for Speed - Effective Unit Testing for Java EE

  1. 1. Logo auf dunklen Hintergrund (z.B. Website) NEEDLE for Java EE Need(le)  for  Speed  –  Effec<ve  Unit  Tes<ng  hellem Hintergrund Logo auf for  Java  EE     Heinz  Wilming,  akquinet  AG  •  Version  2.0   h-p://needle.spree.de/  
  2. 2. Mo<va<on   Why  do  we  Test?   §  Confidence §  Cost  Reduc<on   §  Be-er  Design   §  Documenta<on   §  Less  Debug  Time  •  Version  2.0   h-p://needle.spree.de/  
  3. 3. Unit  tes<ng   NEEDLE for Java EE The  levels  of  tes0ng   Logo auf dunklen Hintergrund (z.B. Website) §  Unit   NEEDLE §  Integra<on   for Java EE §  Acceptance   Logo auf hellem Hintergrund   NEEDLE for Java EE Logo in Graustufen NEEDLE for Java EE Logo in Schwarz/Weiss•  Version  2.0   h-p://needle.spree.de/  
  4. 4. Unit  tes<ng   Run0me  environment  •  Version  2.0   h-p://needle.spree.de/  
  5. 5. Unit  tes<ng   Unit-­‐Test  environment  •  Version  2.0   h-p://needle.spree.de/  
  6. 6. Unit  tes<ng   Unit-­‐Test  environment   OrderDao mock = mock(OrderDao.class); new ShoppingCart();•  Version  2.0   h-p://needle.spree.de/  
  7. 7. Unit  tes<ng   Breaking  the  encapsula0on   public class ShoppingCart { @Inject protected OrderDao orderDao; ... }•  Version  2.0   h-p://needle.spree.de/  
  8. 8. Unit  tes<ng   Breaking  the  encapsula0on   public class ShoppingCart { @Inject private OrderDao orderDao; ...              /*        *  for  unit  test  only        */ protected void setOrderDao(OrderDao dao){ orderDao = dao; } }•  Version  2.0   h-p://needle.spree.de/  
  9. 9. Unit  tes<ng   Using  reflec0on   public class ShoppingCartTest { private ShoppingCart shoppingCart; private OrderDao mock; @Before public void setup() throws Exception { shoppingCart = new ShoppingCartService(); mock = mock(OrderDao.class); Field field = ShoppingCart.class.getDeclaredField("orderDao"); field.setAccessible(true); field.set(shoppingCart, mock); } @Test public void testCheckout() { ... } }•  Version  2.0   h-p://needle.spree.de/  
  10. 10. Need(le)  for  Speed   Effec0ve  Unit  Tes0ng  for  Java  EE   §  out  of  container  tes<ng   §  test  components  in  isola<on   §  reduce  test  setup  code   §  analyze  dependencies     and  provide  mocks   §  Fast  in  development   and  execu<on  •  Version  2.0   h-p://needle.spree.de/  
  11. 11. Need(le)  for  Speed   public class ShoppingCartTest { @Rule public NeedleRule needleRule = new NeedleRule(); @ObjectUnderTest private ShoppingCart shoppingCart; @Test public void testCheckout() { boolean checkout = shoppingCart.checkout(); assertTrue(checkout); } }•  Version  2.0   h-p://needle.spree.de/  
  12. 12. Need(le)  for  Speed   Instan<a<on  of  @ObjectUnderTest   Components     Dependency  Injec<on   §  Field   §  Method   §  Constuctor   Default  are  Mock  Objects  •  Version  2.0   h-p://needle.spree.de/  
  13. 13. Need(le)  for  Speed   Injec0on  Provider   Out-­‐of-­‐the-­‐box   @Inject, @EJB, @Resource, @PersistenceContext, @PersistenceUnit Configura<on  of  addi<onal  Annota<ons   §  e.g.  Seam  2  -­‐  @In,@Logger     Configura<on  of  addi<onal  injec<on  provider   §  e.g. javax.inject.Qualifier•  Version  2.0   h-p://needle.spree.de/  
  14. 14. Need(le)  for  Speed   Mock  injec0on   public class ShoppingCartTest { @Rule public NeedleRule needleRule = new NeedleRule(); @ObjectUnderTest private ShoppingCart shoppingCart; @Inject private OrderDao orderDaoMock; @Test public void testCheckout() { when(orderDaoMock.find(anyLong())).thenReturn(new Order()); boolean checkout = shoppingCart.checkout(); assertTrue(checkout); } }•  Version  2.0   h-p://needle.spree.de/  
  15. 15. Need(le)  for  Speed   Tes0ng  object  graphs   Provide  own  objects     Mul<ple     @ObjectUnderTest   Components   Wiring  complex  object  graph   §  @InjectInto §  @InjectIntoMany  •  Version  2.0   h-p://needle.spree.de/  
  16. 16. Need(le)  for  Speed   Database  tes0ng   via  JPA  Provider,  e.g.  EclipseLink  or  Hibernate       En<tyManager  crea<on  and  injec<on       Op<onal:  Execute  Database  opera<on  on  test  setup   and  teardown     §  Import  SQL  Scripts   §  Dele<ng  test  data  aaer  the  test  execu<on  •  Version  2.0   h-p://needle.spree.de/  
  17. 17. Need(le)  for  Speed   public class OrderDaoTest { @Rule public DatabaseRule dbRule = new DatabaseRule(); @Rule public NeedleRule needleRule = new NeedleRule(dbRule); @ObjectUnderTest private OrderDao orderDao; @Test public void testFind() { Order order = new OrderTestdataBuilder().buildAndSave(); Order orderFromDb = orderDao.find(order.getId()); assertEquals(checkout); } }•  Version  2.0   h-p://needle.spree.de/  
  18. 18. Live  Demo   DEMO  h-p://seam-­‐archetype.sourceforge.net/jbosscc-­‐seam-­‐archetype/1.4/javaee.html     •  Version  2.0   h-p://needle.spree.de/  
  19. 19. Summary   Summary   Fast   Less  Glue  Code   Keep  Dependencies  Private   Flexible  &  Extensible     Developer  Happiness  ;-­‐)  •  Version  2.0   h-p://needle.spree.de/  
  20. 20. Summary   Get  Started  Today!   <dependency> <groupId>de.akquinet.jbosscc</groupId> <artifactId>jbosscc-needle</artifactId> <version>2.1</version> <scope>test</scope> </dependency>•  Version  2.0   h-p://needle.spree.de/  
  21. 21. Links   http://needle.spree.de http://sourceforge.net/projects/jbosscc-needle/ heinz.wilming@akquinet.de http://blog.akquinet.de/•  Version  2.0   h-p://needle.spree.de/  
  22. 22. Logo auf dunklen Hintergrund (z.B. Website) NEEDLE for Java EE Logo auf hellem Hintergrund•  Version  2.0   h-p://needle.spree.de/  

×