11package org .jooby ;
22
3+ import com .google .common .collect .ImmutableList ;
4+ import com .typesafe .config .Config ;
35import static org .easymock .EasyMock .expect ;
6+ import org .jooby .test .MockUnit ;
47import static org .junit .Assert .assertArrayEquals ;
58import static org .junit .Assert .assertEquals ;
6-
7- import java .io .PrintWriter ;
8- import java .io .StringWriter ;
9- import java .util .Map ;
10-
11- import com .typesafe .config .Config ;
12- import org .jooby .test .MockUnit ;
139import org .junit .Test ;
1410import org .junit .runner .RunWith ;
1511import org .powermock .core .classloader .annotations .PrepareForTest ;
1612import org .powermock .modules .junit4 .PowerMockRunner ;
1713import org .slf4j .Logger ;
1814import org .slf4j .LoggerFactory ;
1915
20- import com .google .common .collect .ImmutableList ;
16+ import java .io .PrintWriter ;
17+ import java .io .StringWriter ;
18+ import java .util .Map ;
2119
2220@ RunWith (PowerMockRunner .class )
23- @ PrepareForTest ({Err .DefHandler .class , LoggerFactory .class })
21+ @ PrepareForTest ({Err .DefHandler .class , LoggerFactory .class })
2422public class DefaultErrHandlerTest {
2523
26- @ SuppressWarnings ({"unchecked" })
24+ @ SuppressWarnings ({"unchecked" })
2725 @ Test
2826 public void handleNoErrMessage () throws Exception {
2927 Err ex = new Err (500 );
@@ -33,33 +31,7 @@ public void handleNoErrMessage() throws Exception {
3331 String [] stacktrace = writer .toString ().replace ("\r " , "" ).split ("\\ n" );
3432
3533 new MockUnit (Request .class , Response .class , Route .class , Config .class , Env .class )
36- .expect (unit -> {
37- Logger log = unit .mock (Logger .class );
38- log .error ("execution of: {}{} resulted in exception\n Route:\n {}\n \n Stacktrace:" , "GET" ,
39- "/path" , "route" , ex );
40-
41- unit .mockStatic (LoggerFactory .class );
42- expect (LoggerFactory .getLogger (Err .class )).andReturn (log );
43-
44- Route route = unit .get (Route .class );
45- expect (route .print (6 )).andReturn ("route" );
46-
47- Config conf = unit .get (Config .class );
48- Env env = unit .get (Env .class );
49- expect (env .name ()).andReturn ("dev" );
50-
51- Request req = unit .get (Request .class );
52-
53- expect (req .require (Config .class )).andReturn (conf );
54- expect (req .require (Env .class )).andReturn (env );
55- expect (req .path ()).andReturn ("/path" );
56- expect (req .method ()).andReturn ("GET" );
57- expect (req .route ()).andReturn (route );
58-
59- Response rsp = unit .get (Response .class );
60-
61- rsp .send (unit .capture (Result .class ));
62- })
34+ .expect (handleErr (ex ,true ))
6335 .run (unit -> {
6436
6537 Request req = unit .get (Request .class );
@@ -78,46 +50,49 @@ public void handleNoErrMessage() throws Exception {
7850 });
7951 }
8052
81- @ SuppressWarnings ({"unchecked" })
82- @ Test
83- public void handleWithErrMessage () throws Exception {
84- Err ex = new Err (500 , "Something something dark" );
53+ private MockUnit .Block handleErr (Throwable ex , boolean stacktrace ) {
54+ return unit -> {
55+ Logger log = unit .mock (Logger .class );
56+ log .error ("execution of: {}{} resulted in exception\n Route:\n {}\n \n Stacktrace:" , "GET" ,
57+ "/path" , "route" , ex );
8558
86- StringWriter writer = new StringWriter ();
87- ex .printStackTrace (new PrintWriter (writer ));
88- String [] stacktrace = writer .toString ().replace ("\r " , "" ).split ("\\ n" );
59+ unit .mockStatic (LoggerFactory .class );
60+ expect (LoggerFactory .getLogger (Err .class )).andReturn (log );
8961
90- new MockUnit (Request .class , Response .class , Route .class , Env .class , Config .class )
91- .expect (unit -> {
92- Logger log = unit .mock (Logger .class );
93- log .error ("execution of: {}{} resulted in exception\n Route:\n {}\n \n Stacktrace:" , "GET" ,
94- "/path" , "route" , ex );
62+ Route route = unit .get (Route .class );
63+ expect (route .print (6 )).andReturn ("route" );
9564
96- unit .mockStatic (LoggerFactory .class );
97- expect (LoggerFactory .getLogger (Err .class )).andReturn (log );
65+ Config conf = unit .get (Config .class );
66+ expect (conf .getBoolean ("err.stacktrace" )).andReturn (stacktrace );
67+ Env env = unit .get (Env .class );
68+ expect (env .name ()).andReturn ("dev" );
9869
99- Route route = unit .get (Route .class );
100- expect (route .print (6 )).andReturn ("route" );
70+ Request req = unit .get (Request .class );
10171
102- Config conf = unit .get (Config .class );
103- Env env = unit .get (Env .class );
104- expect (env .name ()).andReturn ("dev" );
72+ expect (req .require (Config .class )).andReturn (conf );
73+ expect (req .require (Env .class )).andReturn (env );
74+ expect (req .path ()).andReturn ("/path" );
75+ expect (req .method ()).andReturn ("GET" );
76+ expect (req .route ()).andReturn (route );
10577
106- Request req = unit .get (Request .class );
78+ Response rsp = unit .get (Response .class );
10779
108- expect (req .require (Config .class )).andReturn (conf );
109- expect (req .require (Env .class )).andReturn (env );
80+ rsp .send (unit .capture (Result .class ));
81+ };
82+ }
11083
111- expect (req .path ()).andReturn ("/path" );
112- expect (req .method ()).andReturn ("GET" );
113- expect (req .route ()).andReturn (route );
84+ @ SuppressWarnings ({"unchecked" })
85+ @ Test
86+ public void handleWithErrMessage () throws Exception {
87+ Err ex = new Err (500 , "Something something dark" );
11488
115- Response rsp = unit .get (Response .class );
89+ StringWriter writer = new StringWriter ();
90+ ex .printStackTrace (new PrintWriter (writer ));
91+ String [] stacktrace = writer .toString ().replace ("\r " , "" ).split ("\\ n" );
11692
117- rsp .send (unit .capture (Result .class ));
118- })
119- .run (
120- unit -> {
93+ new MockUnit (Request .class , Response .class , Route .class , Env .class , Config .class )
94+ .expect (handleErr (ex , true ))
95+ .run (unit -> {
12196
12297 Request req = unit .get (Request .class );
12398 Response rsp = unit .get (Response .class );
0 commit comments