2016-04-01 17 views
0

Spring boot ile JUNIT testinde bir sorun yaşıyorum: @Value çözülmedi. İşte kodu:Spring Boot Sınama: Özellik kaynağı ile @Value çözülmedi

Bahar önyükleme yapılandırma sınıfı:

@Configuration 
@PropertySource(value="classpath:/config/parametrage-environnement.properties",name="env") 
public class ExternalRessourceConfiguration { 

    //To resolve ${} in @Value 
     @Bean 
     public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() { 
      return new PropertySourcesPlaceholderConfigurer(); 
     } 
} 

Sınıf testi:

@RunWith(SpringJUnit4ClassRunner.class) 
@SpringApplicationConfiguration(classes = Application.class) 
@WebAppConfiguration 
@IntegrationTest 
public class ConnexionEbicsResourceIntTest { 

@Test 
@Transactional 
public void createConnexionEbics() throws Exception { 

      restConnexionEbicsMockMvc.perform(post("/api/connexionEbicss") 
      .contentType(TestUtil.APPLICATION_JSON_UTF8) 
      .content(TestUtil.convertObjectToJsonBytes(connexionEbicsDTO))) 
      .andExpect(status().isCreated()); 

Java ressource:

@RestController 
@RequestMapping("/api") 
public class ConnexionEbicsResource { 

    @Value("${env['connexion.proxy.host']}") 
    //@Value("${connexion.proxy.host}") 
    public String protocol; 

@RequestMapping(value = "/connexionEbicss", 
     method = RequestMethod.POST, 
     produces = MediaType.APPLICATION_JSON_VALUE) 
    @Timed 
    public ResponseEntity<ConnexionEbicsDTO> createConnexionEbics(@RequestBody ConnexionEbicsDTO connexionEbicsDTO) throws URISyntaxException { 
     log.debug("REST request to save ConnexionEbics : {}", connexionEbicsDTO); 
     String a = protocol; 

} 

Java ressource'da, testi çalıştırdığımda "a" değeri boş. @Value çözülmedi, neden? İlkbahar önyükleme yapılandırmam TÜM önyükleme yapıldı.

parametrage-environnement.properties dosya hem yolları bulunur: src/main/resources/config ve src/test/kaynaklar/config benim denetleyicisi alay edildiği için (kopyala/yapıştır)

cevap

0

1), ben env bir yay fasulyesi (@Bean) olması beklenir, çünkü kontrol cihazı doğrudan bu yay enjeksiyon kullanamaz (dalga geçen yok etmesi gereken yay!)

2) sözdizimi

@Value("${env['connexion.proxy.host']}") 

yanlıştır.

//To resolve ${} in @Value 
     @Bean 
     public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() { 
      return new PropertySourcesPlaceholderConfigurer(); 
     } 

3) ayarlamak için: here

Yani, @PropertySource ile, @value ("$ {connexion.proxy.host}") sözdizimi kullanmak zorunda ve Çözümleyici unutma görün kontrol sınıfında protokol, bir test sınıfından enjekte gerekir:

@Before 
    public void setup() { 
     MockitoAnnotations.initMocks(this); 
     ConnexionEbicsResource connexionEbicsResource = new ConnexionEbicsResource(); 
     ReflectionTestUtils.setField(connexionEbicsResource, "connexionEbicsService", connexionEbicsService); 
     **ReflectionTestUtils.setField(connexionEbicsResource, "protocol", protocol);** 
     ReflectionTestUtils.setField(connexionEbicsResource, "connexionEbicsMapper", connexionEbicsMapper); 
     this.restConnexionEbicsMockMvc = MockMvcBuilders.standaloneSetup(connexionEbicsResource) 
      .setCustomArgumentResolvers(pageableArgumentResolver) 
      .setMessageConverters(jacksonMessageConverter).build(); 
    } 

test sınıfı yüklenen kaynak paketi sahip:

@RunWith(SpringJUnit4ClassRunner.class) 
@SpringApplicationConfiguration(classes = Application.class) 
@WebAppConfiguration 
@IntegrationTest 
@PropertySource(value="classpath:/config/parametrage-environnement.properties",name="env") 
//@PropertySource("classpath:/config/parametrage-environnement.properties") 
public class ConnexionEbicsResourceIntTest { 
.... 
    @Value("${connexion.proxy.host}") 
    public String protocol; 

Neyse

+1

Kurucu enjeksiyonu kullandıysanız 'ReflectionTestUtils' kullanımını engelleyebilirsiniz. –

İlgili konular