A presentation that guides you through the stages of testing your Java enterprise application. Finally it shows you that Arquillian is the best tool for that
8. @ivan_stefanov
Testing Persistence
Use embedded databases (HSQLDB, Derby)
Covered by other tests, often not needed
Used for quick check of persistence code
11. @ivan_stefanov
3) Begin transaction
@Before
public void setUp() throws Exception {
entityManager.getTransaction().begin();
insertTestData();
entityManager.flush();
this.competitionsService = new CompetitionsService();
competitionsService.entityManager = entityManager;
}
12. @ivan_stefanov
@Test
public void shouldStoreCompetition() throws Exception {
Competition newCompetition = new Competition(
"Premiership 2015/2016", "The English Premier League");
Competition storedCompetition = competitionsService
.storeCompetition(newCompetition);
assertNotNull(storedCompetition.getId());
assertEquals(newCompetition,
entityManager.find(Competition.class,
storedCompetition.getId()));
}
4) Write your test
13. @ivan_stefanov
5) Cleanup
@After
public void tearDown() {
entityManager.getTransaction().rollback();
}
@AfterClass
public static void closeEntityManager() {
entityManager.close();
}
15. @ivan_stefanov
Testing CDI
Use CDI Unit or Deltaspike
Launches CDI container
Easy injection of dependencies, mocks, alternatives
Control of requests and sessions
Used for quick tests when dependencies and
scopes are involved
22. @ivan_stefanov
Core principles
Tests should be portable to any container
Tests should be executable from both IDE and build
tool
The platform should extend existing test frameworks
27. @ivan_stefanov
Step 3 – package and deploy
ShrinkWrap library
◦ Deployment
◦ Resolve from Maven
◦ Create descriptors
28. @ivan_stefanov
@RunWith(Arquillian.class)
public class CompetitionsServiceIntegrationTest {
@Deployment
public static WebArchive createDeployment() {
return ShrinkWrap.create(WebArchive.class)
.addClass(CompetitionsService.class)
.addPackage(Prediction.class.getPackage())
.addAsResource(
new File("src/main/resources/META-INF/persistence.xml"),
"META-INF/persistence.xml");
}
}
3) Prepare the test archive
29. @ivan_stefanov
Step 4 – run the test
Tests runs in-container
◦ CDI, EJB, JNDI available
◦ No need to mock most of the services
Present the result as a normal unit test
30. @ivan_stefanov
@Inject
private CompetitionsService competitionsService;
@Test
public void shouldCreateCompetition() throws Exception {
testCompetition = new Competition("Premiership 2015/2016",
"English Premier League");
testGame = new Game("Manchester City", "Juventus",
LocalDateTime.of(2015, 9, 15, 21, 45));
testCompetition.getGames().add(testGame);
Competition persistedCompetition =
competitionsService.storeCompetition(testCompetition);
assertNotNull(persistedCompetition.getId());
assertEquals(testCompetition, persistedCompetition);
}
4.1) Write the test
31. @ivan_stefanov
@RunWith(Arquillian.class)
@RunAsClient
public class CompetitionResourceTest {
@Test
public void shouldCreateCompetition(@ArquillianResource URL base) {
URL url = new URL(base, "rest/competition");
WebTarget target = ClientBuilder.newClient().target(url.toExternalForm());
Form newCompetitionForm = new Form();
newCompetitionForm.param("name", COMPETITION_NAME);
newCompetitionForm.param("description", DESCRIPTION);
Response response = target.request(MediaType.APPLICATION_JSON_TYPE)
.post(Entity.entity(newCompetitionForm,
MediaType.APPLICATION_FORM_URLENCODED_TYPE));
assertEquals(201, response.getStatus());
}
}
4.2) Client side tests
32. @ivan_stefanov
Step 5 – undeploy the test
Undeploy the test archive
Disconnect or stop the container