Springboot TestController
挺简单的,话不多说,直接上代码。
@RunWith(SpringRunner.class)
@SpringBootTest
public class FinOrderControllerTest {
private MockMvc mvc;
private ObjectMapper mapper = new ObjectMapper();
@Autowired
private WebApplicationContext context;
@Before
public void setup() {
mvc = MockMvcBuilders
.webAppContextSetup(context)
//.apply(springSecurity())
.build();
}
@Test
public void selectAll() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/order/all")
.param("id", String.valueOf(1))//传入参数在这些
.contentType(MediaType.APPLICATION_JSON_UTF8)
// .content(mapper.writeValueAsString(id))//将对象转化成JSON字符换
)
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andDo(MockMvcResultHandlers.print())打印出请求和相应的内容
.andExpect(status().is2xxSuccessful());
}
}