1
1
package controller ;
2
2
3
+ import static org .junit .Assert .assertEquals ;
3
4
import static org .junit .Assert .assertNull ;
5
+ import static org .junit .Assert .assertTrue ;
4
6
import com .diffblue .cover .annotations .MaintainedByDiffblue ;
5
7
import com .diffblue .cover .annotations .MethodsUnderTest ;
6
8
import java .io .IOException ;
9
+ import java .util .Collection ;
10
+ import java .util .Set ;
7
11
import javax .servlet .ServletException ;
8
12
import javax .servlet .http .HttpServletRequest ;
9
- import javax .servlet .http .HttpServletRequestWrapper ;
10
13
import javax .servlet .http .HttpServletResponse ;
11
- import javax .servlet .http .HttpServletResponseWrapper ;
12
- import org .junit .Ignore ;
13
14
import org .junit .Test ;
14
15
import org .junit .experimental .categories .Category ;
16
+ import org .springframework .mock .web .MockHttpServletRequest ;
17
+ import org .springframework .mock .web .MockHttpServletResponse ;
18
+ import org .springframework .mock .web .MockHttpSession ;
15
19
16
20
public class HomeServletDiffblueTest {
17
21
/**
@@ -30,60 +34,124 @@ public void testNewHomeServlet() {
30
34
/**
31
35
* Test {@link HomeServlet#doGet(HttpServletRequest, HttpServletResponse)}.
32
36
* <ul>
33
- * <li>When {@link HttpServletRequestWrapper#HttpServletRequestWrapper(HttpServletRequest)} with request is {@code null }.</li>
37
+ * <li>Given {@link MockHttpSession#MockHttpSession() }.</li>
34
38
* </ul>
35
39
* <p>
36
40
* Method under test: {@link HomeServlet#doGet(HttpServletRequest, HttpServletResponse)}
37
41
*/
38
42
@ Test
39
- @ Ignore ("TODO: Complete this test" )
40
43
@ Category (MaintainedByDiffblue .class )
41
44
@ MethodsUnderTest ({"void HomeServlet.doGet(HttpServletRequest, HttpServletResponse)" })
42
- public void testDoGet_whenHttpServletRequestWrapperWithRequestIsNull () throws IOException , ServletException {
43
- // TODO: Diffblue Cover was only able to create a partial test for this method:
44
- // Reason: No inputs found that don't throw a trivial exception.
45
- // Diffblue Cover tried to run the arrange/act section, but the method under
46
- // test threw
47
- // java.lang.IllegalArgumentException: Request cannot be null
48
- // at javax.servlet.ServletRequestWrapper.<init>(ServletRequestWrapper.java:50)
49
- // at javax.servlet.http.HttpServletRequestWrapper.<init>(HttpServletRequestWrapper.java:47)
50
- // See https://diff.blue/R013 to resolve this issue.
45
+ public void testDoGet_givenMockHttpSession () throws IOException , ServletException {
46
+ // Arrange
47
+ HomeServlet homeServlet = new HomeServlet ();
48
+
49
+ MockHttpServletRequest request = new MockHttpServletRequest ();
50
+ request .setSession (new MockHttpSession ());
51
+ MockHttpServletResponse response = new MockHttpServletResponse ();
52
+
53
+ // Act
54
+ homeServlet .doGet (request , response );
51
55
56
+ // Assert
57
+ Collection <String > headerNames = response .getHeaderNames ();
58
+ assertEquals (1 , headerNames .size ());
59
+ assertTrue (headerNames instanceof Set );
60
+ assertEquals ("login" , response .getRedirectedUrl ());
61
+ assertEquals (302 , response .getStatus ());
62
+ assertTrue (headerNames .contains ("Location" ));
63
+ assertTrue (response .isCommitted ());
64
+ }
65
+
66
+ /**
67
+ * Test {@link HomeServlet#doGet(HttpServletRequest, HttpServletResponse)}.
68
+ * <ul>
69
+ * <li>Then {@link MockHttpServletResponse} (default constructor) HeaderNames size is one.</li>
70
+ * </ul>
71
+ * <p>
72
+ * Method under test: {@link HomeServlet#doGet(HttpServletRequest, HttpServletResponse)}
73
+ */
74
+ @ Test
75
+ @ Category (MaintainedByDiffblue .class )
76
+ @ MethodsUnderTest ({"void HomeServlet.doGet(HttpServletRequest, HttpServletResponse)" })
77
+ public void testDoGet_thenMockHttpServletResponseHeaderNamesSizeIsOne () throws IOException , ServletException {
52
78
// Arrange
53
79
HomeServlet homeServlet = new HomeServlet ();
54
- HttpServletRequestWrapper request = new HttpServletRequestWrapper (null );
80
+ MockHttpServletRequest request = new MockHttpServletRequest ();
81
+ MockHttpServletResponse response = new MockHttpServletResponse ();
55
82
56
83
// Act
57
- homeServlet .doGet (request , new HttpServletResponseWrapper (null ));
84
+ homeServlet .doGet (request , response );
85
+
86
+ // Assert
87
+ Collection <String > headerNames = response .getHeaderNames ();
88
+ assertEquals (1 , headerNames .size ());
89
+ assertTrue (headerNames instanceof Set );
90
+ assertEquals ("login" , response .getRedirectedUrl ());
91
+ assertEquals (302 , response .getStatus ());
92
+ assertTrue (headerNames .contains ("Location" ));
93
+ assertTrue (response .isCommitted ());
58
94
}
59
95
60
96
/**
61
97
* Test {@link HomeServlet#doPost(HttpServletRequest, HttpServletResponse)}.
62
98
* <ul>
63
- * <li>When {@link HttpServletRequestWrapper#HttpServletRequestWrapper(HttpServletRequest)} with request is {@code null }.</li>
99
+ * <li>Given {@link MockHttpSession#MockHttpSession() }.</li>
64
100
* </ul>
65
101
* <p>
66
102
* Method under test: {@link HomeServlet#doPost(HttpServletRequest, HttpServletResponse)}
67
103
*/
68
104
@ Test
69
- @ Ignore ("TODO: Complete this test" )
70
105
@ Category (MaintainedByDiffblue .class )
71
106
@ MethodsUnderTest ({"void HomeServlet.doPost(HttpServletRequest, HttpServletResponse)" })
72
- public void testDoPost_whenHttpServletRequestWrapperWithRequestIsNull () throws IOException , ServletException {
73
- // TODO: Diffblue Cover was only able to create a partial test for this method:
74
- // Reason: No inputs found that don't throw a trivial exception.
75
- // Diffblue Cover tried to run the arrange/act section, but the method under
76
- // test threw
77
- // java.lang.IllegalArgumentException: Request cannot be null
78
- // at javax.servlet.ServletRequestWrapper.<init>(ServletRequestWrapper.java:50)
79
- // at javax.servlet.http.HttpServletRequestWrapper.<init>(HttpServletRequestWrapper.java:47)
80
- // See https://diff.blue/R013 to resolve this issue.
107
+ public void testDoPost_givenMockHttpSession () throws IOException , ServletException {
108
+ // Arrange
109
+ HomeServlet homeServlet = new HomeServlet ();
110
+
111
+ MockHttpServletRequest request = new MockHttpServletRequest ();
112
+ request .setSession (new MockHttpSession ());
113
+ MockHttpServletResponse response = new MockHttpServletResponse ();
114
+
115
+ // Act
116
+ homeServlet .doPost (request , response );
81
117
118
+ // Assert
119
+ Collection <String > headerNames = response .getHeaderNames ();
120
+ assertEquals (1 , headerNames .size ());
121
+ assertTrue (headerNames instanceof Set );
122
+ assertEquals ("login" , response .getRedirectedUrl ());
123
+ assertEquals (302 , response .getStatus ());
124
+ assertTrue (headerNames .contains ("Location" ));
125
+ assertTrue (response .isCommitted ());
126
+ }
127
+
128
+ /**
129
+ * Test {@link HomeServlet#doPost(HttpServletRequest, HttpServletResponse)}.
130
+ * <ul>
131
+ * <li>Then {@link MockHttpServletResponse} (default constructor) HeaderNames size is one.</li>
132
+ * </ul>
133
+ * <p>
134
+ * Method under test: {@link HomeServlet#doPost(HttpServletRequest, HttpServletResponse)}
135
+ */
136
+ @ Test
137
+ @ Category (MaintainedByDiffblue .class )
138
+ @ MethodsUnderTest ({"void HomeServlet.doPost(HttpServletRequest, HttpServletResponse)" })
139
+ public void testDoPost_thenMockHttpServletResponseHeaderNamesSizeIsOne () throws IOException , ServletException {
82
140
// Arrange
83
141
HomeServlet homeServlet = new HomeServlet ();
84
- HttpServletRequestWrapper request = new HttpServletRequestWrapper (null );
142
+ MockHttpServletRequest request = new MockHttpServletRequest ();
143
+ MockHttpServletResponse response = new MockHttpServletResponse ();
85
144
86
145
// Act
87
- homeServlet .doPost (request , new HttpServletResponseWrapper (null ));
146
+ homeServlet .doPost (request , response );
147
+
148
+ // Assert
149
+ Collection <String > headerNames = response .getHeaderNames ();
150
+ assertEquals (1 , headerNames .size ());
151
+ assertTrue (headerNames instanceof Set );
152
+ assertEquals ("login" , response .getRedirectedUrl ());
153
+ assertEquals (302 , response .getStatus ());
154
+ assertTrue (headerNames .contains ("Location" ));
155
+ assertTrue (response .isCommitted ());
88
156
}
89
157
}
0 commit comments