下面是相关代码:
controller层
@RestController
@RequestMapping("/studentTest/")
public class StudentControllerTest {
@Autowired
private StudentDao studentDao;
@RequestMapping(value = "insertStudent.html",method = RequestMethod.POST,produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
public String insertStudent(@RequestParam("name") String name, @RequestParam("age") String age, @RequestParam("sex") String sex){
Student stu = new Student();
System.out.println(UUID.randomUUID().toString());
stu.setId(UUID.randomUUID().toString());
stu.setName(name);
stu.setAge(age);
stu.setSex(sex);
studentDao.insertStudent(stu);
return "success";
}
}
测试类:
@RunWith(SpringRunner.class)
@SpringBootTest
@ComponentScan(basePackages = "com.tjf.learn")
public class Demo1ApplicationTests {
private MockMvc mockMvc;
@Before
public void initMockMvc (){
this.mockMvc = MockMvcBuilders.standaloneSetup(new IndexController()).build();
}
@Test
public void insertStudent () throws Exception{
RequestBuilder rb = MockMvcRequestBuilders.post("/studentTest/insertStudent.html")
.param("name","张三")
.param("age","20")
.param("sex","1")
.contentType(MediaType.APPLICATION_JSON_UTF8).accept(MediaType.APPLICATION_JSON_UTF8);
mockMvc.perform(rb)
// .andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
.andExpect(MockMvcResultMatchers.status().isOk())
.andReturn().getResponse().getContentAsString();
}
}
求哪位大神解决一下