2016-07-22 34 views
1

比方說,我有一個JUnit方法生成數據驅動的單元測試中的Java

public class BusinessClassTest { 

    private BusinessClass fixture; 

    @Test 
    public void test1() { 
     //the following two paths reside in the man/test/resources folder 
     String inputPath = "/fixtures/1/input"; 
     String expectedPath = "/fixtures/1/expected"; 
     validatedFixture(inputPath, expectedPath); 
    } 

    private void valiateFixture(String inputPath, String expectedPath) { 
     //inputData = load the input data 
     //feed it to a fixture 
     //actual = fixture.process(inputData) 
     //expectedData = loadExpectedData 
     //validate(expectedData, actualData); 
    } 
} 

現在讓我們假設我有下夾具20個文件夾。我如何遍歷文件夾和每個文件夾生成類似於

@Test 
    public void test{test_number}() { 
     //the following two paths reside in the man/test/resources folder 
     String inputPath = "/fixtures/{test_number}/input"; 
     String expectedPath = "/fixtures/{test_number}/expected"; 
     validatedFixture(inputPath, expectedPath); 
    } 

Idlealy的方法,我想建立這個類出隨着Maven的一部分。


更新

我用速度但是生成類,不知道怎麼搞的行家代碼生成...

回答

3

使用Matt爲文件系統遍歷列出的內容,可以使用參數化測試來執行數字驗證。

這種方法將爲您提供基於@Parameters(name)註釋的參數的命名測試。

import java.io.File; 
import java.util.ArrayList; 
import java.util.Collection; 

import org.junit.Assert; 
import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.junit.runners.Parameterized; 
import org.junit.runners.Parameterized.Parameters; 

@RunWith (Parameterized.class) 
public class BusinessClassTest{ 
    @Parameters(name="{0}:{1}") 
    public static Collection<Object[]> getTestPaths() { 
     Collection<Object[]> allTests = new ArrayList<>(); 

     File file = new File("/fixtures/"); 
      String[] dirs = file.list((current, name) -> 
          new File(current, name).isDirectory()); 

      for (String testNumber : dirs) { 
      String inputPath = "/fixtures/" + dirs + "/input"; 
      String expectedPath = "/fixtures/" + dirs + "/expected"; 
      allTests.add(asConstructorArguments(inputPath, expectedPath)); 
      } 

     return allTests; 
    } 

    private static Object[] asConstructorArguments(String inputPath, String expectedPath) { 
     return new Object[]{inputPath, expectedPath}; 
    } 

    private final String inputData; 
    private final String expectedData; 
    private final Fixture fakedFixture; 


    public BusinessClassTest(String input, final String expected) { 
     this.inputData = input; 
     this.expectedData = expected; 
     fakedFixture = new Fixture() {   
      @Override 
      public String process(String path) { 
       return expected; 
      } 
     }; 
    } 

    @Test 
    public void validateFixture() { 
     //feed it to a fixture 
     String actualData = fakedFixture.process(inputData); 
     Assert.assertEquals(expectedData, actualData); 
    } 

    //Interface to emulate your API 
    public interface Fixture { 
     String process(String path); 
    } 
} 
2

你真的需要動態生成的測試類?另一種方法是迭代fixtures基路徑的所有子目錄,然後直接調用validatedFixture來獲取每個路徑。在下面的Java8中查找樣本:

@Test 
public void bigTest { 
    File file = new File("/fixtures/"); 
    String[] dirs = file.list((current, name) -> 
        new File(current, name).isDirectory()); 

    for (String testNumber : dirs) { 
    String inputPath = "/fixtures/" + dirs + "/input"; 
    String expectedPath = "/fixtures/" + dirs + "/expected"; 
    // omit the test method and call validation directly 
    validatedFixture(inputPath, expectedPath); 
    } 
} 
+0

謝謝您花時間做到這一點...我意識到這種做法。我需要實際的方法,以便我可以註釋他們...... – hba