mopodafordeya
mopodafordeya

Reputation: 635

How to design a data driven JUnit test class

@Parameters
public static Collection data() throws IOException {       
   ArrayList<String> lines = new ArrayList();

   URL url = PokerhandTestCase.class.getClassLoader().getResource("test/TestFile.txt");
   File testFile = new File(url.getFile());
   FileReader fileReader = new FileReader(testFile);
   bufReader = new BufferedReader(fileReader);
   assertFalse("Failed to load the test file.", testFile == null);

   boolean isEOF = false;
   while (!isEOF){

        String aline = bufReader.readLine();

        if (aline == null){
            System.out.println("Done processing.");
            isEOF = true;
        }

        lines.add(aline);   
   }

   return Arrays.asList(lines); 

 }

The last line of the program is causing the crash, I would like to know what is the proper way to define a collection from a arrayList. This function is required to Collection as the return type.

Upvotes: 2

Views: 1429

Answers (3)

Matthew Farwell
Matthew Farwell

Reputation: 61695

Your collection that you are returning needs to be a Collection<Object[]>. You are returning a Collection. You need to do something like this (for a complete example):

@RunWith(Parameterized.class)
public class MyTest {
    @Parameters
    public static Collection<Object[]> data() throws IOException {
        List<Object[]> lines = new ArrayList<>();

        File testFile = new File("/temp/TestFile.txt");
        FileReader fileReader = new FileReader(testFile);
        BufferedReader bufReader = new BufferedReader(fileReader);
        Assert.assertFalse("Failed to load the test file.", testFile == null);

        boolean isEOF = false;
        while (!isEOF) {
            String aline = bufReader.readLine();

            if (aline == null) {
                System.out.println("Done processing.");
                isEOF = true;
            }

            lines.add(new Object[] { aline });
        }

        return lines;
    }

    private final String file;

    public MyTest(String file) {
        this.file = file;
    }

    @Test
    public void test() {
        System.out.println("file=" + file);
    }

}

Note that you're not closing the files here, and you're adding a useless null value to the end of the list, but I copied your code :-).

Upvotes: 0

fatmind
fatmind

Reputation: 21

ArrayList lines = new ArrayList(); ... return Arrays.asList(lines);

this return two-dimensional array.

This function is required to Collection as the return type.

I think user1697575's answer is correct.

Upvotes: 0

user1697575
user1697575

Reputation: 2848

Replace last line with this:

return (Collection)lines;

Since ArrayList implements Collection interface: http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html

So overall code:

  public static Collection data() throws IOException 
  {
    ArrayList<String> lines = new ArrayList();
    // populate lines collection...
    return (Collection)lines;
  }

Based on the comments below, perhaps this would qualify as "Collection of arrays":

  public static Collection data() throws IOException 
  {
    ArrayList<String> array1 = new ArrayList();
    ArrayList<String> array2 = new ArrayList();
    ArrayList<String> array3 = new ArrayList();
    // populate lines collection...
    ArrayList<ArrayList<String>> lines = new ArrayList();
    lines.add(array1);
    lines.add(array2);
    lines.add(array3);
    return (Collection)lines;
  }

Upvotes: 3

Related Questions