0

I have this situation with my Spring Boot application and the management of tests:

The application is running correctly. But for its unit tests, regular tests are OK, while integration tests are giving me some issues.

In order to be able to operate with a database for the tests, I do the following in a base class from which all integration tests inherit:

public abstract class IntegrationTestBase {

  protected static PostgreSQLContainer<?> postgres = null;

  private static boolean databaseStarted = false;

  @BeforeAll
  public static void beforeAll() {
    if (!databaseStarted) {
      postgres = new PostgreSQLContainer<>("postgres:16-alpine")
        .withReuse(true);

      postgres.start();
      databaseStarted = true;

      Runtime.getRuntime().addShutdownHook(new Thread(() -> {
        // Ideally this should stop the DB only after executing all classes
        postgres.stop();
      }));
    }
  }
}

I can launch a test individually through a command like this:

mvn test -Dtest="UserServiceIT"

But when I try to launch all of them:

mvn failsafe:integration-test

I get the following error:

org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'entityManagerFactory' defined in class path resource
[org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]:
Invocation of init method failed; nested exception is javax.persistence.PersistenceException:
[PersistenceUnit: default] Unable to build Hibernate SessionFactory;
nested exception is org.hibernate.tool.schema.spi.SchemaManagementException:
Schema-validation: wrong column type encountered in column [item_id] in table [Item];
found [int8 (Types#BIGINT)], but expecting [int4 (Types#INTEGER)]

The class "Item" comes from an external jar and therefore I am unable to change its type, but I can tell you it is defined as a long.

I have been trying to make workarounds such as trying to redefine the entityManagerFactory but that only makes things more complicated. After all the tests are working when executing individually.

Do you know what could be causing this? Am I launching the tests the right way?

4
  • Is your test class annotated with @SpringBootTest or @DataJpaTest? Example for the latter (replace MySQLContainer with PostgreSQL): github.com/roar-skinderviken/spring-jpa-auditing-demo/blob/… Commented Jul 3 at 14:56
  • withReuse will keep your container running after test is finished, can it be the case where a previous run was with an int type instead of long? Commented Jul 3 at 21:39
  • I have the SpringBootTest annotation in this abstract class. Commented Jul 4 at 6:54
  • I also tried removing the withReuse statement, but the result is the same. The issue happens from the first test. Commented Jul 4 at 6:55

1 Answer 1

0

What ended up happening was that the database field types were not set in the Java classes according to the database types. As it was part of a dependency I had no clue what was going on. So if you have come across this kind of issue, know that there is some error in field type correspondence.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.