Home > Enterprise >  I can't test on a test database during my integration tests
I can't test on a test database during my integration tests

Time:10-17

I would like to do some integration tests. These tests would then use a h2 test database, which would always be deleted afterwards. Here is my test:

@RunWith(SpringRunner.class)
@SpringBootTest()
@DataJpaTest
@Slf4j
public class PostePomponServiceIT{
    @Autowired
    private PostePomponService postePomponService;

    @Autowired
    private PostePomponRepository postePomponRepository;

    @Test
    public void addPostePompon_Ok() throws BadRequestException {
        PostePomponForm postePomponForm = new PostePomponForm();
        postePomponService.add(postePomponForm);
        assertEquals(1[![enter image description here][1]][1], postePomponRepository.findAll().size());

    }
}

My application.properties

spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:testdb
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
spring.jpa.database = h2

and how my folder is structured: enter image description here

So I can't launch my test, I always get this error message enter image description here

and this one: ava.lang.IllegalStateException: Configuration error: found multiple declarations of @BootstrapWith for test class [com.MailleCoTech.SuiviProduction.services.PostePomponServiceIT]: [@org.springframework.test.context.BootstrapWith(value=org.springframework.boot.test.context.SpringBootTestContextBootstrapper.class), @org.springframework.test.context.BootstrapWith(value=org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTestContextBootstrapper.class)]

I am waiting for your help, thank you in advance :) Have a nice day

CodePudding user response:

Remove annotation @SpringBootTest from test case. You have @DataJpaTest which does everything @SpringBootTest some extra stuff(you can check documentation for more details).

CodePudding user response:

The problem here is that you are using an empty @SpringBootTest.

but you also need to say the test which configuration classes you want to execute. I mean you say to the test "please execute a spring test", but you are not providing any spring application to it.

@SpringBootTest(classes={YourJavaClassWithStaticMain.class})

is what you want to do

  • Related