Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse Junit run configuration that takes the current selected test class as an arg?

Instead of constantly creating identical debug configuraitons for my test cases, I would like to be able to simply save a few arguments common across all my Junit tests, right click on a specific test, then run that single run config. IE I would like a single debug configuration that can take as an argument the current selected test case instead of requiring me to manually specify it every time in the JUnit run configuration. My only options in the dialog appear to be either to specify a single test class or run all the tests in the project. As a result, Eclipse is littered with dozens of run configurations for all my test cases.

Instead of specifying a specific test class, I'd like it to specify a variable like ${container_loc} or ${resource_loc} for the class to run as in this question. Is there a variable in Eclipse that specifies the current selected Java class that I could place in the test class field in the dialog?

A specific example where this is useful is when running the Lucene unit tests. There's lots of arguments you can specify to customize the tests, some of which like -ea are required. Everytime I want to test a specific test case in Lucene in Eclipse, I have to manually setup these variables in the Eclipse debug config dialog :-/.

like image 983
Doug T. Avatar asked May 08 '13 14:05

Doug T.


1 Answers

Have you looked at Parameterized Tests in JUnit? Here is an example:

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class ParamTest {
    @Parameters(name = "{index}: fib({0})={1}")
    public static Iterable<Object[]> data() {
        return Arrays.asList(new Object[][] { 
                 { 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 }
           });
    }

    private int input;
    private int expected;

    public ParamTest(int input, int expected) {
        this.input = input;
        this.expected = expected;
    }

    @Test
    public void test() {
        Assert.assertEquals(expected, input);
    }
}

If you just want to run one test at a time you can use private variables as in:

    public class MultipleTest {
    private int x;
    private int y;

    public void test1(){
        Assert.assertEquals(x, y);
    }
    public void test2(){
        Assert.assertTrue(x  >y);
    }

    public void args1(){
        x=10; y=1;
    }
    public void args2(){
        x=1;y=1;
    }
    public void args3(){
        x=1;y=10;
    }
    @Test
    public void testArgs11(){
        args1();
        test1();
    }
    @Test
    public void testArgs21(){
        args2();
        test1();
    }
    @Test
    public void testArgs31(){
        args3();
        test1();
    }
    @Test
    public void testArgs12(){
        args1();
        test2();
    }
    @Test
    public void testArgs22(){
        args2();
        test2();
    }
    @Test
    public void testArgs32(){
        args3();
        test2();
    }
}
like image 134
dan b Avatar answered Oct 01 '22 12:10

dan b