Inheritance (or alternative) in parameterized jUnit tests

I would like to do something like this in jUnit:

@Runwith(Parameterized.class)
public abstract class BaseTest {

     protected abstract List<Object[]> extraParams();
     protected abstract ClassUnderTest testObject;

     @Parameters
     public Collection<Object[]> data() {
         List<Object> params = ...; // a standard set of tests
         params.addAll(extraParams());
         return params;
     }

     @Test
     public doTest() {
         // assert things about testObject
     }
}

public class ConcreteTest extends BaseTest {
     protected ClassUnderTest = new ConcreteClass(...);
     protected List<Object[]) extraParams() {
         List<Object> extraParams = ...; // tests specific to this concrete type
         return extraParams;
     }
}

Thus, expanding this class, I run a bunch of standard tests against the test object, as well as some additional ones specified in a particular class.

However, jUnit requires the method to @Parametersbe static. How else can I accurately achieve the goal, have a set of standard parameters plus additional ones in specific classes?

The best thing I've come up with so far is to have un-annotated Collection<Object[]> standardParams()in an abstract class and require the subclass to contain the method:

 @Parameters
 public Collection<Object[]> data() {
     List<Object> params = standardParams();
     params.addAll(...); // extra params
     return params;
 }

... but this is not as neat as we would like, since he bears too much responsibility for the author of the subclass.

+4
1

JUnit , @Parameters , , No public static parameters method on class Exception.

, org.junit.rules.TestRule, :

BaseTest

public abstract class BaseTest {

    @Rule
    public MyBaseTestRule myProjectTestRule = new MyBaseTestRule(data());

    protected abstract List<Object[]> extraParams();

    public List<Object[]> data() {  
        List<Object[]> listTotal = new ArrayList<>();
        listTotal.addAll(extraParams());
        //add your base test data here
        return listTotal;
    }

    public abstract List<Object[]> extraParams();
}

ConcreteTest

public class ConcreteTest extends BaseTest  {

    @Override
    public List<Object[]> extraParams() {
        List<Object[]> list = ...//set up data here
        return list;
    }   

    @Test
    public void test1() {
        Object[] testData = myProjectTestRule.getTestData();
        //use the test data for the test
        //Example: Assume addition of two integers scenario and data 
        //data[0] expectedresult, data[1],[2] inputs
        //Assert.assertEquals((int)data[0], (int)(data[1]+data[2]));

    }

    //add other test cases
}

MyBaseTestRule:

import java.util.List;

import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

public class MyBaseTestRule implements TestRule { 

    private final List<Object[]> totalTestData;

    private final int totalTestsSize;

    private int currentTestIndex;

    public MyProjectTestRule(List<Object[]> list) {
      this.totalTestsSize = list.size();
      this.totalTestData = list;
   }

   public Object[] getTestData(){
      return totalTestData.get(currentTestIndex);
   }

   @Override
   public Statement apply(Statement stmt, Description desc) {

      return new Statement() {

         @Override
         public void evaluate() throws Throwable {
            for(int i=0; i<totalTestsSize; i++) {
                currentTestIndex = i;
                stmt.evaluate();
            }
         }
       };
    }
}
+2

Source: https://habr.com/ru/post/1659205/


All Articles