1111import org .springframework .boot .test .autoconfigure .web .servlet .AutoConfigureMockMvc ;
1212import org .springframework .boot .test .context .SpringBootTest ;
1313import org .springframework .boot .test .context .SpringBootTest .WebEnvironment ;
14- import org .springframework .security .web .FilterChainProxy ;
1514import org .springframework .test .context .junit4 .SpringJUnit4ClassRunner ;
1615import org .springframework .test .web .servlet .MockMvc ;
1716import org .springframework .test .web .servlet .setup .MockMvcBuilders ;
1817import org .springframework .web .context .WebApplicationContext ;
1918
20- import static org .springframework .security .test .web .servlet .request .SecurityMockMvcRequestPostProcessors .*;
21-
2219@ RunWith (SpringJUnit4ClassRunner .class )
2320@ SpringBootTest (webEnvironment = WebEnvironment .RANDOM_PORT , classes = ToggleApplication .class )
2421@ AutoConfigureMockMvc
@@ -31,54 +28,39 @@ public class ToggleIntegrationTest {
3128 EmployeeRepository employeeRepository ;
3229
3330 @ Autowired
34- private MockMvc mvc ;
31+ private MockMvc mockMvc ;
3532
3633 @ Autowired
3734 private WebApplicationContext wac ;
3835
39- @ Autowired
40- private FilterChainProxy springSecurityFilterChain ;
41-
4236 @ Before
4337 public void setup () {
44- this .mvc = MockMvcBuilders .webAppContextSetup (this .wac )
45- .addFilter (springSecurityFilterChain )
46- .build ();
38+ this .mockMvc = MockMvcBuilders .webAppContextSetup (this .wac ).build ();
4739 }
4840
4941 @ Test
50- public void givenNoAuthentication_whenIncreaseSalary_thenNoIncrease () throws Exception {
42+ public void givenFeaturePropertyFalse_whenIncreaseSalary_thenNoIncrease () throws Exception {
5143 Employee emp = new Employee (1 , 2000 );
5244 employeeRepository .save (emp );
53- mvc .perform (post ("/increaseSalary" ).param ("id" , emp .getId () + "" ))
54- .andExpect (status ().is (200 ));
45+
46+ System .setProperty ("employee.feature" , "false" );
47+
48+ mockMvc .perform (post ("/increaseSalary" ).param ("id" , emp .getId () + "" )).andExpect (status ().is (200 ));
5549
5650 emp = employeeRepository .findOne (1L );
5751 assertEquals ("salary incorrect" , 2000 , emp .getSalary (), 0.5 );
5852 }
5953
6054 @ Test
61- public void givenAdminAuthentication_whenIncreaseSalary_thenIncrease () throws Exception {
55+ public void givenFeaturePropertyTrue_whenIncreaseSalary_thenIncrease () throws Exception {
6256 Employee emp = new Employee (1 , 2000 );
6357 employeeRepository .save (emp );
64- mvc .perform (post ("/increaseSalary" ).param ("id" , emp .getId () + "" )
65- .with (httpBasic ("admin" , "pass" )))
66- .andExpect (status ().is (200 ));
6758
68- emp = employeeRepository .findOne (1L );
69- assertEquals ("salary incorrect" , 2200 , emp .getSalary (), 0.5 );
70- }
59+ System .setProperty ("employee.feature" , "true" );
7160
72- @ Test
73- public void givenUserAuthentication_whenIncreaseSalary_thenNoIncrease () throws Exception {
74- Employee emp = new Employee (1 , 2000 );
75- employeeRepository .save (emp );
76- mvc .perform (post ("/increaseSalary" ).param ("id" , emp .getId () + "" )
77- .with (httpBasic ("user" , "pass" )))
78- .andExpect (status ().is (200 ));
61+ mockMvc .perform (post ("/increaseSalary" ).param ("id" , emp .getId () + "" )).andExpect (status ().is (200 ));
7962
8063 emp = employeeRepository .findOne (1L );
81- assertEquals ("salary incorrect" , 2000 , emp .getSalary (), 0.5 );
64+ assertEquals ("salary incorrect" , 2200 , emp .getSalary (), 0.5 );
8265 }
83-
8466}
0 commit comments