Logo auf dunklen Hintergrund (z.B. Website)                            NEEDLE             for Java EEEffective Unit Testin...
Why do we Test?•    Confidence•    Cost Reduction•    Better Design•    Documentation•    Less Debug Time
NEEDLE             for Java EE         The Levels of Testing                      Logo auf dunklen Hintergrund (z.B. Websi...
Need(le) for Speed•  out of container testing•  test components in isolation•  reduce test setup code•  analyze dependenci...
Needle JUnit Rule to instantiate       and initialize the objects under test.Basic Test       Provides access to dependenc...
Needle Testcase•  Instantiation of @ObjectUnderTest   Components•  Dependency Injection  –  Field  –  Method  –  Constucto...
Injection Provider•  Out-of-the-box   –  @Inject, @EJB, @Resource,      @PersistenceContext, @PersistenceUnit•  Configurat...
Custom Injection Provider
Custom Injection Provider1.  NeedleRule2.  needle.propertiescustom.injection.annotations=org.jboss.seam.annot   ations.In,...
Testing an Object Graph•  Provide own objects•  Wiring complex object graph                      <ObjectUnderTest>        ...
Testing an Object Graphpublic class AuthenticatorTest {    @Rule    public NeedleRule needleRule = new NeedleRule();    @O...
Mock Objectspublic class AuthenticatorTest {                                                                    An EasyMoc...
Database Testing•  via JPA Provider, e.g. EclipseLink or   Hibernate•  EntityManager creation and injection•  Transaction ...
Provides access to thepublic class UserDaoTest {                               database via JPA.    @Rule                 ...
Summary•    Fast•    Less Glue Code•    Keep Dependencies Private•    Extensible•    Developer Happiness ;-)
Get Started Today!<dependency>  <groupId>de.akquinet.jbosscc</groupId>  <artifactId>jbosscc-needle</artifactId>  <version>...
Logo auf dunklen Hintergrund (z.B. Website)                                           NEEDLE             for Java EE      ...
Q&A
Need(le) for Speed - Effective Unit Testing for Java EE
Need(le) for Speed - Effective Unit Testing for Java EE
Upcoming SlideShare
Loading in …5
×

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

1,834 views

Published on

Needle is a lightweight framework for testing Java EE components outside of the container in isolation. It reduces the test setup code by analysing dependencies and automatic injection of mock objects. It will thus maximize the speed of development as well as the execution of unit tests.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,834
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
22
Comments
0
Likes
1
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 EEEffective Unit Testing of Java EE Logo auf hellem Hintergrundheinz.wilming@akquinet.decarsten.erker@akquinet.de
  2. 2. Why do we Test?•  Confidence•  Cost Reduction•  Better Design•  Documentation•  Less Debug Time
  3. 3. NEEDLE for Java EE The Levels of Testing Logo auf dunklen Hintergrund (z.B. Website)•  Unit NEEDLE for Java EE•  Integration Logo auf hellem Hintergrund•  Acceptance NEEDLE for Java EE•  Exploratory Logo in Graustufen NEEDLE for Java EE Logo in Schwarz/Weiss
  4. 4. Need(le) for Speed•  out of container testing•  test components in isolation•  reduce test setup code•  analyze dependencies and provide mocks•  fast in development and execution
  5. 5. Needle JUnit Rule to instantiate and initialize the objects under test.Basic Test Provides access to dependencies of the the objects under test initialized object under test instance with injected dependencies
  6. 6. Needle Testcase•  Instantiation of @ObjectUnderTest Components•  Dependency Injection –  Field –  Method –  Constuctor•  Default are Mock Objects
  7. 7. Injection Provider•  Out-of-the-box –  @Inject, @EJB, @Resource, @PersistenceContext, @PersistenceUnit•  Configuration of additional Annotations –  e.g. Seam 2•  Configuration of additional injection provider –  e.g. javax.inject.Qualifier
  8. 8. Custom Injection Provider
  9. 9. Custom Injection Provider1.  NeedleRule2.  needle.propertiescustom.injection.annotations=org.jboss.seam.annot ations.In, org.jboss.seam.annotations.Loggercustom.injection.provider.classes=de.akquinet.jud con.CurrentUserInjectionProvider
  10. 10. Testing an Object Graph•  Provide own objects•  Wiring complex object graph <ObjectUnderTest> Authenticator UserDao <ObjectUnderTest> Sercurity@InjectInto UserDao Logging@InjectIntoMany <Mock> <Mock> <ObjectUnderTest> Security
  11. 11. Testing an Object Graphpublic class AuthenticatorTest { @Rule public NeedleRule needleRule = new NeedleRule(); @ObjectUnderTest private Authenticator authenticator; @ObjectUnderTest @InjectInto(targetComponentId=“authenticator“) private UserDaoBean userDao; <ObjectUnderTest> Authenticator UserDao <ObjectUnderTest> @InjectIntoMany Sercurity Logging UserDao private Security security; @Test <Mock> public void test () throws Exception { <Mock> ... <ObjectUnderTest> } Security}
  12. 12. Mock Objectspublic class AuthenticatorTest { An EasyMock specific @Rule MockProvider. public NeedleRule needleRule = new NeedleRule(); Inherits from EasyMockSupport. @ObjectUnderTest private Authenticator authenticator; private EasyMockProvider mockProvider = needleRule.getMockProvider(); @Test public void test () throws Exception { UserDao userDaoMock = needleRule.getInjectedObject(UserDao.class); EasyMock.expect(userDaoMock.find((String)EasyMock.anyObject())); ... mockProvider.replayAll(); authenticator.authenticate(); Access to injected dependencies. mockProvider.verifyAll(); }}
  13. 13. Database Testing•  via JPA Provider, e.g. EclipseLink or Hibernate•  EntityManager creation and injection•  Transaction Utilities•  Optional: Execute Database operation on test setup and teardown –  Import SQL Scripts –  Deleting test data after the test execution
  14. 14. Provides access to thepublic class UserDaoTest { database via JPA. @Rule Execute DB Operations public DatabaseRule dbRule = new DatabaseRule(); @Rule public NeedleRule needleRule = new NeedleRule(dbRule); @ObjectUnderTest(implementation = UserDaoBean.class) private UserDao userDao; Injection Provider of @Test the EntityManger public void testFind() throws Exception { User user = new UserTestdataBuilder( dbRule.getEntityManager()).withName("kbeck") .buildAndSave(); User userFromDb = userDao.find("kbeck"); Creates and persist an user object assertEquals(...); }}
  15. 15. Summary•  Fast•  Less Glue Code•  Keep Dependencies Private•  Extensible•  Developer Happiness ;-)
  16. 16. Get Started Today!<dependency> <groupId>de.akquinet.jbosscc</groupId> <artifactId>jbosscc-needle</artifactId> <version>2.0</version> <scope>test</scope></dependency>
  17. 17. Logo auf dunklen Hintergrund (z.B. Website) NEEDLE for Java EE Logo auf hellem Hintergrund http://needle.spree.de NEEDLEhttp://sourceforge.net/projects/jbosscc-needle/Java EE for Logo in Graustufen heinz.wilming@akquinet.de NEEDLE carsten.erker@akquinet.de for Java EE Logo in Schwarz/Weiss http://blog.akquinet.de/
  18. 18. Q&A

×