17
17
import java .lang .reflect .InvocationTargetException ;
18
18
import java .lang .reflect .Method ;
19
19
import java .time .LocalDate ;
20
+ import java .util .Optional ;
20
21
import java .util .Set ;
21
- import static org .assertj .core .api .AssertionsForClassTypes .assertThat ;
22
-
23
22
23
+ import static org .assertj .core .api .AssertionsForClassTypes .assertThat ;
24
24
import static org .mockito .Mockito .verify ;
25
25
import static org .mockito .Mockito .when ;
26
26
@@ -66,27 +66,35 @@ void dateServletExtendsHttpServlet() {
66
66
Set <Class <? extends HttpServlet >> httpServlets =
67
67
reflections .getSubTypesOf (HttpServlet .class );
68
68
69
- httpServlets .stream ()
69
+ Optional < String > anyDateHttpServlet = httpServlets .stream ()
70
70
.map (Class ::getSimpleName )
71
71
.filter (servlet -> servlet .equals (DATE_SERVLET ))
72
- .findAny ()
73
- . orElseThrow ();
72
+ .findAny ();
73
+ assertThat ( anyDateHttpServlet ). isNotEmpty ();
74
74
}
75
75
76
76
@ Test
77
77
@ Order (3 )
78
78
void dateServletIsMarkedAsWebServlet () {
79
79
Set <Class <?>> servlets =
80
80
reflections .getTypesAnnotatedWith (WebServlet .class );
81
- servlets .stream ()
81
+ Optional < String > anyMarkedDateServlet = servlets .stream ()
82
82
.map (Class ::getSimpleName )
83
83
.filter (servlet -> servlet .equals (DATE_SERVLET ))
84
- .findAny ()
85
- . orElseThrow ();
84
+ .findAny ();
85
+ assertThat ( anyMarkedDateServlet ). isNotEmpty ();
86
86
}
87
87
88
88
@ Test
89
89
@ Order (4 )
90
+ void dateServletIsMarkedWithProperPath () throws ClassNotFoundException {
91
+ String [] value = Class .forName (SERVLET_PACKAGE + "." + DATE_SERVLET )
92
+ .getAnnotation (WebServlet .class ).value ();
93
+ assertThat (value ).contains ("/date" );
94
+ }
95
+
96
+ @ Test
97
+ @ Order (5 )
90
98
void dateServletContentTypeIsProper () throws IllegalAccessException ,
91
99
InvocationTargetException , NoSuchMethodException , IOException {
92
100
Method doGetMethod = getDoGetMethod ();
@@ -100,16 +108,17 @@ void dateServletContentTypeIsProper() throws IllegalAccessException,
100
108
}
101
109
102
110
@ Test
103
- @ Order (5 )
111
+ @ Order (6 )
104
112
void dateServletReturnsDateInResponse () throws IOException , NoSuchMethodException , InvocationTargetException ,
105
113
IllegalAccessException {
114
+ Method doGetMethod = getDoGetMethod ();
115
+
106
116
StringWriter stringWriter = new StringWriter ();
107
117
PrintWriter printWriter = new PrintWriter (stringWriter );
108
118
when (response .getWriter ()).thenReturn (printWriter );
109
- Method doGetMethod = getDoGetMethod ();
110
119
111
120
doGetMethod .invoke (dateServletObject , request , response );
112
- assertThat (stringWriter .getBuffer ().toString ().contains (LocalDate .now ().toString ())). isTrue ( );
121
+ assertThat (stringWriter .getBuffer ().toString ()) .contains (LocalDate .now ().toString ());
113
122
}
114
123
115
124
private Method getDoGetMethod () throws NoSuchMethodException {
0 commit comments