Reputation: 2149
I am new to Spring Boot, I am having trouble in loading properties file using @TestPropertySource annotation along with @SpringBootTest , Here is my code
@RunWith(SpringRunner.class)
@SpringBootTest()
@TestPropertySource(locations="classpath:test_application.properties")
@Sql(value = {"/user_test_script.sql"})
@AutoConfigureTestDatabase(replace = Replace.NONE)
public class UserServiceImpleIntegrationTest {
@Autowired
private UserService userService;
@Autowired
ApplicationContext applicationContext;
@Test
public void testGetAllGuestUsers() throws IOException {
List<User> users =userService.getAllGuestUsers(0, 10);
assertThat(users).isNotNull();
assertThat(users).isNotEmpty();
assertThat(users).are(new Condition<User>() {
public boolean matches(User user) {
return user.getFirstName().contains("Guest"); }
});
}
}
Here is my test_application.properties file content
# Connection url for the database "test"
spring.datasource.url=jdbc:mysql://localhost:3306/test_db
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
# Hibernate
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.database-platform = org.hibernate.dialect.MySQL5Dialect
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate5.SpringSessionContext
spring.jpa.show-sql = true
spring.jpa.properties.hibernate.format_sql=true
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1
#LOGGING
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
logging.level.org.springframework.web=ERROR
logging.level.org.hibernate=DEBUG
spring.profiles.active=junit
#TransactionManagement
logging.level.org.springframework.transaction.interceptor=TRACE
My classpath:test_application.properties is located in src/test/resources location.
When I used the same @TestPropertySource annotation with @DataJpaTest ,properties files are loading as excepted.Here is my code for the same
@RunWith(SpringRunner.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace = Replace.NONE)
@Sql(value = {"/item_test_script.sql" ,"/image_test_script.sql"})
@TestPropertySource(value="classpath:test_application.properties")
public class ItemRepoTest {
@Autowired
ItemRepo itemRepo;
@Test
public void testGetAllImagesByItemId() {
List<Image> images= itemRepo.getAllImagesByItemId(1l);
assertThat(images).isNotEmpty();
assertThat(images).size().isGreaterThanOrEqualTo(1);
}
}
The very strange thing is If I use properties attribute as
@TestPropertySource(properties= {"spring.datasource.username=root","spring.datasource.password=root"})
instead of location attribute then these values are taken to load the database.what I am missing here , Or what configuration is wrong.
Upvotes: 2
Views: 11977
Reputation: 2149
I was having @Configuration in one of my Test configuration Class instead of @TestConfiguration Annotation , So the application.properties file is loaded and replacing the test_application.properties file
Upvotes: 5
Reputation: 585
Try removing classpath from the annotation, should look like this.
@TestPropertySource("test_application.properties")
@TestPropertySource
is usually used in conjunction with @ContextConfiguration
Upvotes: 5