'Springboot: Unit testing with H2 emulating postgres

I'm trying to test queries against H2 emulating Postgress.

The unit test look as follow:

Unit test

@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = {MyRepository.class})
@Sql(scripts = "classpath:schema-test.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
//@TestPropertySource("classpath:application-test.properties")
public class ImportIdTest {

    @Autowired
    private MyRepository repo;


    @Test
    public void importIdSeqTest() {
        assertEquals(1, repo.getNexImportId());
        assertEquals(2, repo.getNexImportId());
    }
}

And the test application properties as follow:

application-test.properties

spring.datasource.url=jdbc:h2:mem:mydatabase;DB_CLOSE_ON_EXIT=FALSE;MODE=PostgreSQL;INIT=CREATE SCHEMA IF NOT EXISTS myschema
spring.datasource.username=sa
spring.datasource.password=password
spring.datasource.driverClassName=org.h2.Driver
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL95Dialect
spring.datasource.platform=test
spring.jpa.hibernate.ddl-auto= none
spring.jpa.defer-datasource-initialization=true

pom.xml

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>

While running the unit test, the error I'm getting is:

No qualifying bean of type 'cfoo.bar.MyRepository'

while @ContextConfiguration(classes = {MyRepository.class}) as been added to the unit test.

What am I doing wrong ?

EDIT

As per comment, in the project code :

Repository declaration:

@Repository
public interface MyRepository extends JpaRepository<MyEntity, Long> {

    @Query(value = "SELECT nextval('import_id_seq')", nativeQuery = true)
    Long getNexImportId();
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source