effective unit testing of java ee - jbossan easymock specific mockprovider. inherits from...

18

Upload: others

Post on 26-May-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Effective Unit Testing of Java EE - JBossAn EasyMock specific MockProvider. Inherits from EasyMockSupport. Access to injecting dependencies with the key of the injection provider
Page 2: Effective Unit Testing of Java EE - JBossAn EasyMock specific MockProvider. Inherits from EasyMockSupport. Access to injecting dependencies with the key of the injection provider

Effective Unit Testing of Java EE

Heinz WilmingCarsten Erker

Page 3: Effective Unit Testing of Java EE - JBossAn EasyMock specific MockProvider. Inherits from EasyMockSupport. Access to injecting dependencies with the key of the injection provider

Why do we Test?

• Confidence• Cost Reduction• Better Design• Documentation• Less Debug Time

Page 4: Effective Unit Testing of Java EE - JBossAn EasyMock specific MockProvider. Inherits from EasyMockSupport. Access to injecting dependencies with the key of the injection provider

The Levels of Testing

• Unit• Integration• Acceptance• Exploratory

Page 5: Effective Unit Testing of Java EE - JBossAn EasyMock specific MockProvider. Inherits from EasyMockSupport. Access to injecting dependencies with the key of the injection provider

Need(le) for Speed

• out of container testing• test components in isolation

• reduce test setup code• analyze dependencies

and provide mocks

• fast in development andexecution

Page 6: Effective Unit Testing of Java EE - JBossAn EasyMock specific MockProvider. Inherits from EasyMockSupport. Access to injecting dependencies with the key of the injection provider

Basic TestNeedle JUnit Rule to instantiate

and initialize the objects under test.

Provides access to dependenciesof the the objects under test

initialized object under testinstance with injected

dependencies

Page 7: Effective Unit Testing of Java EE - JBossAn EasyMock specific MockProvider. Inherits from EasyMockSupport. Access to injecting dependencies with the key of the injection provider

Needle Testcase

• Instantiation of @ O b j e c t U n d e r T e s t

Components

• Dependency Injection– Field– Method– Constuctor

• Default are Mock Objects

Page 8: Effective Unit Testing of Java EE - JBossAn EasyMock specific MockProvider. Inherits from EasyMockSupport. Access to injecting dependencies with the key of the injection provider

Injection Provider

• Out of the box injection for– @ I n j e c t , @ E J B , @ R e s o u r c e ,

@ P e r s i s t e n c e C o n t e x t , @ P e r s i s t e n c e U n i t

• Configuration of additional Annotations– e.g. Seam 2 injection

• Configuration of addtional injection provider– e.g. j a v a x . i n j e c t . Q u a l i fi e r

Page 9: Effective Unit Testing of Java EE - JBossAn EasyMock specific MockProvider. Inherits from EasyMockSupport. Access to injecting dependencies with the key of the injection provider

Custom Injection Provider

Page 10: Effective Unit Testing of Java EE - JBossAn EasyMock specific MockProvider. Inherits from EasyMockSupport. Access to injecting dependencies with the key of the injection provider

Custom Injection Provider

1. NeedleRule

2. needle.propertiesc u s t o m . i n j e c t i o n . a n n o t a t i o n s = o r g . j b o s s . s e a m . a n n o t a t i o n s . I n ,

o r g . j b o s s . s e a m . a n n o t a t i o n s . L o g g e r

c u s t o m . i n j e c t i o n . p r o v i d e r . c l a s s e s = d e . a k q u i n e t . j u d c o n . C u r r e n t

U s e r I n j e c t i o n P r o v i d e r

Page 11: Effective Unit Testing of Java EE - JBossAn EasyMock specific MockProvider. Inherits from EasyMockSupport. Access to injecting dependencies with the key of the injection provider

Testing an Object Graph

• Provide own objects• Wiring complex object graph

@ I n j e c t I n t o

@ I n j e c t I n t o M a n y

<ObjectUnderTest>UserDao

UserDaoSercuri tyLogging

<ObjectUnderTest>Authenticator

<ObjectUnderTest>S ecurit y

<Mock><Mock>

Page 12: Effective Unit Testing of Java EE - JBossAn EasyMock specific MockProvider. Inherits from EasyMockSupport. Access to injecting dependencies with the key of the injection provider

Testing an Object Graph

<ObjectUnderTest>UserDao

UserDaoSercuri tyLogging

<ObjectUnderTest>Authenticator

<ObjectUnderTest>S ecurit y

<Mock><Mock>

Page 13: Effective Unit Testing of Java EE - JBossAn EasyMock specific MockProvider. Inherits from EasyMockSupport. Access to injecting dependencies with the key of the injection provider

Mock Objects An EasyMock specific

MockProvider.Inherits from EasyMockSupport.

Access to injectingdependencies with the key

of the injection provider.

Page 14: Effective Unit Testing of Java EE - JBossAn EasyMock specific MockProvider. Inherits from EasyMockSupport. Access to injecting dependencies with the key of the injection provider

Database Testing

• via JPA Provider, e.g. EclipseLink orHibernate

• EntityManager creation and injection• Transaction Utilities

• Optional: Execute Database operationon test setup and teardown– Import SQL Scripts– Deleting test data after the test execution

Page 15: Effective Unit Testing of Java EE - JBossAn EasyMock specific MockProvider. Inherits from EasyMockSupport. Access to injecting dependencies with the key of the injection provider

Provides access to thedatabase via JPA.

Execute DB Operations

Injection Provider ofthe EntityManger

Creates andpersists an user

object

Page 16: Effective Unit Testing of Java EE - JBossAn EasyMock specific MockProvider. Inherits from EasyMockSupport. Access to injecting dependencies with the key of the injection provider

Summary

• Fast• Less Glue Code• Keep Dependencies Private• Extensible• Developer Happiness ;-)

Page 17: Effective Unit Testing of Java EE - JBossAn EasyMock specific MockProvider. Inherits from EasyMockSupport. Access to injecting dependencies with the key of the injection provider

Q&A

Page 18: Effective Unit Testing of Java EE - JBossAn EasyMock specific MockProvider. Inherits from EasyMockSupport. Access to injecting dependencies with the key of the injection provider

[email protected]@akquinet.de

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

http://blog.akquinet.de/