32

独自のプロパティ ファイル (application-test.properties) とそのスプリング構成ファイル (application-core-test.xml) を持つ jUnit テストがあります。

メソッドの 1 つは、Spring config によってインスタンス化されたオブジェクトを使用し、それは Spring コンポーネントです。クラスのメンバーの 1 つは、メインのプロパティ ファイルである application.properties から値を取得します。jUnit を介してこの値にアクセスしている間は、常に null です。実際のプロパティ ファイルを指すようにプロパティ ファイルを変更しようとしましたが、うまくいかないようです。

プロパティファイルオブジェクトにアクセスする方法は次のとおりです

@Component
@PropertySource("classpath:application.properties")
public abstract class A {

    @Value("${test.value}")
    public String value;

    public A(){
        SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
    }

    public A(String text) {
        this();
        // do something with text and value.. here is where I run into NPE
    }

}

public class B extends A { 
     //addtnl code

    private B() {

    }


    private B(String text) {
         super(text)
    }
}

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:META-INF/spring/application-core-test.xml",
                             "classpath:META-INF/spring/application-schedule-test.xml"})
@PropertySource("classpath:application-test.properties")
public class TestD {

    @Value("${value.works}")
    public String valueWorks;

    @Test
    public void testBlah() {     
        SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
        B b= new B("blah");
        //...addtnl code

    }    
}      
4

3 に答える 3