OneToOne and OneToMany relational entities are not loading in spring boot unit test

Viewed 530

I have a spring boot project that was generated using jhipster. For some reason hibernate is not loading @OneToOne and @OneToMany annotated fields when running unit/integration test. Whenever the annotated field getter method is called it returns null for @OneToOne and an empty list/set for @OneToMany.

The test is configured to use JUnit for unit tests, Spring Test Context framework for integration tests and In-memory H2 for the database. Below shows the configuration for test DB and entity definitions.

Test config:

application.yml


eureka:
    client:
        enabled: false
    instance:
        appname: ${APPNAME}
        instanceId: ${INSTANCE_ID}

spring:
    profiles:
        active: test
    application:
        name: TestApp
    cache:
        type: simple
    datasource:
        type: com.zaxxer.hikari.HikariDataSource
        url: jdbc:h2:mem:testapp;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
        name:
        username:
        password:
        hikari:
            auto-commit: false
    jpa:
        database-platform: io.github.jhipster.domain.util.FixedH2Dialect
        database: H2
        open-in-view: false
        show-sql: false
        hibernate:
            ddl-auto: none
            naming:
                physical-strategy: org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
                implicit-strategy: org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
        properties:
            hibernate.id.new_generator_mappings: true
            hibernate.connection.provider_disables_autocommit: true
            hibernate.cache.use_second_level_cache: false
            hibernate.cache.use_query_cache: false
            hibernate.generate_statistics: true
            hibernate.hbm2ddl.auto: validate
            hibernate.jdbc.time_zone: UTC
    liquibase:
        contexts: test
    mail:
        host: localhost
    messages:
        basename: i18n/messages
    mvc:
        favicon:
            enabled: false
    thymeleaf:
        mode: HTML


server:
    port: 10344
    address: localhost

I have the following entities defined.

@Entity
@Table(name = "db_user")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
class User implements Serializable {
    @OneToOne(mappedBy = "user", fetch = LAZY, cascade = CascadeType.REMOVE)
    @JsonIgnore
    private Employee employee;

    ...

    <define getters and setters>
}

@Entity
@Table(name = "employee")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Employee implements Serializable {
    
    @OneToOne    @JoinColumn(unique = true)
    private User user;

    @ManyToOne(fetch = FetchType.LAZY)
    private Company company;

    ...
    <define getters and setters>
}

@Entity
@Table(name = "company")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Company implements Serializable {
    
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "shareFileItem")
    private Set<Employee> employees;

    ...
    <define getters and setters>
}

When running the application in dev or prod environment this works fine.

@Service
@Transactional
class ExampleService {
    public void testMethod() {
      // getting list of a employee in a company
      Company company = companyReposity.findById(...);

      company.getEmployees(); // this returns the list of employee correctly in dev/prod 
                              // but an empty set in test.

      
      // getting employee object from user entity.
      User user =  userRepository.findById(...);

      user.getEmployee(); // returns employee object in prod/dev but null in test
    }
}

I've tried mirroring my prod/dev profile in my test config and visa-versa, the result is the same. I believe I missing something in my configuration but I'm not what it is.

NB: DEV database is H2 Disk and PROD database is MYSQL

0 Answers
Related