Failed to load ApplicationContext while running AppTest.class in java spring project

61 views Asked by At

I'm writing spring web-program. After I wrote controllers,mappers and so on I need to test them. Before this I just tried to run my BankAppApplicationTests.class that was generated automatically, method @Test void contextLoads() {} failed. This is my test class:

@TestPropertySource(locations = {"classpath:application.yaml", "classpath:db/changelog/db.changelog-master-test.xml"})
@SpringBootTest(classes = BankAppApplication.class, properties = {"spring.profiles.active=test"})
class BankAppApplicationTests {

    @Test
    void contextLoads() {
    }
}

This is project structure

I have two properties.yaml files (the only difference between them is url for datasource(i created two DB - one for main app, second for testing)):

spring:
  profiles:
    active: default

  main:
    banner-mode: off

  datasource:
    url: ${datasource.url}
    username: ${datasource.username}
    password: ${datasource.password}
    driver-class-name: com.mysql.cj.jdbc.Driver

  jpa:
    show-sql: false
    hibernate:
      ddl-auto: none

  liquibase:
    enabled: true
    change-log: classpath:db/changelog/db.changelog-master.xml

Program run without problem - it creates DB, DB filled via liquibase, code works. Test fails, and database for test does'nt filled in.

I expect my test class run without errors and database for test created and filled succesfully but it not.

10:59:33.497 [main] ERROR org.springframework.boot.SpringApplication -- Application run failed
java.lang.IllegalStateException: Failed to add PropertySource to Environment

Thanks to M. Deinum reply, i deleted @TestPropertySource from my class test, and second - i deleted from both my yaml files: "profiles: active: default"

After it - testslass runned successfully and created connection with test-DB.

0

There are 0 answers