Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. import java.lang.reflect.Method;
  2. import java.util.logging.Logger;
  3.  
  4. import org.junit.jupiter.api.extension.AfterTestExecutionCallback;
  5. import org.junit.jupiter.api.extension.BeforeTestExecutionCallback;
  6. import org.junit.jupiter.api.extension.ExtensionContext;
  7. import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
  8. import org.junit.jupiter.api.extension.ExtensionContext.Store;
  9.  
  10. public class TimingExtension implements BeforeTestExecutionCallback, AfterTestExecutionCallback {
  11.  
  12. private static final Logger logger = Logger.getLogger(TimingExtension.class.getName());
  13.  
  14. private static final String START_TIME = "start time";
  15.  
  16. @Override
  17. public void beforeTestExecution(ExtensionContext context) throws Exception {
  18. getStore(context).put(START_TIME, System.currentTimeMillis());
  19. }
  20.  
  21. @Override
  22. public void afterTestExecution(ExtensionContext context) throws Exception {
  23. Method testMethod = context.getRequiredTestMethod();
  24. long startTime = getStore(context).remove(START_TIME, long.class);
  25. long duration = System.currentTimeMillis() - startTime;
  26.  
  27. logger.info(() ->
  28. String.format("Method [%s] took %s ms.", testMethod.getName(), duration));
  29. }
  30.  
  31. private Store getStore(ExtensionContext context) {
  32. return context.getStore(Namespace.create(getClass(), context.getRequiredTestMethod()));
  33. }
  34.  
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement