1
1
package com .bobocode ;
2
2
3
3
import org .junit .jupiter .api .*;
4
+ import org .junit .jupiter .api .extension .ExtendWith ;
5
+ import org .mockito .Mock ;
6
+ import org .mockito .junit .jupiter .MockitoExtension ;
4
7
import org .reflections .Reflections ;
5
8
9
+ import javax .servlet .ServletOutputStream ;
6
10
import javax .servlet .annotation .WebServlet ;
7
11
import javax .servlet .http .HttpServlet ;
12
+ import javax .servlet .http .HttpServletRequest ;
13
+ import javax .servlet .http .HttpServletResponse ;
14
+ import java .io .IOException ;
15
+ import java .io .PrintWriter ;
16
+ import java .io .StringWriter ;
17
+ import java .lang .reflect .InvocationTargetException ;
18
+ import java .lang .reflect .Method ;
19
+ import java .time .LocalDate ;
8
20
import java .util .Set ;
21
+ import static org .assertj .core .api .AssertionsForClassTypes .assertThat ;
22
+
23
+
24
+ import static org .mockito .Mockito .verify ;
25
+ import static org .mockito .Mockito .when ;
26
+
9
27
10
28
@ TestMethodOrder (MethodOrderer .OrderAnnotation .class )
29
+ @ ExtendWith (MockitoExtension .class )
11
30
public class DateServletTest {
31
+
32
+ @ Mock
33
+ private HttpServletRequest request ;
34
+
35
+ @ Mock
36
+ private HttpServletResponse response ;
37
+
38
+ @ Mock
39
+ private ServletOutputStream outputStream ;
40
+
41
+ private Object dateServletObject ;
42
+ private Class <?> dateServletClass ;
43
+
12
44
public static final String SERVLET_PACKAGE = "com.bobocode.hello_servlet" ;
13
45
public static final String DATE_SERVLET = "DateServlet" ;
14
46
Reflections reflections ;
15
47
16
48
@ BeforeEach
17
- public void init () {
49
+ public void init () throws ClassNotFoundException , IllegalAccessException , InvocationTargetException ,
50
+ InstantiationException {
18
51
reflections = new Reflections (SERVLET_PACKAGE );
52
+ dateServletClass = Class .forName (SERVLET_PACKAGE + "." + DATE_SERVLET );
53
+ dateServletObject = dateServletClass .getConstructors ()[0 ].newInstance ();
19
54
}
20
55
21
56
@ Test
@@ -49,4 +84,37 @@ void dateServletIsAnnotated() {
49
84
.findAny ()
50
85
.orElseThrow ();
51
86
}
87
+
88
+ @ Test
89
+ @ Order (4 )
90
+ void dateServletContentTypeIsProper () throws IllegalAccessException ,
91
+ InvocationTargetException , NoSuchMethodException , IOException {
92
+ Method doGetMethod = getDoGetMethod ();
93
+
94
+ StringWriter stringWriter = new StringWriter ();
95
+ PrintWriter printWriter = new PrintWriter (stringWriter );
96
+ when (response .getWriter ()).thenReturn (printWriter );
97
+
98
+ doGetMethod .invoke (dateServletObject , request , response );
99
+ verify (response ).setContentType ("text/html" );
100
+ }
101
+
102
+ @ Test
103
+ @ Order (5 )
104
+ void dateServletReturnsDateInResponse () throws IOException , NoSuchMethodException , InvocationTargetException ,
105
+ IllegalAccessException {
106
+ StringWriter stringWriter = new StringWriter ();
107
+ PrintWriter printWriter = new PrintWriter (stringWriter );
108
+ when (response .getWriter ()).thenReturn (printWriter );
109
+ Method doGetMethod = getDoGetMethod ();
110
+
111
+ doGetMethod .invoke (dateServletObject , request , response );
112
+ assertThat (stringWriter .getBuffer ().toString ().contains (LocalDate .now ().toString ())).isTrue ();
113
+ }
114
+
115
+ private Method getDoGetMethod () throws NoSuchMethodException {
116
+ Method doGetMethod = dateServletClass
117
+ .getMethod ("doGet" , HttpServletRequest .class , HttpServletResponse .class );
118
+ return doGetMethod ;
119
+ }
52
120
}
0 commit comments