Last active
February 5, 2019 00:45
-
-
Save javaverse/3eeb987420c265c0f98b3a800fdafb0b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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