Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit @Ignore all other tests (@IgnoreOther ?)

I'm testing extensively with JUnit and sometimes - while debugging my code - I want (temporary) only run a single @Test of my @RunWith(Arquillian.class) test class. Currently I'm adding a @Ignore to all other tests and wondering if something like @IgnoreOther does exist.

Are there better solutions to ignore all other tests?

like image 285
Thor Avatar asked Nov 29 '11 18:11

Thor


2 Answers

The simplest way is to replace all @Test to //###$$$@Test. Then when your debugging is finished replace //###$$$@Test to @Test.

Moreover typically IDEs allow running one test only. For example in Eclipse you can do it from Outline view.

like image 30
AlexR Avatar answered Oct 17 '22 18:10

AlexR


Just my two cents. You can try to use Junit Rules as @srkavin suggested.

Here is an example.

package org.foo.bar;

import org.junit.rules.MethodRule;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;

public class SingleTestRule implements MethodRule {
    private String applyMethod;
    public SingleTestRule(String applyMethod) {
        this.applyMethod = applyMethod;
    }
    @Override
    public Statement apply(final Statement statement, final FrameworkMethod method, final Object target) {
        return new Statement() {
            @Override
            public void evaluate() throws Throwable {
                if (applyMethod.equals(method.getName())) {
                    statement.evaluate();
                }
            }
        };
    }
}

package org.foo.bar;

import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;

public class IgnoreAllTest {

    @Rule
    public SingleTestRule test = new SingleTestRule("test1");

    @Test
    public void test1() throws Exception {
        System.out.println("test1");
    }

    @Test
    public void test2() throws Exception {
        Assert.fail("test2");
    }

    @Test
    public void test3() throws Exception {
        Assert.fail("test3");
    }

}
like image 86
szhem Avatar answered Oct 17 '22 18:10

szhem