Skip to content

Instantly share code, notes, and snippets.

@Legionivo
Created June 3, 2019 10:17
Show Gist options
  • Save Legionivo/23313f5998284e25af9a4eb6570a6360 to your computer and use it in GitHub Desktop.
Save Legionivo/23313f5998284e25af9a4eb6570a6360 to your computer and use it in GitHub Desktop.
/**
* Custom Extension which executes code only once before all tests are started and after all tests finished.
* This is temporary solution until https://github.com/junit-team/junit5/issues/456 will not be released
*/
public class SystemSetupExtension implements BeforeAllCallback, ExtensionContext.Store.CloseableResource {
private static boolean systemReady = false;
/**
* Separate method with 'synchronized static' required for make sure procedure will be executed
* only once across all simultaneously running threads
*/
synchronized private static void systemSetup() throws Exception {
// 'if' is used to make sure procedure will be executed only once, not before every class
if (!systemReady) {
systemReady = true;
initialSystemSetupExecutedOnce();
}
}
/**
* Initial setup of system. Including configuring services,
* adding calls to callrec, users to scorecard, call media files
*
* @param context junit context
*/
@Override
public void beforeAll(ExtensionContext context) throws Exception {
systemSetup();
context.getRoot().getStore(GLOBAL).put(systemReady, this);
}
/**
* CloseableResource implementation, adding value into GLOBAL context is required to registers a callback hook
* With such steps close() method will be executed only once in the end of test execution
*/
@Override
public void close() throws Exception {
// clean data from system
finalTearDownExecutedAfterAllTests();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment