Testing 测试如何将RestController放入Spring引导
如何使用Spring Boot测试一个PUT请求?? 我有这个方法:Testing 测试如何将RestController放入Spring引导,testing,spring-boot,put,Testing,Spring Boot,Put,如何使用Spring Boot测试一个PUT请求?? 我有这个方法: @RequestMapping(method = RequestMethod.PUT, value = "/") public NaturezaTitulo save(@RequestBody NaturezaTitulo naturezaTitulo){ return naturezaTituloService.save(naturezaTitulo); } 这个测试班: @RunWith
@RequestMapping(method = RequestMethod.PUT, value = "/")
public NaturezaTitulo save(@RequestBody NaturezaTitulo naturezaTitulo){
return naturezaTituloService.save(naturezaTitulo);
}
这个测试班:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
public class NaturezaTituloControllerTest {
private MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(),
MediaType.APPLICATION_JSON.getSubtype(),
Charset.forName("utf8"));
private MockMvc mockMvc;
private HttpMessageConverter mappingJackson2HttpMessageConverter;
private List<NaturezaTitulo> naturezaTituloList = new ArrayList<>();
@Autowired
private WebApplicationContext webApplicationContext;
@Autowired
void setConverters(HttpMessageConverter<?>[] converters) {
this.mappingJackson2HttpMessageConverter = Arrays.asList(converters).stream().filter(
hmc -> hmc instanceof MappingJackson2HttpMessageConverter).findAny().get();
Assert.assertNotNull("the JSON message converter must not be null",
this.mappingJackson2HttpMessageConverter);
}
@Before
public void setup() throws Exception {
this.mockMvc = webAppContextSetup(webApplicationContext).build();
}
@Test
public void naturezaTituloNotFound() throws Exception {
mockMvc.perform(get("/naturezatitulo/55ce2dd6222e629f4b8d6fe0"))
.andExpect(status().is4xxClientError());
}
@Test
public void naturezaTituloSave() throws Exception {
NaturezaTitulo naturezaTitulo = new NaturezaTitulo();
naturezaTitulo.setNatureza("Testando");
mockMvc.perform(put("/naturezatitulo/").content(this.json(naturezaTitulo))
.contentType(contentType))
.andExpect(jsonPath("$.id", notNullValue()));
}
protected String json(Object o) throws IOException {
MockHttpOutputMessage mockHttpOutputMessage = new MockHttpOutputMessage();
this.mappingJackson2HttpMessageConverter.write(
o, MediaType.APPLICATION_JSON, mockHttpOutputMessage);
return mockHttpOutputMessage.getBodyAsString();
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(类=Application.class)
@WebAppConfiguration
公共类性质属性控制器测试{
私有MediaType contentType=新的MediaType(MediaType.APPLICATION_JSON.getType(),
MediaType.APPLICATION_JSON.getSubtype(),
字符集forName(“utf8”);
私有MockMvc-MockMvc;
私有HttpMessageConverter映射Jackson2HttpMessageConverter;
private List naturezaTituloList=new ArrayList();
@自动连线
私有WebApplicationContext WebApplicationContext;
@自动连线
void setConverters(HttpMessageConverter[]转换器){
this.mappingJackson2HttpMessageConverter=Arrays.asList(converters).stream().filter(
hmc->hmc instanceof MappingJackson2HttpMessageConverter.findAny().get();
Assert.assertNotNull(“JSON消息转换器不能为null”,
此.mappingJackson2HttpMessageConverter);
}
@以前
public void setup()引发异常{
this.mockMvc=webAppContextSetup(webApplicationContext.build();
}
@试验
public void naturezaTituloNotFound()引发异常{
mockMvc.perform(get(“/naturezatitulo/55ce2d6222e2e629f4b8d6fe0”))
.andExpect(status().is4xxClientError());
}
@试验
public void naturezaTituloSave()引发异常{
NaturezaTitulo NaturezaTitulo=新的NaturezaTitulo();
naturezaTitulo.setNatureza(“Testando”);
mockMvc.perform(put(“/naturezatitulo/”).content(this.json(naturezatitulo))
.contentType(contentType))
.andExpect(jsonPath(“$.id”,notNullValue());
}
受保护字符串json(对象o)引发IOException{
MockHttpOutputMessage MockHttpOutputMessage=新的MockHttpOutputMessage();
this.mappingJackson2HttpMessageConverter.write(
o、 MediaType.APPLICATION_JSON,mockHttpOutputMessage);
返回mockHttpOutputMessage.getBodyAsString();
}
}
但我有一个错误:
java.lang.IllegalArgumentException:json不能为null或空
com.jayway.jsonpath.internal.Utils.notEmpty(Utils.java:259)
我怎样才能通过一个物体的测试
tks