JUNIT 4
You can create parent class with only one test. And extend all classes from it.
private static final AtomicBoolean REQUIRES_LOGIN = new AtomicBoolean(true);
@Test
void setup() {
if (REQUIRES_LOGIN.getAndSet(false)) {
System.out.println("Doing login here");
}
}
JUNIT 5
You can register global extension in following file.
META-INF/services/org.junit.jupiter.api.extension.Extension
demo.api.BeforeAllExtension
Extension class:
public class BeforeAllExtension implements BeforeAllCallback {
private static final AtomicBoolean REQUIRES_LOGIN = new AtomicBoolean(true);
@Override
public void beforeAll(ExtensionContext extensionContext) {
if (REQUIRES_LOGIN.getAndSet(false)) {
System.out.println("Doing login here");
}
}
}
Checkout how to turn on extension registration here