2015-10-21 27 views
17

Tôi muốn tham gia một thử nghiệm Junit cho mùa xuân-khởi động như sau:@value không hoạt động trên mùa xuân Boot thử nghiệm

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(classes = {ApplicationTest.class}) 
public class TestOnSpring { 
    @Value("${app.name}") 
    private String appName; 

    @Test 
    public void testValue(){ 
     System.out.println(appName); 
    } 
} 

và ApplicationTest.java như thế này

@ComponentScan("org.nerve.jiepu") 
@EnableAutoConfiguration() 
public class ApplicationTest { 

    public static void main(String[] args) { 
     SpringApplication.run(ApplicationTest.class, args); 
    } 
} 

và POM của tôi như thế này :

<parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.3.0.BUILD-SNAPSHOT</version> 
    </parent> 

Khi tôi chạy thử nghiệm, tôi đã bên dưới thông tin lỗi

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'app.name' in string value "${app.name}" 
    at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174) 
    at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126) 
    at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:204) 
    at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:178) 
    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:172) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:807) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1027) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:543) 
    ... 31 more 

Nhưng Khi tôi chạy ứng dụng này như ứng dụng Java bình thường

@SpringBootApplication 
public class Application { 

    public static void main(String[] args){ 
     SpringApplication.run(Application.class, args); 
    } 
} 

Nó hoạt động tốt!

Có vấn đề gì với nó? Làm thế nào tôi nên làm bài kiểm tra junit với Spring-boot? Cảm ơn rất nhiều!

+0

Bạn đang chạy trường hợp thử nghiệm của mình sai. Bạn đang sử dụng Spring Boot, sau đó sử dụng cách kiểm tra thích hợp. Thay vì 'ContextConfiguration' sử dụng' SpringApplicationConfiguration'. –

Trả lời

27

Bạn cần phải thêm

@PropertySource ("classpath: application.properties")

tới lớp học của bạn, vì vậy nó sẽ chọn cấu hình bình thường của bạn.

Nếu bạn cần cấu hình khác nhau để kiểm tra bạn có thể thêm

@TestPropertySource (địa điểm = "classpath: test.properties")

Nếu không chỉ cần sao chép dán tập tin cấu hình của bạn đểtest/resourcesthư mục, sau đó khởi động sẽ chọn từ đó.

Xem this.

+2

Cảm ơn bạn rất nhiều vì đã giúp đỡ! Tôi thêm 'code' @ TestPropertySource (locations = "classpath: test.properties") 'code', và nó hoạt động. Cảm ơn một lần nữa! –

+1

Chỉ cần làm rõ, bạn cần thêm '@TestPropertySource ...' vào lớp java đang chạy thử nghiệm. –

Các vấn đề liên quan