Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

435
Views
How to use the actual database for integration testing?

There were some proposed solutions to the question "How to test SQL statements in an application" -

  • Using RAM memory - I can't change the configuration of staging environment where testing happens.
  • Using H2 - Not very compatible even in PostgreSQL mode
  • Use the same database to run the tests.
  • Using in-memory mode - PostgreSQL doesn't have one.

The third one was viable and I looked into Test Containers which is actually a beautiful solution but a relatively new one. As a result, our company is sceptical of adopting it.

We use Mybatis to access PostgreSQL.

Another way would be to recreate entire schema and populate required tables before tests. Here is the problem, I could create and delete schema with tables with the same name. To avoid name collision I'd have to change schema's name, as a result, even queries should be renamed which is not at all preferred. Is there a way to do this without changing queries but pointing them to the dummy schema.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You should NOT change your queries. In tests you should only change the connection url your application will use. The problem is, how to get that url working.

To have full test coverage you need the same db (as you noticed, h2 and other in-memory db are not very compatible). postgres doesn't have in-memory mode so you have to manage the lifecycle yourself. there is a few decisions you have to make. some of them:

  • where will you get the db from: require all the devs to provide postgres (installation / docker / vagrant) or automate the setup?

  • how to prepare db for tests: manual schema setup and cleanup?

  • how to reset db between tests: restart? always rollback? predefined and separately defined content? some kind of reverse operations?

  • if and how to make those tests fast?

there are some tools that can help you solve some of the problems:

  1. testcontainers will help you provide db.

  2. dbunit - will help you prepare data for your test.

    cons:

    • a lot of work is required to create and maintain schema and data. especially when your project is in a intensive development stage.
    • it's another abstraction layer so if suddenly you want to use some db feature that is unsupported by this tool, it may be difficult to test it
  3. testegration - intents to provide you full, ready to use and extensible lifecycle (disclosure: i'm a creator).

    cons:

    • free only for small projects
    • very young project

you can also fill the gaps on your own. as always it's a trade: time vs money

over 4 years ago · Santiago Trujillo Report

0

you can define database configuration for test purpose and connect to your real database base for execute tests. you should access to test database configuration in test classes.

for example, if you use spring and hibernate to connect to the database, you can define a test hibernate configuration xml file where it connect to test database. then in your test classes, use this configuration file as follow:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguratiion({testHibernate.xml, testSpring.xml , .... })
@TestExecutionListeners({...})
public class TestClass {
....
   @Test
   public void test1(){ 
     ...
   }
}

so, you can access your test hibernate session factory to execute your queries.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!