Embracing Jakarta EE 10 not only enable you to write better structured applications, it also enables you to write more testable applications. Correctly leveraging CDI enables integration testing where you can simulate a database error and verify that JMS messages are lost due to a RuntimeException. This session will focus on strategies and techniques for testing message handing, transaction management, security, and data model integrity. You will see how you can check message delivery and error handling, session failover, database versioning, and two-phase commits with JMS and databases. All too often assumptions are made that are proven wrong in production. This session will show you how to take your testing to the next level.
7. Test Types
• Unit Tests
• Integration Tests
• Functional Tests
• End-to-End Tests
• Acceptance Testing
• Smoke Testing
Easily tested with Jakarta EE
* https://www.atlassian.com/continuous-delivery/software-testing/types-of-software-testing
Important to test more than happy
paths.
11. Arquillian Container Interaction
Remote – container resides in a separate JVM
Managed – container is remove but start/stop controlled
Embedded – resides inside same JVM
Different tests require different approaches!
12. Arquillian Run Modes
Where does the test run?
Embedded
• Test application internals
• Default mode – not annotation needed
External
• Test how application is used by clients ex. REST API
• JavaScript browser code
• Test web services, remote EJBs, etc.
• Tests annotated with @RunAsClient
13. Arquillian Test Enrichment
Injection
• @Resource – reference to any JNDI entry
• @EJB – Injects EJB, Local/Remote
• @Inject – CDI Beans
• @PersistenceContext – JPA persistence context
Contexts
• Request and conversation – test method
• Session – test class
• Application – test class
Used in the
tests!
14. ShrinkWrap
API for programmatically building artifacts
Eliminates system/script spaghetti
Artifacts can be test specific ex:
• Include/don’t include web services
• Exclude/replace initialization logic etc.
Supported archives types: JAR, WAR, EAR, RAR
https://github.com/shrinkwrap
15. ShrinkWrap API
ShrinkWrap class is the main entry point
Call ShrinkWrap.create() with one of the following:
• GenericArchive – simplest archive type
• JavaArchive – allows for addition of class/package, and manfiest
entries
• EnterpriseArchive - Java EAR type
• WebArchive - Java WAR type
• ResourceAdapterArchive – Java RAR type
WebArchive myArchive = ShrinkWrap.create(WebArchive.class,”app.jar");
20. What can we do?
Arquillian + Shrinkwrap + JUnit
• Write JUnits that run in a container
• Write JUnits that call code in a container
• Inject CDI beans/EJBs/resources into a JUnit
• Programmatically construct an application (WAR)
• Programmatically generate or specify configuration files
• Include code from test class path
• Test pieces of an application
21. Integration Tests Setup
• Convention – end integration test in “IT”.
• Bind integration tests to Maven verify build phase.
• Profiles for embedded, remove, managed tests.
24. Extending Arquillian
• Testing often requires container setup
• JUnit supports the following:
• @BeforeClass @Before @After @AfterClass
• What about before deploy/undeploy?
27. Leveraging Container Lifecycle
• Payara can be configured via command line.
• Register drivers, connectors, etc.
• Has a programmatic API for running commands (CommandRunner).
• Web configuration console can generate a script.
• More versatile and safer than hand crafted XML config files.
28. CDI
• CDI = Contexts and Dependency Injection
• Introduced in Java EE 6
• Type-safe dependency injection
• Well-defined life-cycle for stateful objects
• Integrated with Unified Expression Language (EL)
• Interceptors
• Events
• Extensible…
29. CDI & Testing
CDI simplifies testing:
• Mocking & error simulation using Alternatives
• Verification via events
• Spy on objects using interceptors
52. JPA Caching
Database
User A
User B
User C
Second Level
Cache
User A
User B
User C
Entity Manager 1
Entity Manager 1
User A
User C
User C
User B
Note: Default for cacheable is TRUE.
53. JPA Caching
Server A
Database
Entity Manger Cache
Server B
Entity Manger Cache
• Same caching issue arises in load balanced environment.
• Scale-up problems – simple environment works.
58. Transactions & Tests
ejb-jar.xml to the rescue!
• Add this test-only ejb-jar to archive using
shrinkit.
• The “purge” method will now be run in a
transaction.
THIS IS custom-
ejb.xml on the test
classpath.
72. Application Security
• EJB Security
• Who can trigger business logic
• Database Security
• Insert/update/select permissions
• Web Security
• Basic/Form base security
Web Security
EJB
Database
85. Strategies
• Arqullian rules!
• Leverage CDI to reduce coupling and enable testing
• Break application into testable blocks
• Split tests into remote and embedded
• One test class per test configuration
• XML config file
• Classes
• Use Interceptors where instrumented classes (Mockito
Spies) cannot be used.
86. Gotchas
• Transaction testing can be tricky.
• Simulating exceptions may not generate expected
rollback.
• MDBs are asynchronous – how long to wait?
• Security testing can be divided into testing roles and
data access via current user.
87. Integration Testing Suggestions
• Testing different application container versions
• Check if a point release will have any change
• “Easily” verify if another container is feasible
• Check impact of newer Java version.
• Test sporadic errors/failures
• Database down
• Multiple concurrent messages
• Bad data load (data dump loaded without Bean Validation)