2016-10-20 5 views
1

@value( "$ {}")を使用してsrc/main/resourcesフォルダからapplication.propertiesファイルのプロパティを読み取ります。これは、アプリケーションを実行するとうまくいきます。しかし、私がテストケースを実行すると、この値は設定されません。 は、私はすでに次のことを試してみました:Spring 4のapplication.properties Junitのテストケース実行時に@Valueが設定されていません

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

ここに私の完全なコードです。

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(); 
    } 

} 
@valueが設定されているこれは

EntApiUtil.java

@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; 
    } 

テストクラス

@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")); 
    } 
} 

テストケースを実行するときに値が設定されていないため、Nullpointer例外が発生します。私は今、何をしないのです

(同様にそれを削除しようとした)
@Bean 
public static PropertySourcesPlaceholderConfigurer propertiesResolver() { 

    return new PropertySourcesPlaceholderConfigurer(); 
} 

をクラスで明らかなように可能なすべての注釈を使用し、また私のConfigクラスApplication.javaにこれを追加しようとしていますか?

答えて

2

SpringBootを使用しているときにPropertySourcesPlaceholderConfigurerを追加する必要はありません。既定では既に追加されています。

なぜEntApiUtilプロパティ値が設定されていないのですか?

あなた自身で新しいEntApiUtil()をインスタンス化しているので、春にはこれが分かりません。あなたはこの

@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

を配線する必要が

はありがとうございました。それは魅力のように働いた。注釈@ContextConfigurationを追加すると、Environmentオブジェクトがオートワイヤされると思っていました。 –

関連する問題