Failing Maven test JUnit5 test programmatically

I do execute my Maven tests with JUnit5, where all the test classes have the

@ExtendWith({ProcessExtension.class})

annotation. This extension must fail test methods according to a special logic in the if it is the case. But I have no idea how.

The method

@Override
public void afterEach(ExtensionContext context) {
    ...
}

gets this context parameter but the is no way to fail the already executed test method. How could I notify the Maven that the method has failed?

KI

Upvotes: 0

Views: 498

Answers (1)

eee
eee

Reputation: 3458

I think you can use the fail method for this:

import static org.junit.jupiter.api.Assertions.fail;


public class ProcessExtension implements AfterEachCallback {

    @Override
    public void afterEach(ExtensionContext context) {
        boolean failTest = ...;

        if (failTest) {
            fail("test failed");
        }
    }
}

Upvotes: 1

Related Questions