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.
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Need(le) for Speed - Effective Unit Testing for Java EE
1.
2. Logo auf dunklen Hintergrund (z.B. Website)
NEEDLE for Java EE
Effective Unit Testing of Java EE
Logo auf hellem Hintergrund
heinz.wilming@akquinet.de
carsten.erker@akquinet.de
3.
4. Why do we Test?
• Confidence
• Cost Reduction
• Better Design
• Documentation
• Less Debug Time
5. 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
6. 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
7. 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
8. Needle Testcase
• Instantiation of @ObjectUnderTest
Components
• Dependency Injection
– Field
– Method
– Constuctor
• Default are Mock Objects
9. 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
12. Testing an Object Graph
• Provide own objects
• Wiring complex object graph
<ObjectUnderTest>
Authenticator
UserDao
<ObjectUnderTest>
Sercurity
@InjectInto
UserDao
Logging
@InjectIntoMany
<Mock>
<Mock>
<ObjectUnderTest>
Security
13. Testing an Object Graph
public 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
}
14. Mock Objects
public 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();
}
}
15. 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
16. Provides access to the
public 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(...);
}
}
17. Summary
• Fast
• Less Glue Code
• Keep Dependencies Private
• Extensible
• Developer Happiness ;-)
18. Get Started Today!
<dependency>
<groupId>de.akquinet.jbosscc</groupId>
<artifactId>jbosscc-needle</artifactId>
<version>2.0</version>
<scope>test</scope>
</dependency>
19. Logo auf dunklen Hintergrund (z.B. Website)
NEEDLE for Java EE
Logo auf hellem Hintergrund
http://needle.spree.de NEEDLE
http://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/