2017-08-01 19 views
1

私は、外部ソースからデータを取得します。私の場合はjsonファイルです。私は対応するフィールドのデータ注入にJUnit Parameterizedを使用しています。JUnitパラメータ化されたデータの注入

@RunWith(Parameterized.class) 
public class MyTest extends CommonTest 
{ 
    @Parameterized.Parameter 
    public String field1; 
    @Parameterized.Parameter(1) 
    public String field2; 

    public static Collection<Object[]> params() throws PropertyNotFoundException 
    {  
     final JsonReader reader = new JsonReader(new FileReader("path to the json file")); 
     // here I am reading the tests sampledata from the json file 
    }  
} 

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = {"classpath:/test-spring.xml"}) 
@ActiveProfiles(resolver = SpringActiveProfileResolver.class) 
public class CommonTest 
{ 
    @ClassRule 
    public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule(); 

    @Rule 
    public final SpringMethodRule springMethodRule = new SpringMethodRule(); 

    ... 
} 

現時点では、params()メソッドで外部ソースへの静的パスを使用しています。この外部ソースへの相対パスを使用するにはどうしたらいいですか? jsonファイルはtest-spring.xmlの隣にあります。

私の問題は、params()メソッドは静的で、Springを介して何かを注入して解決できないということです。

この問題はどのように解決できますか?

私はあなたがまた見てみたいことがあり

Resource resource = new ClassPathResource("/file.json"); 
File file = resource.getFile(); 

... JSONファイルでもあるので、もし、あなたがこのようなファイルハンドルを取得することができ、test-spring.xmlは、あなたのクラスパスのルートにあることがわかり

+0

あなたのテストクラスの隣のJSONファイルを保存したい場合)。getResourceAsStream(「SRC /テスト/リソースへの相対パス」) これはInputStreamを返しますが、FileReaderのISAにInputStreamReader 、あなたはそれを使用できるはずです。 – gmaslowski

答えて

関連する問題