Skip to content

Instantly share code, notes, and snippets.

@javaverse
Last active February 5, 2019 00:45
Show Gist options
  • Save javaverse/3eeb987420c265c0f98b3a800fdafb0b to your computer and use it in GitHub Desktop.
Save javaverse/3eeb987420c265c0f98b3a800fdafb0b to your computer and use it in GitHub Desktop.
import java.lang.reflect.Method;
import java.util.logging.Logger;
import org.junit.jupiter.api.extension.AfterTestExecutionCallback;
import org.junit.jupiter.api.extension.BeforeTestExecutionCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
import org.junit.jupiter.api.extension.ExtensionContext.Store;
public class StopwatchExtension implements BeforeAllCallback, AfterAllCallback {
private static final Logger logger = Logger.getLogger(TimingExtension.class.getName());
private static final String START_TIME = "start time";
@Override
public void beforeTestExecution(ExtensionContext context) throws Exception {
getStore(context).put(START_TIME, System.currentTimeMillis());
}
@Override
public void afterTestExecution(ExtensionContext context) throws Exception {
Method testMethod = context.getRequiredTestMethod();
long startTime = getStore(context).remove(START_TIME, long.class);
long duration = System.currentTimeMillis() - startTime;
logger.info(() -> String.format(">> Execution Time =[%s] ms.", duration));
}
private ExtensionContext.Store getStore(ExtensionContext context) {
return context.getRoot().getStore(
ExtensionContext.Namespace
.create("com.javaverse.course.springboot.zero.2.ninja"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment