Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Skip tests at runtime when using SpringJUnit4ClassRunner

Tags:

junit

spring

In my project, I have acceptance tests which take a long time to run. When I add new features to the code and write new tests, I want to skip some existing test cases for the sake of time. I am using Spring 3 and junit 4 using SpringJUnit4ClassRunner. My idea is to create an annotation (@Skip or something) for the test class. I am guessing I would have to modify the runner to look for this annotation and determine from system properties if a test class should be included while testing. My question is, is this easily done? Or am I missing an existing functionality somewhere which will help me?

Thanks. Eric

like image 816
badgerduke Avatar asked Dec 02 '22 20:12

badgerduke


1 Answers

Annotate your class (or unit test methods) with @Ignore in Junit 4 and @Disabled in Junit 5 to prevent the annotated class or unit test from being executed.

Ignoring a test class:

@Ignore
public class MyTests {
    @Test
    public void test1() {
       assertTrue(true);
    }
}

Ignoring a single unit test;

public class MyTests {
    @Test
    public void test1() {
         assertTrue(true);
    }

    @Ignore("Takes too long...")
    @Test
    public void longRunningTest() {
        ....
    }

    @Test
    public void test2() {
         assertTrue(true);
    }
}
like image 61
James Avatar answered Dec 17 '22 18:12

James