3

Say I have a JUnit test case as:

@RunWith(Parameterized.class)
public class TestMyClass {

    @Parameter
    private int expected;
    @Parameter
    private int actual;  

    @Parameters
    public static Collection<Object[]> data() {
    return Arrays.asList(new Object[][] {     
             { 0,1 }, { 1,2 }, { 2,3 }, { 3,4 }, { 4,5 }, { 5,6 },{ 6,7 }  
       });
    }

    @Test
    public void test1 { //test }

    @Test
    public void test2 { //test }

}

I want to run test1 only with {0,1}, {1,2} and {2,3} and run test2 only with {3,4}, {4,5} {5,6}

How can I achieve this?

Edit: Parameters are read at Runtime from a file.

Anmol Gupta
  • 2,797
  • 9
  • 27
  • 43

3 Answers3

2

Seems that there is no way you can use different sets of parameters for different tests in once class using JUnit standart '@Parameters' stuff. You can try out junit-dataprovider. It is similar to TestNG data providers. For example:

@RunWith(DataProviderRunner.class)
public class TestMyClass {

    @DataProvider
    public static Object[][] firstDataset() {
    return new Object[][] {     
             { 3,4 }, { 4,5 }, { 5,6 },{ 6,7 }  
       };
    }


    @DataProvider
    public static Object[][] secondDataset() {
    return new Object[][] {     
             { 3,4 }, { 4,5 }, { 5,6 },{ 6,7 }  
       };
    }

    @Test
    @UseDataProvider("firstDataset")
    public void test1(int a, int b) { //test }

    @Test
    @UseDataProvider("secondDataset")
    public void test2(int a, int b) { //test }

}

Or you can create 2 classes for each test.

But I think using junit-dataprovider is more convenient.

pomkine
  • 1,605
  • 4
  • 18
  • 29
1

there is plenty of junit libraries that allows you do that. if you know your parameters up-front (looks like your case), parametrized testing with zohhak may be the simplest:

@RunWith(ZohhakRunner.class)
public class TestMyClass {      

    @TestWith({
        "1, 6".
        "2, 8",
        "3, 4"
    })
    public void test1(int actual, int expected) { //test }

    @TestWith({
        "2, 2".
        "0, -8",
        "7, 1"
    })
    public void test2(int actual, int expected) { //test }

}

if you need to build parameters in run-time (generating, reading from file etc.) then you can check things like junit-dataprovider or junit-params

piotrek
  • 13,982
  • 13
  • 79
  • 165
  • Thanks Piotrek, this helped. The dataset is actually getting fetched from a file so I am going with junit-dataprovider. – Anmol Gupta Sep 23 '15 at 12:18
1

You can use JUnit's Enclosed runner if you don't want to use another library:

@RunWith(Enclosed.class)
public class ATest {

  @RunWith(Parameterized.class)
  public static class TestMyClass {

    @Parameter
    private int expected;
    @Parameter
    private int actual;  

    @Parameters
    public static Collection<Object[]> data() {
      return Arrays.asList(new Object[][] {     
         { 0,1 }, { 1,2 }, { 2,3 }  
      });
    }

    @Test
    public void test1 {
      //test
    }
  }

  @RunWith(Parameterized.class)
  public static class TestMyClass {
    @Parameter
    private int expected;
    @Parameter
    private int actual;  

    @Parameters
    public static Collection<Object[]> data() {
      return Arrays.asList(new Object[][] {     
         { 3,4 }, { 4,5 }, { 5,6 },{ 6,7 }  
      });
    }

    @Test
    public void test2 {
      //test
    }
  }
}

By the way: you don't have to wrap the parametes with a List with JUnit 4.12.

@Parameters
public static Object[][] data() {
  return new Object[][] {     
     { 0,1 }, { 1,2 }, { 2,3 }  
  };
}
Stefan Birkner
  • 24,059
  • 12
  • 57
  • 72