Spring restful service test case fail HTTP status

2019-08-10 22:47发布

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条回答
Evening l夕情丶
2楼-- · 2019-08-10 23:01

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.

查看更多
登录 后发表回答