Search code examples
javaspringpostgresqlintegration-testing

An error occurred when accessing the database view when running an integration test on the server, which works on the PC without problems


I'm running an integration test, which creates some database structure before its execution and fills it with test data.
Further, my method accesses the view via JDBC and retrieves data from it.
When running integration tests on the server during build, I regularly get an error that the view does not exist. When running on a PC, this problem does not occur.

Can you suggest what could be the reason?

Technology:
Java 8
postgres 13
Spring 5

Test example:

@Sql(
  scripts = {
  "drop-shema.sql", 
  "create-schema.sql", 
  "create-tables.sql", 
  "add-constraints.sql",
  "create-view.sql",
  "inser-data.sql"
  },
  config = @SqlConfig(transactionMode = TransactionMode.ISOLATED))
@Test
public void foo() { 
    List<Bar> bars = selectBarDao.exec();
    ...
}

Hypotheses that I tested, and which did not help me:

  1. Parallel launch gives problems, because The server has more CPU than my PC.
    Checks:
    а) Added a parameter config = @SqlConfig(transactionMode = TransactionMode.ISOLATED) to the @SQL annotation
    b) Added the @Transaction annotation above the test class. So in this case, the database structure was cleaned and completely created in my test.
    The parallel launch of other tests could not interfere, because he was in other transactions
    c) Disabled multi-threaded running of tests

  2. @SQL annotation does not notify me of an error in the script, such as that the view could not be created.
    Check: removed the script from @SQL to clean up the structure. When running on a PC, I got errors.

  3. The order of execution of scripts inside the same @SQL annotation gets confused
    Check: merged several files into one


Solution

  • Hypothes (1) was right.

    I fixed problem, when separate tests in two modules on two schemas, and start pass schema name as parameter.
    Another word, in tests in package_1 i'm pass to method use schema_1 In tests in package_2 i'm pass to method use schema_2.