5.7.3.2. Middleware Integration Tests

At the middle tier, you can create integration tests which are run in a fully functional Spring container connected to the database. In such tests you can run code from any layer of the Middleware, from services to ORM.

To create integration tests, the application’s core module should contain a base class derived from CubaTestCase. This class should override the methods for data access initialization and configuration files list retrieval. For example:

public class SalesTestCase extends CubaTestCase {

    @Override
    protected void initDataSources() throws Exception {
        Class.forName("org.postgresql.Driver");
        TestDataSource ds = new TestDataSource("jdbc:postgresql://localhost/sales_test", "cuba", "cuba");
        TestContext.getInstance().bind("java:comp/env/jdbc/CubaDS", ds);
    }

    @Override
    protected List<String> getTestAppProperties() {
        String[] files = {
                "cuba-app.properties",
                "app.properties",
                "test-app.properties",
        };
        return Arrays.asList(files);
    }
}

We recommend using a separate test DB, which can be created, for example, with the help of the following task in build.gradle:

configure(coreModule) {
...
    task createTestDb(dependsOn: assemble, description: 'Creates local Postgres database for tests', type: CubaDbCreation) {
        dbms = 'postgres'
        dbName = 'sales_test'
        dbUser = 'cuba'
        dbPassword = 'cuba'
    }

CubaTestCase class contains the following fields and methods that can be used in the test code:

  • persistence – a reference to the Persistence interface.

  • metadata – a reference to the Metadata interface.

  • deleteRecord() – a method that can be conveniently used in tearDown() to delete test objects from DB.

An example of a test that checks loading entities from the database:

public class CustomerLoadTest extends SalesTestCase {

    private UUID customerId;

    @Override
    public void setUp() throws Exception {
        super.setUp();
        persistence.createTransaction().execute(new Transaction.Runnable() {
            @Override
            public void run(EntityManager em) {
                Customer customer = new Customer();
                customerId = customer.getId();
                customer.setName("testCustomer");
                em.persist(customer);
            }
        });
    }

    @Override
    public void tearDown() throws Exception {
        deleteRecord("SALES_CUSTOMER", customerId);
        super.tearDown();
    }

    public void test() {
        Transaction tx = persistence.createTransaction();
        try {
            EntityManager em = persistence.getEntityManager();
            TypedQuery<Customer> query = em.createQuery(
                "select c from sales$Customer c", Customer.class);
            List<Customer> list = query.getResultList();
            tx.commit();
            assertTrue(list.size() > 0);
        } finally {
            tx.end();
        }
    }
}