Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to load application.properties file @DataJpaTest annotation

i am trying to load the application.properties in spring boot project for testing . i am using @DataJpaAnnotation along with the my custom application.properties file.

Here is my sample configuration looks like


@DataJpaTest
@RunWith(SpringRunner.class)
@SqlGroup({
        @Sql(executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD, scripts = {
                "classpath:sql/dont-use-cascadeType-remove/before.sql" }),
        @Sql(executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD, scripts = {
                "classpath:sql/dont-use-cascadeType-remove/after.sql" }) })
@TestPropertySource(locations = { "classpath:application.properties" })
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
@Slf4j
public class BookCategoryRepositoryTest {

i am able to execute test cases successfully but when i verified the logs my application is taking the embeded H2 Db URL not the one which i mentioned in application.properties file.

From the logs i found

embedded database: url='jdbc:h2:mem:69b49362-3f83-4e79-9f35-b0deb5e744f2;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false', username='sa' 

my property file contains

spring.datasource.url=jdbc:p6spy:mem:jpa-best-practices;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.username=db-user
spring.datasource.password=db-password

not sure why its happening and i am unable to find the solution. please help.

like image 755
Madhu Avatar asked Oct 15 '22 12:10

Madhu


1 Answers

You just need to annotate your test with

@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)

as documented.

like image 77
JB Nizet Avatar answered Oct 21 '22 05:10

JB Nizet