我是新來的春天開機當外部屬性文件,我有文件使用@TestPropertySource批註與@SpringBootTest沿裝載性能麻煩,這裏是我的代碼彈簧引導@TestPropertySource不加載使用@SpringBootTest
@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"); }
});
}
}
這裏是我的test_application.properties文件內容
# 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
我的類路徑:test_application.properties位於的src /測試/資源的位置。
當我用@DataJpaTest相同@TestPropertySource註釋,屬性文件加載爲excepted.Here是我同
@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);
}
}
非常奇怪的代碼是如果我使用性質屬性爲
@TestPropertySource(屬性= { 「spring.datasource.username =根」, 「spring.datasource.password =根」})
而不是位置屬性,然後這些值被用來加載數據庫。我在這裏丟失了什麼,或者什麼配置是錯誤的。
很高興,你解決了它。 – Amiko