ControllerClass, entity object inside the controller getting null return from service.save when run with testng
@RestController
@RequestMapping("/api")
public class PostController{
@PostMapping("/post")
public ResponseEntity<PostResponse> savePostInfo(@RequestBody PostRequest req) throws Exception {
PostEntity entity = postService.savePost(req.getData());
//above line returning null when testng test perform
PostResponse response = new PostResponse();
if (entity != null) {
response.setId(req.getData().getPostId());
response.setData(req.getData());
response.setStatus(Status.SUCCESS);
response.setMessage("Post Information Saved");
response.setIsAuthenticated(Boolean.TRUE);
return new ResponseEntity<>(response, HttpStatus.OK);
} else {
response.setStatus(Status.FAILURE);
response.setMessage("Data not Saved");
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}
}
}
TestClass
@RunWith(SpringRunner.class)
@ContextConfiguration(classes=PostController.class)
@WebAppConfiguration
@WebMvcTest
public class PostControllerTest extends AbstractTestNGSpringContextTests{
private static final Logger logger = LoggerFactory.getLogger(PostControllerTest.class);
@Autowired
private WebApplicationContext wac;
@Autowired
private PostController postController;
@Autowired
ObjectMapper mapper;
@MockBean
private PostService postServiceUT = mock(PostService.class);
private MockMvc mockMvc;
before method
@BeforeMethod
public void init() {
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
test method, mockMvc.perform getting the status.BAD_REQUEST due to PostEntity entity = postService.savePost(req.getData()) returning null in testng test, Can someone help me how to write a test for above controller method.
@Test
public void savePostInfoTest() throws Exception {
PostEntity entity =new PostEntity();
entity.setAccountId(11);
entity.setCampaignId(12);
entity.setContent("one");
entity.setDateTime("now");
entity.setExternalAccountId(13);
entity.setExternalLink("link1");
entity.setPostId(14);
entity.setStatusId(15);
entity.setTypeId(16);
PostRequest req = new PostRequest();
req.setData(entity);
PostResponse response = new PostResponse();
response.setId(entity.getPostId());
response.setData(entity);
response.setStatus(Status.SUCCESS);
response.setMessage("Post Information Saved");
response.setIsAuthenticated(Boolean.TRUE);
logger.info("-------inside logger info before Mockito.when");
Mockito.when(postServiceUT.savePost(Mockito.any(PostEntity.class))).thenReturn(entity);
logger.info("-------inside logger info before mvc.perform");
logger.info("-------------data ="+req.getData());
mockMvc.perform(post("/api/post").contentType(MediaType.APPLICATION_JSON_VALUE)
.accept(MediaType.APPLICATION_JSON).characterEncoding("UTF-8")
.content(mapper.writeValueAsString(req)))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.message", is("Post Information Saved")))
.andExpect(jsonPath("$.status", is("SUCCESS")))
.andExpect(jsonPath("$.isAuthenticated", is(true)));
}}
Aucun commentaire:
Enregistrer un commentaire