need(le) for speed - effective unit testing for java ee

Post on 22-May-2015

991 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

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.

TRANSCRIPT

•  Version  2.0  

Logo auf dunklen Hintergrund (z.B. Website)

Logo auf hellem Hintergrund

Logo in Graustufen

Logo in Schwarz/Weiss

NEEDLEfor Java EE

NEEDLEfor Java EE

NEEDLEfor Java EE

NEEDLEfor Java EE

h-p://needle.spree.de/  

Need(le)  for  Speed  –  Effec<ve  Unit  Tes<ng  for  Java  EE    

Heinz  Wilming,  akquinet  AG  

•  Version  2.0   h-p://needle.spree.de/  

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/  

The  levels  of  tes0ng  §  Unit  §  Integra<on  §  Acceptance    

Logo auf dunklen Hintergrund (z.B. Website)

Logo auf hellem Hintergrund

Logo in Graustufen

Logo in Schwarz/Weiss

NEEDLEfor Java EE

NEEDLEfor Java EE

NEEDLEfor Java EE

NEEDLEfor Java EE

Unit  tes<ng  

•  Version  2.0   h-p://needle.spree.de/  

Run0me  environment  

Unit  tes<ng  

•  Version  2.0   h-p://needle.spree.de/  

Unit-­‐Test  environment  

Unit  tes<ng  

•  Version  2.0   h-p://needle.spree.de/  

Unit-­‐Test  environment  

new ShoppingCart();

OrderDao mock = mock(OrderDao.class);

Unit  tes<ng  

•  Version  2.0   h-p://needle.spree.de/  

public class ShoppingCart {

@Inject protected OrderDao orderDao;

...

}

Breaking  the  encapsula0on  

Unit  tes<ng  

•  Version  2.0   h-p://needle.spree.de/  

public class ShoppingCart {

@Inject private OrderDao orderDao;

...

             /*        *  for  unit  test  only        */

protected void setOrderDao(OrderDao dao){ orderDao = dao; } }

Breaking  the  encapsula0on  

Unit  tes<ng  

•  Version  2.0   h-p://needle.spree.de/  

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() { ... } }

Using  reflec0on  

Unit  tes<ng  

•  Version  2.0   h-p://needle.spree.de/  

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  

Need(le)  for  Speed  

•  Version  2.0   h-p://needle.spree.de/  

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/  

Instan<a<on  of  @ObjectUnderTest  Components    

Dependency  Injec<on  §  Field  §  Method  §  Constuctor  

Default  are  Mock  Objects  

Need(le)  for  Speed  

•  Version  2.0   h-p://needle.spree.de/  

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

Need(le)  for  Speed  

Injec0on  Provider  

•  Version  2.0   h-p://needle.spree.de/  

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/  

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/  

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/  

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/  

Live  Demo  

DEMO  h-p://seam-­‐archetype.sourceforge.net/jbosscc-­‐seam-­‐archetype/1.4/javaee.html    

•  Version  2.0   h-p://needle.spree.de/  

Fast  Less  Glue  Code  Keep  Dependencies  Private  Flexible  &  Extensible    Developer  Happiness  ;-­‐)  

Summary  

Summary  

•  Version  2.0   h-p://needle.spree.de/  

Get  Started  Today!  

<dependency> <groupId>de.akquinet.jbosscc</groupId> <artifactId>jbosscc-needle</artifactId> <version>2.1</version> <scope>test</scope> </dependency>

Summary  

•  Version  2.0   h-p://needle.spree.de/  

http://needle.spree.de

http://sourceforge.net/projects/jbosscc-needle/

heinz.wilming@akquinet.de

http://blog.akquinet.de/

Links  

•  Version  2.0  

Logo auf dunklen Hintergrund (z.B. Website)

Logo auf hellem Hintergrund

Logo in Graustufen

Logo in Schwarz/Weiss

NEEDLEfor Java EE

NEEDLEfor Java EE

NEEDLEfor Java EE

NEEDLEfor Java EE

h-p://needle.spree.de/  

top related