2

I have a simple Junit 5 Siute with @BeforeAll and @AfterAll annotation. The Suite is executed but these methods are not executed before and after all classess are executed.

package demo;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.Suite;

@Suite
@SelectClasses({ demo.TestDemoClass1.class, demo.TestDemoClass2.class })

public class TestSuite {

    @BeforeAll
    public static void start() throws Exception {
        System.out.println("Before All from Suite1");
    }

    @AfterAll
    public static void end() throws Exception {
        System.out.println("After All from Suite1");
    }
}`
Progman
  • 16,827
  • 6
  • 33
  • 48
Ayesha Pathan
  • 63
  • 1
  • 8
  • I have added an answer if it solved the requirement i appreciate if you accept it as answer . – Lunatic Jan 22 '22 at 10:47
  • Maybe you want a `TestExecutionListener` instead? – Mark Rotteveel Jan 22 '22 at 11:23
  • You are mixing two things that cannot be mixed. As you can see from the imported packages Suite is processed by the Suite engine. Before/AfterAll are from Jupiter, that’s why these must be added to your test classes. – johanneslink Jan 22 '22 at 12:38
  • @johanneslink There is no annotation to work from Suite Engine?I searched a lot but nothign at Suite level – Ayesha Pathan Jan 22 '22 at 16:32
  • Also @Lunatic The steps mentioned in your link https://stackoverflow.com/questions/43282798/in-junit-5-how-to-run-code-before-all-tests here do not work when I add the ExtendWith on Suite class – Ayesha Pathan Jan 22 '22 at 18:39
  • @AyeshaPathan Not that I know of. And the same goes for ExtendWith, it's a Jupiter concept. – johanneslink Jan 22 '22 at 22:03
  • @johanneslink john is right same as i explained below , the BeforeAll and AfterAll executable in their current and every class that extended from them and Suite has entirely different engine . – Lunatic Jan 23 '22 at 07:31

1 Answers1

4

The @BeforeAll Denotes that the annotated method should be executed before all @Test, @RepeatedTest, @ParameterizedTest, and @TestFactory methods in the current class; analogous to JUnit 4’s @BeforeClass. Such methods are inherited (unless they are hidden or overridden) and must be static (unless the "per-class" test instance lifecycle is used).

Same story holds for @AfterAll so consider to move start() and end() methods to your TestDemoClass1 and TestDemoClass2 classes or extend your test classes to some BaseClass and keep this methods inside it.

public class BaseTest {

    @BeforeAll
    public static void start() throws Exception {
        System.out.println("Before All from Suite1");
    }

    @AfterAll
    public static void end() throws Exception {
        System.out.println("After All from Suite1");
    }
}

And your test classes

public class SampleTestOne extends BaseTest {
    @Test
    public void testOne(){
        System.out.println("Extended test passed!");
    }

    @Test
    public void testTwo(){
        System.out.println("Extended test passed!");
    }
}
public class SampleTestTwo extends SampleTestOne {
    @Test
    public void testThree(){
        System.out.println("test passed");
    }
}

And the final result test result

Alternatively you can create custom extension to run code before all your test context which fully explained here.

Lunatic
  • 1,519
  • 8
  • 24
  • Extending base class still executes before and after each class in a Suite. I need it to execute once before all tests and after all tests. So this solution did not work in my case – Ayesha Pathan Jan 22 '22 at 16:30
  • @AyeshaPathan no you are wrong if you create hierarchy extended class you will get you desired result, i attached all the classes and result to solution one more ;) – Lunatic Jan 23 '22 at 07:39
  • Are you using JUNIT 5 to execute above sample code. I have same clas structure as you and I still get beliw result:Before All from Suite1 Test 1 from DemoClass 1 Test 2 from DemoClass 1 After All from Suite1 Before All from Suite1 Test 1 from DemoClass 2 Test 2 from DemoClass 2 After All from Suite1 – Ayesha Pathan Jan 23 '22 at 15:40
  • Yes i do !, dear Ayesha please accept answer if you found the requirements . – Lunatic Jan 23 '22 at 15:54
  • I see what was the issue with my code. The function name being same in my code was executing only from class 2 or executing all. Anyway I can accept teh ans for the case where we dont want this to be executed using Suite. My original post about executing BeforeAll and AfterAll at Suite level is still an issue. But this can work as kind of workaround for now. Thanks @Lunatic!!! – Ayesha Pathan Jan 23 '22 at 16:08