1313import static org .mockito .BDDMockito .given ;
1414import java .util .List ;
1515import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
16+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .post ;
1617import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .jsonPath ;
1718import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
1819import com .gourav .restapi .models .Pets ;
@@ -23,7 +24,7 @@ public class PetsControllerTest {
2324
2425 @ Autowired
2526 private MockMvc mvc ;
26-
27+
2728 @ MockBean
2829 private PetsController petsController ;
2930
@@ -33,43 +34,36 @@ public void getAllPets() throws Exception {
3334 Pets pets = new Pets (id , "Liam" , "cat" , "tabby" );
3435
3536 List <Pets > allPets = singletonList (pets );
36-
37+
3738 given (petsController .getAllPets ()).willReturn (allPets );
3839
3940 mvc .perform (get ("/pets/" ).contentType ("application/json;charset=UTF-8" )).andExpect (status ().isOk ())
40- .andExpect (jsonPath ("$[0].name" ).value ("Liam" ))
41- .andExpect (jsonPath ("$[0].breed" ).value ("tabby" ))
42- .andExpect (jsonPath ("$[0].species" ).value ("cat" ))
43- .andReturn ();
41+ .andExpect (jsonPath ("$[0].name" ).value ("Liam" )).andExpect (jsonPath ("$[0].breed" ).value ("tabby" ))
42+ .andExpect (jsonPath ("$[0].species" ).value ("cat" )).andReturn ();
4443 }
45-
44+
4645 @ Test
4746 public void getPetById () throws Exception {
4847 ObjectId id = ObjectId .get ();
4948 Pets pets = new Pets (id , "Liam" , "cat" , "tabby" );
5049
5150 given (petsController .getPetById (id )).willReturn (pets );
5251
53- mvc .perform (get ("/pets/" +id +"/" ).contentType ("application/json;charset=UTF-8" )).andExpect (status ().isOk ())
54- .andExpect (jsonPath ("$.name" ).value ("Liam" ))
55- .andExpect (jsonPath ("$.breed" ).value ("tabby" ))
56- .andExpect (jsonPath ("$.species" ).value ("cat" ))
57- .andReturn ();
52+ mvc .perform (get ("/pets/" + id + "/" ).contentType ("application/json;charset=UTF-8" )).andExpect (status ().isOk ())
53+ .andExpect (jsonPath ("$.name" ).value ("Liam" )).andExpect (jsonPath ("$.breed" ).value ("tabby" ))
54+ .andExpect (jsonPath ("$.species" ).value ("cat" )).andReturn ();
55+ }
56+
57+ @ Test
58+ public void createPet () throws Exception {
59+ Pets pets = new Pets ();
60+
61+ pets .setName ("Liam" );
62+ pets .setBreed ("tabby" );
63+ pets .setSpecies ("cat" );
64+
65+ given (petsController .createPet (pets )).willReturn (pets );
66+
67+ mvc .perform (post ("/pets/" ).contentType ("application/json;charset=UTF-8" )).andExpect (status ().isOk ());
5868 }
59-
60- // @Test
61- // public void modifyPetById() throws Exception {
62- // ObjectId id = ObjectId.get();
63- // Pets pets = new Pets();
64- //
65- // pets.setId(id);
66- //
67- // //given(petsController.modifyPetById(id, pets)).willReturn(pets);
68- //
69- // mvc.perform(get("/pets/"+id+"/").contentType("application/json;charset=UTF-8")).andExpect(status().isOk())
70- // .andExpect(jsonPath("$.name").value("Liam"))
71- // .andExpect(jsonPath("$.breed").value("tabby"))
72- // .andExpect(jsonPath("$.species").value("cat"))
73- // .andReturn();
74- // }
7569}
0 commit comments