How to test a repository with an embedded ddbb in a multimodule project?

31 Views Asked by At

I have a multimodule project with the following structure:

project

  • api module (containing the rest controllers, the main class and the properties configuration)
  • core module (containing the repositories)

The technological stack:

  • Spring boot
  • Java 11

So I'm trying to create a test class for the repository with and embedded database (h2) in the core module but it tells me that it does not find the configuration (because it seems to be in the other module).

How can I fix that?

My test class looks something like this:

@ExtendWith(SpringExtension.class)
@DataJpaTest
public class NaceRepositoryTest {

    @Autowired
    private NaceRepository repository;

    @Test
    void saveTest() {
        NaceEntity entitySaved = repository.save(TestUtils.createEntity(3L));
        assert(entitySaved.getOrder().equals(3L));
    }

    @Test
    void getByIdTest() {
        NaceEntity entitySaved = repository.save(TestUtils.createEntity(4L));
        assertNotNull(repository.findById(entitySaved.getOrder()));
    }

}

I have created in the test resources of my module "core" the file data.sql for the embedded h2 DDBB, and also added in the same folder an application.properties file pointing to my embedded DDBB.

But it gives me the following message error:

java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
0

There are 0 best solutions below