Spring restful service test case fail HTTP status is 500

岁酱吖の 提交于 2020-01-05 14:28:31

问题


I want to implement test case for spring restful web services which return a json MY controller test class is :

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {WebAppContext.class,JpaTestConfiguration.class
})
@WebAppConfiguration

public class DominProfileRestControllerTest {

private MockMvc mockMvc;

@Autowired
private WebApplicationContext webApplicationContext;


private MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(),
        MediaType.APPLICATION_JSON.getSubtype(),
        Charset.forName("utf8"));
@Before
public void setUp() {
    mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}


@Test
public void testGetDomainProfile() throws Exception {

    String profileId = domainProfile.getId().toString();
    System.out.print("testing restful"+profileId);

    mockMvc.perform(get("/service/domainprofile/get/{id}", profileId) )
            .andExpect(status().isOk())
            .andExpect(content().contentType(contentType))
            .andExpect(jsonPath("$.city", is("Chandigrah")));

 /*              mockMvc.perform(get("/service/domainprofile/get/{id}",profileId).accept(MediaType.TEXT_PLAIN))
            .andExpect(status().isOk())
            .andExpect(content().contentType("text/plain;charset=ISO-8859-1"))
            .andExpect(content().string("hello Prashant"));
*/
}

My Controller class Is :

@RestController
 @RequestMapping("/service/domainprofile")
 public class DominProfileRestController {

@Autowired
private JpaDomainProfileRepository jpaDomainProfileRepository;

@RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
public DomainProfileResource getDomainProfile(@PathVariable String id) {
    JpaDomainProfile domainProfile = jpaDomainProfileRepository.findOne(Long.valueOf(id));
   DomainProfileResource domainProfileResource = new  DomainProfileResource();
    System.out.println("domainProfile.getCity()*************" + domainProfile.getCity());
    System.out.println("domainProfile.getAddress()*************" + domainProfile.getAddress());
    domainProfileResource.setCity(domainProfile.getCity());
    domainProfileResource.setAddress(domainProfile.getAddress());

  //  return new ResponseEntity<DomainProfileResource>(domainProfileResource, HttpStatus.OK);
   return domainProfileResource;
   // return domainProfile;

}
}

When I run test case I got An error while we got values in domainprofile.city and domainprofile.address. Error Is : java.lang.AssertionError: Status Expected :200 Actual :500 It Is Working Fine When I return a plain text


回答1:


can you do this

mockMvc.perform(get("/service/domainprofile/get/{id}", profileId) ) .andDo(print());

this will print the full response with exception , now if you see HttpMessageNotWritableException which was the issue I was facing , you should try to serialize your object using jackson and see if it works (spring internally uses Jackson ). For example , If any of your fields are null the Serialization will fail.



来源:https://stackoverflow.com/questions/27617432/spring-restful-service-test-case-fail-http-status-is-500

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!