3

I am writing a test case where in I want to run a one DataPoint for one test case and second DataPoint for second test case.

@RunWith(Theories.class)
public class DummyTest {

    @DataPoints
    public static String[] getFileNames() {
        return new String[] { "firstFile.txt","firstFile1.txt" };
    }

    @Theory
    public void test1(String fileName) throws Exception {
        System.out.println(fileName);
        assertThat(true, is(equalTo(Boolean.TRUE)));
    }

    @DataPoints
    public static String[] getSecondFileNames() {
        return new String[] { "secondFile.txt","secondFile1.txt" };
    }

    @Theory
    public void test2(String fileName) throws Exception {
        System.out.println(fileName);
        assertThat(true, is(equalTo(Boolean.TRUE)));
    }

}

I want that for first test case my first datapoints i.e. getFileNames method is called and for second test case getSecondFileNames datapoints should be called. Can anybody suggest is this feasible?

Thanks,
Shekhar

Chadwick
  • 12,555
  • 7
  • 49
  • 66
Shekhar
  • 5,771
  • 10
  • 42
  • 48
  • Possible duplicate of [How to attach a DataPoint with a Theory?](https://stackoverflow.com/questions/7554458/how-to-attach-a-datapoint-with-a-theory) – outis Aug 05 '18 at 21:11

4 Answers4

5

As of the shortly-upcoming JUnit 4.12, you can now name sets of datapoints and require parameters to come only from that set, e.g.:

@RunWith(Theories.class)
public class DummyTest {

    @DataPoints("fileNames1")
    public static String[] getFileNames() {
        return new String[] { "firstFile.txt","firstFile1.txt" };
    }

    @Theory
    public void test1(@FromDataPoints("fileNames1") String fileName) throws Exception {
        System.out.println(fileName);
        assertThat(true, is(equalTo(Boolean.TRUE)));
    }

    @DataPoints("fileNames2")
    public static String[] getSecondFileNames() {
        return new String[] { "secondFile.txt","secondFile1.txt" };
    }

    @Theory
    public void test2(@FromDataPoints("fileNames2") String fileName) throws Exception {
        System.out.println(fileName);
        assertThat(true, is(equalTo(Boolean.TRUE)));
    }

}

This should exactly solve your problem :-).

Tim Perry
  • 11,766
  • 1
  • 57
  • 85
2

You could implement your own ParameterSupplier, as explained here: http://blog.schauderhaft.de/2010/02/07/junit-theories/

superjos
  • 12,189
  • 6
  • 89
  • 134
1

Tests can be grouped into "fixtures", where a fixture is a set of code that shares the same setup. Put the tests for cases using the same datapoints together in the same class, so you have one class for each set of data.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
  • What I was looking for was a way to write all the test in a same class. Can we define two datapoints in one test class? – Shekhar Oct 01 '10 at 04:02
0

Have you looked at JUnitParams? If you just want to pass parameters to methods, which is all that your example shows, it's a much cleaner option.

@RunWith(JUnitParamsRunner.class)
public class DummyTest {

    public static String[] getFileNames() {
        return new String[] { "firstFile.txt","firstFile1.txt" };
    }

    @Theory
    public void test1(String fileName) throws Exception {
        System.out.println(fileName);
        assertThat(true, is(equalTo(Boolean.TRUE)));
    }

    @DataPoints
    public static String[] getSecondFileNames() {
        return new String[] { "secondFile.txt","secondFile1.txt" };
    }

    @Theory
    public void test2(String fileName) throws Exception {
        System.out.println(fileName);
        assertThat(true, is(equalTo(Boolean.TRUE)));
    }

}
ngreen
  • 1,559
  • 13
  • 22