1+ package com .baeldung .flips .controller ;
2+
3+ import org .hamcrest .Matchers ;
4+ import org .junit .Test ;
5+ import org .junit .runner .RunWith ;
6+ import org .springframework .beans .factory .annotation .Autowired ;
7+ import org .springframework .boot .test .autoconfigure .web .servlet .AutoConfigureMockMvc ;
8+ import org .springframework .boot .test .context .SpringBootTest ;
9+ import org .springframework .test .context .ActiveProfiles ;
10+ import org .springframework .test .context .junit4 .SpringRunner ;
11+ import org .springframework .test .web .servlet .MockMvc ;
12+ import org .springframework .test .web .servlet .request .MockMvcRequestBuilders ;
13+ import org .springframework .test .web .servlet .result .MockMvcResultMatchers ;
14+
15+ @ RunWith (SpringRunner .class )
16+ @ SpringBootTest (properties = {
17+ "feature.foo.by.id=Y" ,
18+ "feature.new.foo=Y" ,
19+ "last.active.after=2018-03-14T00:00:00Z" ,
20+ "first.active.after=2999-03-15T00:00:00Z" ,
21+ "logging.level.org.flips=info"
22+
23+ })
24+ @ AutoConfigureMockMvc
25+ @ ActiveProfiles ("dev" )
26+ public class FlipControllerTest {
27+
28+ @ Autowired private MockMvc mvc ;
29+
30+ @ Test
31+ public void givenValidDayOfWeek_APIAvailable () throws Exception {
32+ mvc .perform (MockMvcRequestBuilders .get ("/foo/1" ))
33+ .andExpect (MockMvcResultMatchers .status ().is (200 ))
34+ .andExpect (MockMvcResultMatchers .jsonPath ("$.name" , Matchers .equalTo ("Foo1" )))
35+ .andExpect (MockMvcResultMatchers .jsonPath ("$.id" , Matchers .equalTo (1 )));
36+ }
37+
38+ @ Test
39+ public void givenValidDate_APIAvailable () throws Exception {
40+ mvc .perform (MockMvcRequestBuilders .get ("/foo/last" ))
41+ .andExpect (MockMvcResultMatchers .status ().is (200 ))
42+ .andExpect (MockMvcResultMatchers .jsonPath ("$.name" , Matchers .equalTo ("Foo6" )))
43+ .andExpect (MockMvcResultMatchers .jsonPath ("$.id" , Matchers .equalTo (6 )));
44+ }
45+
46+ @ Test
47+ public void givenInvalidDate_APINotAvailable () throws Exception {
48+ mvc .perform (MockMvcRequestBuilders .get ("/foo/first" ))
49+ .andExpect (MockMvcResultMatchers .status ().is (501 ));
50+ }
51+
52+ @ Test
53+ public void givenCorrectProfile_APIAvailable () throws Exception {
54+ mvc .perform (MockMvcRequestBuilders .get ("/foos" ))
55+ .andExpect (MockMvcResultMatchers .status ().isOk ())
56+ .andExpect (MockMvcResultMatchers .jsonPath ("$" , Matchers .hasSize (6 )));
57+ }
58+
59+ @ Test
60+ public void givenPropertySet_APIAvailable () throws Exception {
61+ mvc .perform (MockMvcRequestBuilders .get ("/foos/1" ))
62+ .andExpect (MockMvcResultMatchers .status ().isOk ())
63+ .andExpect (MockMvcResultMatchers .jsonPath ("$.name" , Matchers .equalTo ("Foo1" )))
64+ .andExpect (MockMvcResultMatchers .jsonPath ("$.id" , Matchers .equalTo (1 )));
65+ }
66+
67+ @ Test
68+ public void getValidExpression_FlipBean () throws Exception {
69+ mvc .perform (MockMvcRequestBuilders .get ("/foo/new" ))
70+ .andExpect (MockMvcResultMatchers .status ().is (200 ))
71+ .andExpect (MockMvcResultMatchers .jsonPath ("$.name" , Matchers .equalTo ("Shiny New Foo!" )))
72+ .andExpect (MockMvcResultMatchers .jsonPath ("$.id" , Matchers .equalTo (100 )));
73+ }
74+ }
0 commit comments