53
A fresh look at Java Enterprise Application testing with Arquillian Vineet Reynolds May 2012

A fresh look at Java Enterprise Application testing with Arquillian

Embed Size (px)

DESCRIPTION

JavaOne India 2012.A talk on using Arquillian to perform in-container testing.

Citation preview

Page 1: A fresh look at Java Enterprise Application testing with Arquillian

A fresh look at Java Enterprise Application testing with Arquillian

Vineet Reynolds

May 2012

Page 2: A fresh look at Java Enterprise Application testing with Arquillian
Page 3: A fresh look at Java Enterprise Application testing with Arquillian
Page 4: A fresh look at Java Enterprise Application testing with Arquillian
Page 5: A fresh look at Java Enterprise Application testing with Arquillian
Page 6: A fresh look at Java Enterprise Application testing with Arquillian
Page 7: A fresh look at Java Enterprise Application testing with Arquillian
Page 8: A fresh look at Java Enterprise Application testing with Arquillian
Page 9: A fresh look at Java Enterprise Application testing with Arquillian
Page 10: A fresh look at Java Enterprise Application testing with Arquillian
Page 11: A fresh look at Java Enterprise Application testing with Arquillian
Page 12: A fresh look at Java Enterprise Application testing with Arquillian

WHY ARQUILLIAN? How does this solve our problems?

Page 13: A fresh look at Java Enterprise Application testing with Arquillian

Test Doubles redux How did we arrive at the current testing landscape?

Page 14: A fresh look at Java Enterprise Application testing with Arquillian

Let’s test a repository @Stateless @Local(UserRepository.class)

public class UserJPARepository implements UserRepository {

@PersistenceContext

private EntityManager em;

public User create(User user) {

em.persist(user);

return user;

}

...

}

Injected by the container. How do we get one in our

tests?

How do we test this?

Page 15: A fresh look at Java Enterprise Application testing with Arquillian

Did you say Mocks? public class MockUserRepositoryTests { … @Before public void injectDependencies() { // Mock em = mock(EntityManager.class);

userRepository = new UserJPARepository(em); } @Test public void testCreateUser() throws Exception { // Setup User user = createTestUser(); // Execute User createdUser = userRepository.create(user);

// Verify verify(em, times(1)).persist(user); assertThat(createdUser, equalTo(user)); …

Page 16: A fresh look at Java Enterprise Application testing with Arquillian

Seems perfect, but…

verify(em, times(1)).persist(user);

• Is brittle

• Violates DRY

• Is not a good use of a Mock

Page 17: A fresh look at Java Enterprise Application testing with Arquillian

TESTING WITH REAL OBJECTS Implementations matter

Page 18: A fresh look at Java Enterprise Application testing with Arquillian

Using a real EntityManager public class RealUserRepositoryTests { static EntityManagerFactory emf; EntityManager em; UserRepository userRepository; @BeforeClass public static void beforeClass() { emf = Persistence.createEntityManagerFactory("jpa-examples"); } @Before public void setup() throws Exception { // Initialize a real EntityManager em = emf.createEntityManager(); userRepository = new UserJPARepository(em); em.getTransaction().begin(); } …

Page 19: A fresh look at Java Enterprise Application testing with Arquillian

Testing with a real EntityManager @Test

public void testCreateUser() throws Exception {

// Setup

User user = createTestUser();

// Execute

User createdUser = userRepository.create(user);

em.flush();

em.clear();

// Verify

User foundUser = em.find(User.class, createdUser.getUserId());

assertThat(foundUser, equalTo(createdUser));

}

Page 20: A fresh look at Java Enterprise Application testing with Arquillian

Better, but …

• Requires a separate persistence.xml

• Manual transaction management

• Flushes and clears the persistence context manually

Page 21: A fresh look at Java Enterprise Application testing with Arquillian

ARQUILLIAN ENTERS THE SCENE Bringing the test as close as possible to production

Page 22: A fresh look at Java Enterprise Application testing with Arquillian

We must walk before we run @RunWith(Arquillian.class) // #1

public class GreeterTest {

@Deployment // #2

public static JavaArchive createDeployment() {

return ShrinkWrap.create(JavaArchive.class)

.addClass(Greeter.class)

.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");

}

@Inject // #3

Greeter greeter;

@Test // #4

public void should_create_greeting() {

assertEquals("Hello, Earthling!",

greeter.greet("Earthling"));

}

}

Use the Arquillian test runner.

Assemble a micro-deployment

A CDI Bean. Managed by the container.

Injected by Arquillian.

Test like you normally do. No mocks.

Just the real thing.

Page 23: A fresh look at Java Enterprise Application testing with Arquillian

@RunWith(Arquillian.class)

JUnit

>= 4.8.1

TestNG

> 5.12.1

Page 24: A fresh look at Java Enterprise Application testing with Arquillian

@Deployment

• Assemble test archives with ShrinkWrap

• Bundles the -

– Class/component to test

– Supporting classes

– Configuration and resource files

– Dependent libraries

Page 25: A fresh look at Java Enterprise Application testing with Arquillian

Revisiting the deployment @Deployment // #1

public static JavaArchive createDeployment() {

return ShrinkWrap.create(JavaArchive.class) // #2

.addClass(Greeter.class) // #3

.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); // #4

}

1. Annotate the method with @Deployment

2. Create a new JavaArchive. This will eventually create a JAR.

3. Add our Class-Under-Test to the archive.

4. Add an empty file named beans.xml to enable CDI.

Page 26: A fresh look at Java Enterprise Application testing with Arquillian

Test enrichment • Arquillian can enrich test class instances with

dependencies. • Supports:

– @EJB – @Inject – @Resource – @PersistenceContext – @PersistenceUnit – @ArquillianResource – …

Page 27: A fresh look at Java Enterprise Application testing with Arquillian

Revisiting dependency injection @Inject

Greeter greeter;

• The CDI BeanManager is used to create a new bean instance.

• Arquillian injects the bean into the test class instance, before running any tests.

Page 28: A fresh look at Java Enterprise Application testing with Arquillian

Writing your tests

• Write assertions as you normally would

– No record-replay-verify model

– Assert as you typically do

– Use real objects in your assertions

Page 29: A fresh look at Java Enterprise Application testing with Arquillian

Running your tests

• Run the tests from your IDE or from your CI server

– Just like you would run unit tests

• Run as JUnit test - Alt+Shift+X, T

• Run as Maven goal - Alt+Shift+X, M

Page 30: A fresh look at Java Enterprise Application testing with Arquillian

TESTING THE REPOSITORY - DEMO

Using Arquillian to test the repository

Page 31: A fresh look at Java Enterprise Application testing with Arquillian

WHAT DID YOU JUST SEE? The stuff that Arquillian does under the hood

Page 32: A fresh look at Java Enterprise Application testing with Arquillian
Page 33: A fresh look at Java Enterprise Application testing with Arquillian
Page 34: A fresh look at Java Enterprise Application testing with Arquillian
Page 35: A fresh look at Java Enterprise Application testing with Arquillian
Page 36: A fresh look at Java Enterprise Application testing with Arquillian
Page 37: A fresh look at Java Enterprise Application testing with Arquillian
Page 38: A fresh look at Java Enterprise Application testing with Arquillian
Page 39: A fresh look at Java Enterprise Application testing with Arquillian

WHY SHOULD YOU USE IT? Arquillian changes the way you see tests

Page 40: A fresh look at Java Enterprise Application testing with Arquillian
Page 41: A fresh look at Java Enterprise Application testing with Arquillian
Page 42: A fresh look at Java Enterprise Application testing with Arquillian
Page 43: A fresh look at Java Enterprise Application testing with Arquillian
Page 44: A fresh look at Java Enterprise Application testing with Arquillian
Page 45: A fresh look at Java Enterprise Application testing with Arquillian
Page 46: A fresh look at Java Enterprise Application testing with Arquillian
Page 47: A fresh look at Java Enterprise Application testing with Arquillian
Page 48: A fresh look at Java Enterprise Application testing with Arquillian

THE PERSISTENCE EXTENSION Refining the tests involving a database

Page 49: A fresh look at Java Enterprise Application testing with Arquillian
Page 50: A fresh look at Java Enterprise Application testing with Arquillian
Page 51: A fresh look at Java Enterprise Application testing with Arquillian

Source Code https://github.com/VineetReynolds/Arquillian-JavaOne-India-2012

Page 52: A fresh look at Java Enterprise Application testing with Arquillian
Page 53: A fresh look at Java Enterprise Application testing with Arquillian