2016-10-20 2 views
1

nicht gesetzt. Ich verwende @value ("$ {}"), um eine Eigenschaft aus der Datei application.properties aus dem Ordner src/main/resources zu lesen. Das funktioniert gut, wenn ich die Anwendung starte. Wenn ich einen Testfall ausführe, wird dieser Wert jedoch nicht festgelegt. Ich habe bereits versucht, die folgenden:Spring 4 application.properties @Value ist beim Ausführen des Junit-Testfalls

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(classes = Application.class) 
@TestPropertySource("classpath:/application.properties") 

Hier ist meine komplette Code.

Application.java

@SpringBootApplication 
@ComponentScan ({"acn.*"}) 
public class Application extends SpringBootServletInitializer { 

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

    @Override 
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 
     return application.sources(Application.class); 
    } 

    @Bean 
    public static PropertySourcesPlaceholderConfigurer propertiesResolver() { 

     return new PropertySourcesPlaceholderConfigurer(); 
    } 

} 

EntApiUtil.java Hier @value gesetzt

@Component 
public class EntApiUtil implements IEntApiUtil{ 
    Logger log = LoggerFactory.getLogger(this.getClass()); 

    @Value("${entapi.url}") 
    private String entApiUrl; 
    private static final String STATUS_VALID = "1"; 
    private static final String STATUS_INVALID ="0" ; 
    private static final String STATUS_ERROR ="0" ; 
    private static final int TIMEOUT = 60000; 
    private static final String SERVICE_NAME_ADDRESS = "AddressService"; 
    .............. 
    .............. 

    private AddressBindingStub getAddressBindingStub() throws Exception { 
     AddressBindingStub binding = null; 
     try { 
      AddressServiceLocator addressServiceLocator = new AddressServiceLocator(); 
      System.out.println("Ent-API URL: " + entApiUrl); 
      addressServiceLocator.setAddressPortEndpointAddress(entApiUrl + "/" + SERVICE_NAME_ADDRESS); 
      binding = (AddressBindingStub) addressServiceLocator.getAddressPort(); 
      //Time out after a minute 
      binding.setTimeout(TIMEOUT); 
     } catch (javax.xml.rpc.ServiceException jre) { 
      log.error("Error occurred attempting to establish address validation API.", jre); 
      System.out.println(" ERROR"); 
     } 

     return binding; 
    } 

Testklasse

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(classes = Application.class) 
@TestPropertySource("classpath:/application.properties") 
public class EntApiUtilTest { 


    IEntApiUtil entApiUtil; 
    Address address; 
    StatusVO statusVO; 

    @Before 
    public void setUp(){ 
     entApiUtil = new EntApiUtil(); 
     address = new Address(); 
     address.setStAddress("2120 Mission Rd"); 
     address.setUnitNum("250"); 
     address.setUnitType("Suite"); 
     address.setCity("ESCCONDIDO"); 
     address.setState("CA"); 
     address.setZipcode("92029"); 
    } 

    @Test 
    public void validateAddress_statusVO_not_null(){ 
     statusVO = entApiUtil.validateAddress(address); 
     assertNotNull(statusVO); 
    } 

    @Test 
    public void validateAddress_valid_streetName(){ 
     statusVO = entApiUtil.validateAddress(address); 
     System.out.println(statusVO.getValidatedAddress().getStreetName() + " ======="); 
     assertTrue("Valid St reet Name", statusVO.getValidatedAddress().getStreetName().equalsIgnoreCase("Mission")); 
    } 
} 

Ich bekomme eine Nullpointer-Ausnahme, weil beim Ausführen von Testfällen kein Wert festgelegt wird. Ich habe alle Anmerkungen verwendet möglichst deutlich in den Klassen und auch versucht, diese zu meinen Config Klasse Application.java (versucht, es so gut zu entfernen)

@Bean 
public static PropertySourcesPlaceholderConfigurer propertiesResolver() { 

    return new PropertySourcesPlaceholderConfigurer(); 
} 

Was soll ich jetzt fehlt?

Antwort

2

Sie müssen PropertySourcesPlaceholderConfigurer nicht hinzufügen, wenn Sie SpringBoot verwenden, es ist bereits standardmäßig hinzugefügt.

Warum werden die EntApiUtil-Eigenschaftswerte nicht festgelegt?

Sie instanziieren neue EntApiUtil() von Ihren eigenen, so dass Frühling dies nicht wissen.

müssen Sie diese

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(classes = Application.class) 
@TestPropertySource("classpath:/application.properties") 
public class EntApiUtilTest 

@Autowire 
private EntApiUtil entApiUtil 

    @Test 
    public void validateAddress_statusVO_not_null(){ 
///..address etc 
     statusVO = entApiUtil.validateAddress(address); 
     assertNotNull(statusVO); 
    } 

} 
+0

Vielen Dank verdrahten. Es hat wie ein Zauber funktioniert. Ich dachte, dass wenn Annotation @ContextConfiguration hinzugefügt würde, würde es Environment-Objekt automatisch. –

Verwandte Themen