2015-10-21 21 views
17

voglio prendere un test JUnit per la primavera-boot, come di seguito:@Value non funziona su Spring Boot prova

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

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

e ApplicationTest.java come questo

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

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

e la mia POM come questo :

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

Quando eseguo il test, ho ottenuto qui di seguito le informazioni sugli errori

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 

ma quando ho eseguito questa applicazione come normale applicazione Java

@SpringBootApplication 
public class Application { 

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

E funziona bene!

Cosa c'è di sbagliato in questo? Come dovrei fare il test di junit con Spring-boot? Grazie mille!

+0

Si sta eseguendo il test case errato. Stai usando Spring Boot quindi usi il modo appropriato di testare. Invece di 'ContextConfiguration' usa' SpringApplicationConfiguration'. –

risposta

27

è necessario aggiungere

@PropertySource ("percorso di classe: application.properties")

alla classe, quindi sarà scegliere le configurazioni normali.

Se avete bisogno di configurazioni diverse per il test è possibile aggiungere

@TestPropertySource (posizioni = "percorso di classe: test.properties")

Se non basta copiare e incollare il file di configurazione pertest/resourcescartella, quindi l'avvio sceglierà da lì.

Vedere this.

+2

Grazie mille per il tuo aiuto! Aggiungo 'code' @ TestPropertySource (locations = "classpath: test.properties") 'code', e funziona. Grazie ancora! –

+1

Giusto per chiarire, è necessario aggiungere "@TestPropertySource ..." alla classe java che sta eseguendo il test. –

Problemi correlati