Event Dispatcher  v0.1.6-beta.1
Project that offers a small library that allows to subscribe and fire events.
TimingExtension.java
Go to the documentation of this file.
1 package com.fermod.extension;
2 
3 import java.lang.reflect.Method;
4 
5 import org.apache.logging.log4j.LogManager;
6 import org.apache.logging.log4j.Logger;
7 import org.junit.jupiter.api.extension.AfterTestExecutionCallback;
8 import org.junit.jupiter.api.extension.BeforeTestExecutionCallback;
9 import org.junit.jupiter.api.extension.ExtensionContext;
10 import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
11 import org.junit.jupiter.api.extension.ExtensionContext.Store;
12 
25 public class TimingExtension implements BeforeTestExecutionCallback, AfterTestExecutionCallback {
26 
27  private static final Logger LOGGER = LogManager.getLogger(TimingExtension.class);
28 
29  private static final String START_TIME = "start time";
30 
43  @Override
44  public void beforeTestExecution(ExtensionContext context) throws Exception {
45  getStore(context).put(START_TIME, System.currentTimeMillis());
46  }
47 
60  @Override
61  public void afterTestExecution(ExtensionContext context) throws Exception {
62  Method testMethod = context.getRequiredTestMethod();
63  long startTime = getStore(context).remove(START_TIME, long.class);
64  long duration = System.currentTimeMillis() - startTime;
65 
66  LOGGER.info(String.format("Method \"%s\" took %s ms.", testMethod.getName(), duration));
67  }
68 
79  private Store getStore(ExtensionContext context) {
80  return context.getStore(Namespace.create(getClass(), context.getRequiredTestMethod()));
81  }
82 
83 }
void afterTestExecution(ExtensionContext context)
This callback is invoked immediately after each test has been executed, and its only purpose is to st...
void beforeTestExecution(ExtensionContext context)
This callback is invoked immediately after each test has been executed, and its only purpose is to st...
Store getStore(ExtensionContext context)
Returns the.
The TimingExtension class implements the BeforeTestExecutionCallback and the AfterTestExecutionCallba...