Spring restful服务测试用例失败HTTP状态为500
我想为返回json的spring restful web服务实现测试用例 我的控制器测试类是:Spring restful服务测试用例失败HTTP状态为500,rest,spring-mvc,Rest,Spring Mvc,我想为返回json的spring restful web服务实现测试用例 我的控制器测试类是: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {WebAppContext.class,JpaTestConfiguration.class }) @WebAppConfiguration public class DominProfileRestControllerTest { private Mo
@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"));
*/
}
我的控制器类是:
@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;
}
}
@RestController
@请求映射(“/service/domainprofile”)
公共类主配置文件测试控制器{
@自动连线
私有JpaDomainProfileRepository JpaDomainProfileRepository;
@RequestMapping(value=“/get/{id}”,method=RequestMethod.get)
公共域配置文件资源getDomainProfile(@PathVariable字符串id){
JpaDomainProfile domainProfile=jpaDomainProfileRepository.findOne(Long.valueOf(id));
DomainProfileResource DomainProfileResource=新的DomainProfileResource();
System.out.println(“domainProfile.getCity()**********”+domainProfile.getCity());
System.out.println(“domainProfile.getAddress()***********”+domainProfile.getAddress());
domainProfileResource.setCity(domainProfile.getCity());
domainProfileResource.setAddress(domainProfile.getAddress());
//返回新的响应属性(domainProfileResource,HttpStatus.OK);
返回域配置文件资源;
//返回域配置文件;
}
}
当我运行测试用例时,当我们在domainprofile.city和domainprofile.address中获取值时,我得到了一个错误。
错误是:
java.lang.AssertionError:状态
预计:200
实际:500
当我返回纯文本时,它工作正常您能这样做吗
mockMvc.perform(get(“/service/domainprofile/get/{id}”,profileId))
.andDo(print())
这将打印带有异常的完整响应,现在如果您看到HttpMessageNotWritableException,这是我面临的问题,您应该尝试使用jackson序列化您的对象,看看它是否有效(spring内部使用jackson)。例如,如果您的任何字段为空,则序列化将失败 你能做到吗
mockMvc.perform(get(“/service/domainprofile/get/{id}”,profileId))
.andDo(print())
这将打印带有异常的完整响应,现在如果您看到HttpMessageNotWritableException,这是我面临的问题,您应该尝试使用jackson序列化您的对象,看看它是否有效(spring内部使用jackson)。例如,如果您的任何字段为空,则序列化将失败