File tree Expand file tree Collapse file tree 2 files changed +41
-0
lines changed
src/test/java/guru/springframework/sfgpetclinic Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change 1+ package guru .springframework .sfgpetclinic .junitextensions ;
2+
3+ import org .junit .jupiter .api .extension .AfterTestExecutionCallback ;
4+ import org .junit .jupiter .api .extension .BeforeTestExecutionCallback ;
5+ import org .junit .jupiter .api .extension .ExtensionContext ;
6+
7+ import java .lang .reflect .Method ;
8+ import java .util .logging .Logger ;
9+
10+ /**
11+ * Original source - https://junit.org/junit5/docs/current/user-guide/#extensions-lifecycle-callbacks-timing-extension
12+ *
13+ * Created by jt on 2018-10-28.
14+ */
15+ public class TimingExtension implements BeforeTestExecutionCallback , AfterTestExecutionCallback {
16+
17+ private static final Logger logger = Logger .getLogger (TimingExtension .class .getName ());
18+
19+ private static final String START_TIME = "start time" ;
20+
21+ @ Override
22+ public void beforeTestExecution (ExtensionContext context ) throws Exception {
23+ getStore (context ).put (START_TIME , System .currentTimeMillis ());
24+ }
25+
26+ @ Override
27+ public void afterTestExecution (ExtensionContext context ) throws Exception {
28+ Method testMethod = context .getRequiredTestMethod ();
29+ long startTime = getStore (context ).remove (START_TIME , long .class );
30+ long duration = System .currentTimeMillis () - startTime ;
31+
32+ logger .info (() -> String .format ("Method [%s] took %s ms." , testMethod .getName (), duration ));
33+ }
34+
35+ private ExtensionContext .Store getStore (ExtensionContext context ) {
36+ return context .getStore (ExtensionContext .Namespace .create (getClass (), context .getRequiredTestMethod ()));
37+ }
38+ }
Original file line number Diff line number Diff line change 11package guru .springframework .sfgpetclinic .services .springdatajpa ;
22
3+ import guru .springframework .sfgpetclinic .junitextensions .TimingExtension ;
34import org .junit .jupiter .api .BeforeEach ;
45import org .junit .jupiter .api .Test ;
6+ import org .junit .jupiter .api .extension .ExtendWith ;
57
68import static org .junit .jupiter .api .Assertions .*;
79
10+ @ ExtendWith (TimingExtension .class )
811class PetTypeSDJpaServiceIT {
912
1013 @ BeforeEach
You can’t perform that action at this time.
0 commit comments