2017-10-12 14 views
0

私は春ブーツに新しい午前を使用する場合、外部プロパティファイルをロードしていないが、私は@SpringBootTestとともに@TestPropertySourceアノテーションを使用してファイルのロードのプロパティで問題を抱えています、ここに私のコード春ブーツ@TestPropertySourceが@SpringBootTestと

ですtest_application.propertiesのsrc /テスト/リソースの場所にあります。ここでは
@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 

です。私はプロパティファイルが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); 

    } 

} 

ための私のコードであるように私は性質が

として属性を使用している場合は非常に奇妙なことがあるロードされ、@DataJpaTestと同じ@TestPropertySourceアノテーションを使用

@TestPropertySource(プロパティ= { "spring.datasource.username =ルート"、 "spring.datasource.password =ルート"})

location属性ではなく

の場合、これらの値はデータベースの読み込みに使用されます。

答えて

1

私が代わりに@TestConfiguration注釈の私のテスト構成クラスの一つに@Configurationを持っていたと一緒に使用されているので、application.propertiesファイルがロードされ、test_application.propertiesを交換しますファイル

+0

嬉しいです、あなたはそれを解決しました。 – Amiko

1

アノテーションからクラスパスを削除してみてください。このようになります。 @TestPropertySource("test_application.properties")

@TestPropertySourceは通常@ContextConfiguration