12

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?

Thor
  • 6,607
  • 13
  • 62
  • 96
  • This one speaks about runtime ignoring http://programmaticallyspeaking.blogspot.com/2008/10/run-time-equivalent-to-junits-ignore.html – mprabhat Nov 29 '11 at 18:53
  • 4
    Does your IDE not allow you to simply highlight the test you're interested in and only run that? – Duncan Nov 29 '11 at 19:00
  • I don't know of an annotation like that, but I do the same thing quite a bit. Maybe a good idea for a feature request? – JohnnyK Nov 29 '11 at 19:01
  • @Duncan: I often execute Arquillian tests from the console. – Thor Nov 30 '11 at 05:50
  • @Jakrabbit: The tracker for junit hosted in Sourceforge is only visible for group members. I'm not sure where to post such a feature request. – Thor Nov 30 '11 at 05:51

4 Answers4

10

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.

AlexR
  • 114,158
  • 16
  • 130
  • 208
10

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");
    }

}
szhem
  • 4,672
  • 2
  • 18
  • 30
5

Test rules (JUnit 4.7+) will help. For example, you can write a rule that ignores all @Test methods except one with a specific name.

approxiblue
  • 6,982
  • 16
  • 51
  • 59
srkavin
  • 1,152
  • 7
  • 17
4

The answer from srkavin (and mijer) is correct, but the code is deprecated from JUnit 4.9. The interface and the method signature have changed. I want to provide this for others interested in this issue.

public class IgnoreOtherRule implements TestRule
{
  private String applyMethod;

  public IgnoreOtherRule(String applyMethod){
    this.applyMethod = applyMethod;
  }

  @Override
  public Statement apply(final Statement statement, final Description description)
  {
    return new Statement()
    {
        @Override
        public void evaluate() throws Throwable {
            if (applyMethod.equals(description.getMethodName())) {
                statement.evaluate();
            }
        }
    };
  }
}
Community
  • 1
  • 1
Thor
  • 6,607
  • 13
  • 62
  • 96