Testing
Phoney objects
In this example use case, you have two different test case classes. This division is made for the type of test contained in these classes. Unit tests are in UVAExchangeJdbcDaoImplTests.java and integration ones are in UVAExchangeJdbcDaoImplIntegrationTests.java.
Unit tests
The basic strategy in UVAExchangeJdbcDaoImplTests.java is to use tests doubles for any object interaction with JDBC classes. Those tests doubles are almost everyone, mock doubles.
We decide that Jumahuaca framework could help to do the plumbing in theses doubles if you are in a similar situation where you have to interact with JDBC classes.
To use theses doubles you have two things to do:
1) Add the jumahuaca JdbcExtension as a registered static field (it must be public). Now you will have a lot of methods that will guide you in each crud test.
@RegisterExtension public static final JdbcExtension extension = JdbcExtension.builder().build();2) Encapsulate your JDBC interactions in one interface
2) Add all the other objects to be mocked as a dependency injection provided for Jumahuaca extension, you don't need to understand how it's just magic.
@MockedDatasource
public DataSource ds;
@MockedConnection
public Connection connection;
@MockedPreparedStatement
public PreparedStatement ps;
@SpiedResultSet
public ResultSet rs;
And that 's it, we encourage you to review each test in the example to see how to use the extension.
Regardless of the framework, it's important to highlight that Jumahuaca is a bigger idea and one of the added value is the "how to" test knowledge. It's important to examine the way the tests are written in the example use case.
For those tests, we are using JUnit 5 as test engine, AssertJ for more declarative asserts and inside Jumahuaca Core we are using Mockito for mocking.
Integration tests
We decide to add integration tests because we think the unit tests explained before were important but they use a lot of Mocks to maintain these in a small size so we think we need real database interaction for closing the example project. These integration tests are in UVAExchangeJdbcDaoImplIntegrationTests.java test class.
Similar to the previous one, you have to register an extension and to inject a custom annotated mock:
@RegisterExtension
public static final JdbcIntegrationExtension extension = JdbcIntegrationExtension.builder().build(SCHEMA_FILE_NAME,MODEL_FILE_NAME);
@InMemoryDatasource
public DataSource datasource;
But in this case, we would watch a little bit the extension definition because it has more magic inside.
public class JdbcIntegrationExtension implements TestInstancePostProcessor, BeforeAllCallback, BeforeEachCallback{
This extension implements different interfaces from a test execution life cycle, and with the Jumahuaca implementation you could have integration tests that would not test in a persistent way (you don't want that for speed and reproducibility issues). Jumahuaca will configure for you a memory H2 database with the help of DBUnit framework. You will only need to create two files:
schema.sql: where you create a reduced schema in a SQL script with the tables you want to test.
model.xml: a XML with DBUnit syntax to insert data in your reduced schema.
With these two files and the extension you are ready to go, we make all the DBUnit plumbing and you have to worry about the tests only.
As always, we are using JUnit 5 and AssertJ and we encourage you to read all the tests.
Last updated
Was this helpful?