2

I've 5 test methods in a test class annotated with @Test. I've @Before and @After methods too. Is there a way to skip the method with @After only for the first Test method and run it for the rest?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Mercenary
  • 2,106
  • 6
  • 34
  • 43
  • What exactly are you trying to achieve by skipping the `@After` method? – RJo Dec 11 '12 at 07:18
  • 3
    As far as I know, you can't skip the execution of a method marked with @After. You should probably think twice before doing it for test readability. However, if you really, really want to do it you could add a `boolean runAfter` field to your test class and set it to `false` in the first method. Furthermore, in the `@After` method add `if (runAfter) { /* after code here */ }`. – RJo Dec 11 '12 at 07:24
  • Thanks a lot. That really helped. Also need to know when I run couple of tests, the first test always takes a lot more time to get executed when compared to other test methods. Is there any reason for this? – Mercenary Dec 11 '12 at 09:43
  • 1
    Mocking frameworks such as Mockito takes a few hundred milliseconds to start the first time it is used ni an execution. – Markus Dec 11 '12 at 12:01

2 Answers2

3
  1. If you want it to be a bit universal, set a variable "afterToBeLaunched" to false in the @Before. And in the every test that needs @After, set it to true. Of course check it at the start of @After and if it is false, return the method.
  2. If you use JUnit after 4.7 version, use @Rules. ( Apply '@Rule' after each '@Test' and before each '@After' in JUnit ) You can use Rule instead of @After and using TestName, you can read the name of the test there and do finishing actions according to the currently ending test.
Community
  • 1
  • 1
Gangnus
  • 24,044
  • 16
  • 90
  • 149
2

This question was asked a while ago, but with version 5 of JUnit (JUnit Jupiter), we can use @Tag and TestInfo to accomplish the same thing. The idea is to add a tag to the test(s) that do not need to have @AfterEach executed and use TestInfo to determine if the current test contains that tag. In code, this might look something like

class TestSuite {
    @BeforeEach
    void beforeEach() {
        System.out.println("BeforeEach");
        // Set-up logic.
    }

    @AfterEach
    void afterEach(TestInfo testInfo) {
        if(testInfo.getTags().contains("SkipCleanup")) {
            return;
        }

        System.out.println("AfterEach");

        // Perform cleanup logic.
    }

    @Test
    @Tag("SkipCleanup")
    void myTestWithoutCleanup() {
        System.out.println("First Test");
        // Test logic.
    }

    @Test
    void myTestWithCleanup() {
        System.out.println("Second Test");
        // Test logic.
    }

    @Test
    void myOtherTestWithCleanup() {
        System.out.println("Third Test");
        // Test logic.
    }
}

The output for the above would look like

BeforeEach

First Test

BeforeEach

Second Test

AfterEach

BeforeEach

Third Test

AfterEach

Community
  • 1
  • 1
c1moore
  • 1,827
  • 17
  • 27