Disable database connections with @SpringBootTest
Asked Answered
G

2

6

I have a SpringBoot application with hibernate. In my tests I would like to disable any sort of db connection and configuration (tests don't have access to db). How should I do it ?

My test class is annotated with @SpringBootTest and has @Autowired properties. What's the proper annotation value to disable all db interactions ?

@SpringBootTest
class MyTest {

  @Autowired
  ....
}

Gdynia answered 23/10, 2019 at 7:10 Comment(1)
I have the same issue. have you found the solution?Anuran
R
0

This solved it for me:

@SpringBootTest
@EnableAutoConfiguration(exclude = {
    DataSourceAutoConfiguration.class,
    DataSourceTransactionManagerAutoConfiguration.class,
    HibernateJpaAutoConfiguration.class })
class MyServiceApplicationTests {

    @MockBean
    MyRepository repository;
    ...
}

check this link for the alternatives

Ribwort answered 14/7 at 19:47 Comment(0)
E
-1

You can use @TestPropertySource to override values in application.properties.

@TestPropertySource(locations="classpath:test.properties")

A Quick Guide to @TestPropertySource

Elzaelzevir answered 23/10, 2019 at 8:36 Comment(2)
Spring data will be on spring context anyway, independent of your test property settings.Peshitta
this don't solve the problem, your db beans are still use and they have no connection to db.Monamonachal

© 2022 - 2024 — McMap. All rights reserved.