11package ru .javawebinar .topjava .service ;
22
3- import org .junit .AfterClass ;
3+ import org .junit .ClassRule ;
44import org .junit .Rule ;
55import org .junit .rules .ExpectedException ;
6+ import org .junit .rules .ExternalResource ;
67import org .junit .rules .Stopwatch ;
7- import org .junit .runner .Description ;
88import org .junit .runner .RunWith ;
9- import org .slf4j .Logger ;
109import org .springframework .test .context .ActiveProfiles ;
1110import org .springframework .test .context .ContextConfiguration ;
1211import org .springframework .test .context .jdbc .Sql ;
1312import org .springframework .test .context .jdbc .SqlConfig ;
1413import org .springframework .test .context .junit4 .SpringRunner ;
1514import ru .javawebinar .topjava .ActiveDbProfileResolver ;
16-
17- import java .util .concurrent .TimeUnit ;
18-
19- import static org .slf4j .LoggerFactory .getLogger ;
15+ import ru .javawebinar .topjava .TimingRules ;
2016
2117@ ContextConfiguration ({
2218 "classpath:spring/spring-app.xml" ,
2622@ Sql (scripts = "classpath:db/populateDB.sql" , config = @ SqlConfig (encoding = "UTF-8" ))
2723@ ActiveProfiles (resolver = ActiveDbProfileResolver .class )
2824abstract public class AbstractServiceTest {
29- private static final Logger log = getLogger ("result" );
30-
31- private static StringBuilder results = new StringBuilder ();
32-
33- // https://dzone.com/articles/applying-new-jdk-11-string-methods
34- private static String DELIM = "-" .repeat (103 );
25+ @ ClassRule
26+ public static ExternalResource summary = TimingRules .SUMMARY ;
3527
3628 @ Rule
37- public ExpectedException thrown = ExpectedException . none () ;
29+ public Stopwatch stopwatch = TimingRules . STOPWATCH ;
3830
3931 @ Rule
40- // http://stackoverflow.com/questions/14892125/what-is-the-best-practice-to-determine-the-execution-time-of-the-bussiness-relev
41- public Stopwatch stopwatch = new Stopwatch () {
42- @ Override
43- protected void finished (long nanos , Description description ) {
44- String result = String .format ("%-95s %7d" , description .getDisplayName (), TimeUnit .NANOSECONDS .toMillis (nanos ));
45- results .append (result ).append ('\n' );
46- log .info (result + " ms\n " );
47- }
48- };
49-
50-
51- @ AfterClass
52- public static void printResult () {
53- log .info ("\n " + DELIM +
54- "\n Test Duration, ms" +
55- "\n " + DELIM + "\n " + results + DELIM + "\n " );
56- results .setLength (0 );
57- }
32+ public ExpectedException thrown = ExpectedException .none ();
5833}
0 commit comments