Search code examples
spring-bootspring-dataspring-webfluxh2r2dbc

Error while testing with H2 database with Reactive Spring boot


I am using Reactive Spring boot to create a REST API and while the API works perfectly (I have tested it with postman), I also need to write some Unit tests and for the Unit tests I need to use a seperate in-memory database for it. I am using H2 database for testing and a hosted Postgres DB for the actual application and while my application entities are being loaded successfully in the postgres DB, None of my tests are passing as I receieve the following error:

Failed to execute SQL script statement #1 of class path resource [data.sql]: CREATE TABLE IF NOT EXISTS full_connection ( id BIGINT PRIMARY KEY , aa TEXT NOT NULL, fip TEXT NOT NULL, status TEXT, created_on TIMESTAMP, created_by TEXT, last_updated_on TIMESTAMP, last_updated_by TEXT ); nested exception is java.lang.ClassCastException: class java.lang.Long cannot be cast to class java.lang.Integer (java.lang.Long and java.lang.Integer are in module java.base of loader 'bootstrap')

I am assuming the error is being thrown because of the id paramater which has the datatype BIGINT which the official documentation of H2 says is mapped to java.long.Long.

Here's my entity:

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@ToString
@Table
public class FullConnection {

    @Id
    @Column(name = "id")
    private Long id;

    @Column(name = "AA", nullable = false)
    private String aa;

    @Column(name = "FIP", nullable = false)
    private String fip;

    private String status;

    private LocalDateTime createdOn;
    private String createdBy;

    private LocalDateTime lastUpdatedOn;
    private String lastUpdatedBy;

}

Here's how I am initializing the data:

@Bean
    ConnectionFactoryInitializer initializer(@Qualifier("connectionFactory") ConnectionFactory connectionFactory) {
        ConnectionFactoryInitializer initializer = new ConnectionFactoryInitializer();
        initializer.setConnectionFactory(connectionFactory);
        ResourceDatabasePopulator resource =
                new ResourceDatabasePopulator(new ClassPathResource(dataInitializerName));
        initializer.setDatabasePopulator(resource);
        return initializer;
    }

where it's reading the dataInitializerName string from application-test.yaml and is being read correctly, the script (for testing)(the script for prod and testing are different) is:

CREATE TABLE IF NOT EXISTS full_connection (
    id BIGINT PRIMARY KEY ,
    aa TEXT NOT NULL,
    fip TEXT NOT NULL,
    status TEXT,
    created_on TIMESTAMP,
    created_by TEXT,
    last_updated_on TIMESTAMP,
    last_updated_by TEXT
);

Here: https://www.h2database.com/html/datatypes.html#bigint_type it clearly states that BIGINT is mapped to java.long.Long, then why is it trying to cast it to int? Or is it regarding some other field?


Solution

  • I had a similar problem. Explicitly defining the version for the dependency r2dbc-h2 in pom.xml is causing the problem. I removed the version so that springboot will automatically use the compatible version of r2dbc-h2, and then the problem resolved.