Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@parameters method is executed before @beforeclass method

Tags:

java

junit

junit4

I'm using "parametrized" feature of junit 4 and I noticed that @parameters method is executed before @beforeclass method. This is creating a problem for me because the parameters i'm passing to the test cases via @parameters depends on the code initialize in the @beforeclass method. For example

@RunWith(Parameterized.class)
public class TestOtherClass {

    String argument;
    private static boolean initializeThis;

    public TestOtherClass(String parameter) throws Exception {
        argument=parameter;
    }

    @BeforeClass
    public static void doSetup() {
        System.out.println("Doing setup before class...");
        initializeThis=true; // true or false, based on some condition
    }

    @Test
    public void otherTest() {
        System.out.println("Other test: " + argument);
    }

    @Parameters
    public static Collection<Object[]> getData(){
        System.out.println("Inside parameter");
        String addThis;
        if(initializeThis)
            addThis="adding true";
        else
            addThis="adding false";

        Object[] para1 = new Object[]{"First parameter :: " + addThis};
        Object[] para2 = new Object[]{"Second parameter :: " + addThis};

        Collection<Object[]> classNames = new ArrayList<Object[]>();
        classNames.add(para1);
        classNames.add(para2);
        return classNames;
    }
}

Now, I'm initializing variable "initializeThis" to true in @beforeclass method but (surprisingly) when I executed the test case it prints

Other test: First parameter :: adding false
Other test: Second parameter :: adding false

That is something not expected.
My question is; is there any way to execute the @beforeclass method before @parameters, can we do this is in junit 4?

like image 203
user85 Avatar asked Jul 11 '12 10:07

user85


3 Answers

I would use just plain old java static {..} initializer instead of @BeforeClass, e.g.:

@RunWith(Parameterized.class)
public class TestOtherClass {

    String argument;
    private static boolean initializeThis;

    public TestOtherClass(String parameter) throws Exception {
        argument=parameter;
    }

    static {
        doSetup();
    }

    // @BeforeClass
    public static void doSetup() {
        System.out.println("Doing setup before class...");
        initializeThis=true; // true or false, based on some condition
    }

    @Test
    public void otherTest() {
        System.out.println("Other test: " + argument);
    }

    @Parameters
    public static Collection<Object[]> getData(){
        System.out.println("Inside parameter");
        String addThis;
        if(initializeThis)
            addThis="adding true";
        else
            addThis="adding false";

        Object[] para1 = new Object[]{"First parameter :: " + addThis};
        Object[] para2 = new Object[]{"Second parameter :: " + addThis};

        Collection<Object[]> classNames = new ArrayList<Object[]>();
        classNames.add(para1);
        classNames.add(para2);
        return classNames;
    }
}

Only drawback I know is that classes inherited from this won't be able to override static initializer, while @BeforeClass gives some freedom in this aspect;

like image 188
Vilnis Avatar answered Nov 17 '22 16:11

Vilnis


This is old question but I had the same issue recently. It strikes me that none of the solutions seem to go for the most obvious workaround - calling the @BeforeClass method in the @Parameters method. The latter is static and executed only once - before any of the tests have been run. So, it is for all intents and purposes a @BeforeClass method even though it is not annotated as such. More details can be found here: http://feraldeveloper.blogspot.co.uk/2013/12/beforeclass-and-parametrized-junit-tests.html

like image 4
Nick Avatar answered Nov 17 '22 17:11

Nick


JUnit creates a Runner for each item in the parameter list, a Runner is what encapsulates the test method. So the @Parameters will always get executed before the @BeforeClass.

However, you can combine the @Parameterized with Assume. You always include all of the parameters in your list, whether or not you intend executing it. Then in the test method, add the assumeTrue() which tests against the initializeThis value.

@RunWith(Parameterized.class)
public class TestOtherClassAssume {
  private final String argument;
  private final boolean initializeThisTest;
  private static boolean initializeThis;

  @Parameters
  public static Collection<Object[]> getData(){
    System.out.println("Inside parameter");

    return Arrays.asList(new Object[][] {
      { false, "First" },
      { true, "Second" },
    });
  }

  public TestOtherClassAssume(boolean initializeThisTest, String argument) {
    this.initializeThisTest = initializeThisTest;
    this.argument = argument;
  }

  @BeforeClass
  public static void doSetup() {
    System.out.println("Doing setup before class...");
    initializeThis = true; // true or false, based on some condition
  }

  @Test
  public void otherTest() {
    Assume.assumeTrue(initializeThis == initializeThisTest);
    System.out.println("Other test: " + argument);
  }
}

The output from this is:

Inside parameter
Doing setup before class...
Other test: Second
like image 1
Matthew Farwell Avatar answered Nov 17 '22 15:11

Matthew Farwell