please i need help to write a few words about why we've created each of the tests below. What do they cover? Why do we need them? ========================================================================= @Test @Order(1) publicvoidsignupIntegrationTest()throwsException{ ObjectMapperobjectMapper=JsonMapper.builder().disable(MapperFeature.USE_ANNOTATIONS).build(); /* Check the Rest End Point Response */ this.mockMvc.perform(MockMvcRequestBuilders.post("/user/signup") .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(user))) .andExpect(status().isOk()).andExpect(jsonPath("$.firstName",is(this.user.getFirstName()))) .andExpect(jsonPath("$.lastName",is(this.user.getLastName()))) .andExpect(jsonPath("$.username",is(this.user.getUsername()))) .andExpect(jsonPath("$.phone",is(this.user.getPhone()))) .andExpect(jsonPath("$.emailId",is(this.user.getEmailId()))); /* Check the DB */ Optionalopt=this.userRepository.findByUsername(this.user.getUsername()); assertTrue(opt.isPresent(),"User Should Exist"); assertEquals(1,opt.get().getUserId()); assertEquals(this.user.getFirstName(),opt.get().getFirstName()); assertEquals(this.user.getLastName(),opt.get().getLastName()); assertEquals(this.user.getUsername(),opt.get().getUsername()); assertEquals(this.user.getPhone(),opt.get().getPhone()); assertEquals(this.user.getEmailId(),opt.get().getEmailId()); assertEquals(false,opt.get().getEmailVerified()); assertTrue(this.passwordEncoder.matches(this.user.getPassword(),opt.get().getPassword())); } @Test @Order(2) publicvoidsignupUsernameExistsIntegrationTest()throwsException{ ObjectMapperobjectMapper=JsonMapper.builder().disable(MapperFeature.USE_ANNOTATIONS).build(); /* Check the Rest End Point Response */ this.mockMvc.perform(MockMvcRequestBuilders.post("/user/signup") .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(this.user))) .andExpect(status().is4xxClientError()) .andExpect(jsonPath("$.httpStatusCode",is(400))) .andExpect(jsonPath("$.httpStatus",is("BAD_REQUEST"))) .andExpect(jsonPath("$.reason",is("BAD REQUEST"))) .andExpect(jsonPath("$.message", is(String.format("Username already exists, %s",this.user.getUsername())))); } @Test @Order(3) publicvoidsignupEmailExistsIntegrationTest()throwsException{ ObjectMapperobjectMapper=JsonMapper.builder().disable(MapperFeature.USE_ANNOTATIONS).build(); this.user.setUsername(this.otherUsername); /* Check the Rest End Point Response */ this.mockMvc.perform(MockMvcRequestBuilders.post("/user/signup") .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(this.user))) .andExpect(status().is4xxClientError()) .andExpect(jsonPath("$.httpStatusCode",is(400))) .andExpect(jsonPath("$.httpStatus",is("BAD_REQUEST"))) .andExpect(jsonPath("$.reason",is("BAD REQUEST"))) .andExpect(jsonPath("$.message",is(String.format("Email already exists, %s",this.user.getEmailId())))); }
please i need help to write a few words about why we've created each of the tests below. What do they cover? Why do we need them?
=========================================================================
@Test
@Order(1)
publicvoidsignupIntegrationTest()throwsException{
ObjectMapperobjectMapper=JsonMapper.builder().disable(MapperFeature.USE_ANNOTATIONS).build();
/* Check the Rest End Point Response */
this.mockMvc.perform(MockMvcRequestBuilders.post("/user/signup")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(user)))
.andExpect(status().isOk()).andExpect(jsonPath("$.firstName",is(this.user.getFirstName())))
.andExpect(jsonPath("$.lastName",is(this.user.getLastName())))
.andExpect(jsonPath("$.username",is(this.user.getUsername())))
.andExpect(jsonPath("$.phone",is(this.user.getPhone())))
.andExpect(jsonPath("$.emailId",is(this.user.getEmailId())));
/* Check the DB */
Optional<User>opt=this.userRepository.findByUsername(this.user.getUsername());
assertTrue(opt.isPresent(),"User Should Exist");
assertEquals(1,opt.get().getUserId());
assertEquals(this.user.getFirstName(),opt.get().getFirstName());
assertEquals(this.user.getLastName(),opt.get().getLastName());
assertEquals(this.user.getUsername(),opt.get().getUsername());
assertEquals(this.user.getPhone(),opt.get().getPhone());
assertEquals(this.user.getEmailId(),opt.get().getEmailId());
assertEquals(false,opt.get().getEmailVerified());
assertTrue(this.passwordEncoder.matches(this.user.getPassword(),opt.get().getPassword()));
}
@Test
@Order(2)
publicvoidsignupUsernameExistsIntegrationTest()throwsException{
ObjectMapperobjectMapper=JsonMapper.builder().disable(MapperFeature.USE_ANNOTATIONS).build();
/* Check the Rest End Point Response */
this.mockMvc.perform(MockMvcRequestBuilders.post("/user/signup")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(this.user)))
.andExpect(status().is4xxClientError())
.andExpect(jsonPath("$.httpStatusCode",is(400)))
.andExpect(jsonPath("$.httpStatus",is("BAD_REQUEST")))
.andExpect(jsonPath("$.reason",is("BAD REQUEST")))
.andExpect(jsonPath("$.message",
is(String.format("Username already exists, %s",this.user.getUsername()))));
}
@Test
@Order(3)
publicvoidsignupEmailExistsIntegrationTest()throwsException{
ObjectMapperobjectMapper=JsonMapper.builder().disable(MapperFeature.USE_ANNOTATIONS).build();
this.user.setUsername(this.otherUsername);
/* Check the Rest End Point Response */
this.mockMvc.perform(MockMvcRequestBuilders.post("/user/signup")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(this.user)))
.andExpect(status().is4xxClientError())
.andExpect(jsonPath("$.httpStatusCode",is(400)))
.andExpect(jsonPath("$.httpStatus",is("BAD_REQUEST")))
.andExpect(jsonPath("$.reason",is("BAD REQUEST")))
.andExpect(jsonPath("$.message",is(String.format("Email already exists, %s",this.user.getEmailId()))));
}
Step by step
Solved in 3 steps