Download - Arquillian : An introduction
![Page 1: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/1.jpg)
Arquillian : An introduction
Testing with real objects Vineet Reynolds
March 2012
![Page 2: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/2.jpg)
![Page 3: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/3.jpg)
![Page 4: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/4.jpg)
![Page 5: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/5.jpg)
![Page 6: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/6.jpg)
![Page 7: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/7.jpg)
![Page 8: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/8.jpg)
![Page 9: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/9.jpg)
![Page 10: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/10.jpg)
![Page 11: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/11.jpg)
![Page 12: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/12.jpg)
WHY ARQUILLIAN? How does this solve our problems?
![Page 13: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/13.jpg)
Test Doubles redux
How did we arrive at the current testing landscape?
![Page 14: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/14.jpg)
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: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/15.jpg)
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: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/16.jpg)
Seems perfect, but…
verify(em, times(1)).persist(user);
• Is brittle
• Violates DRY
• Is not a good use of a Mock
![Page 17: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/17.jpg)
TESTING WITH REAL OBJECTS Implementations matter
![Page 18: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/18.jpg)
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: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/19.jpg)
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: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/20.jpg)
Better, but …
• Requires a separate persistence.xml
• Manual transaction management
• Flushes and clears the persistence context manually
![Page 21: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/21.jpg)
ARQUILLIAN ENTERS THE SCENE Bringing the test as close as possible to production
![Page 22: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/22.jpg)
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: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/23.jpg)
@RunWith(Arquillian.class)
JUnit >= 4.8.1
TestNG > 5.12.1
![Page 24: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/24.jpg)
@Deployment
• Assemble test archives with ShrinkWrap
• Bundles the -
– Class/component to test
– Supporting classes
– Configuration and resource files
– Dependent libraries
![Page 25: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/25.jpg)
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: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/26.jpg)
Test enrichment • Arquillian can enrich test class instances with
dependencies. • Supports:
– @EJB – @Inject – @Resource – @PersistenceContext – @PersistenceUnit – @ArquillianResource – …
![Page 27: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/27.jpg)
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: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/28.jpg)
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: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/29.jpg)
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: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/30.jpg)
Let’s recap @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 31: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/31.jpg)
TESTING THE REPOSITORY - DEMO
Using Arquillian to test the repository
![Page 32: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/32.jpg)
WHAT DID YOU JUST SEE? The stuff that Arquillian does under the hood
![Page 33: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/33.jpg)
![Page 34: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/34.jpg)
![Page 35: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/35.jpg)
![Page 36: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/36.jpg)
![Page 37: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/37.jpg)
![Page 38: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/38.jpg)
![Page 39: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/39.jpg)
![Page 40: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/40.jpg)
WHY SHOULD YOU USE IT? Arquillian changes the way you see tests
![Page 41: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/41.jpg)
![Page 42: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/42.jpg)
![Page 43: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/43.jpg)
![Page 44: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/44.jpg)
![Page 45: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/45.jpg)
![Page 46: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/46.jpg)
![Page 47: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/47.jpg)
![Page 48: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/48.jpg)
![Page 49: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/49.jpg)
![Page 50: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/50.jpg)
THE PERSISTENCE EXTENSION Refining the tests involving a database
![Page 51: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/51.jpg)
THE DRONE AND JBEHAVE EXTENSIONS
ATDD/BDD with Arquillian
![Page 52: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/52.jpg)
![Page 53: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/53.jpg)
![Page 54: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/54.jpg)
![Page 55: Arquillian : An introduction](https://reader034.vdocument.in/reader034/viewer/2022042513/545c6147b1af9f320a8b474f/html5/thumbnails/55.jpg)