Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A proper way to conditionally ignore tests in JUnit5?

I've been spotting a lot of these in my project's tests:

    @Test
    void someTest() throws IOException {
        if (checkIfTestIsDisabled(SOME_FLAG)) return;
        //... the test starts now

Is there an alternative to adding a line at the beginning of each test? For example in JUnit4 there is an old project that provides an annotation @RunIf(somecondition) and I was wondering if there is something similar in JUnit5?

Thank you for your attention.

like image 480
João Matos Avatar asked Jan 29 '26 06:01

João Matos


1 Answers

Tests can be disabled with @DisabledIf and a custom condition.

@Test
@DisabledIf("customCondition")
void disabled() {
    // ...
}

boolean customCondition() {
    return true;
}

See also the user guide about custom conditions.

like image 194
Roland Weisleder Avatar answered Feb 01 '26 04:02

Roland Weisleder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!