Juint单元测试
*********************************************************************************************
单元测试:集成spring+springmvc单元测试,使用SpringJunit4ClassRunner组件测试环境
**********************************************************************************************
控制层测试(controller),具体实现代码
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = "file:src/main/webapp/WEB-INF/config/spring/spring*")
public class Test {
@Autowired
private BdRouteService bdRouteService;
@Autowired
private WebApplicationContext context;
private MockMvc mvc;
@Before
public void setUp() {
mvc = MockMvcBuilders.webAppContextSetup(context).build();
}
@org.junit.Test
public void controllerTest() throws Exception {
String result = mvc.perform(MockMvcRequestBuilders.get("/bd/bdRoute/ff").accept(MediaType.APPLICATION_JSON))
.andDo(MockMvcResultHandlers.print())
.andReturn().getResponse().getContentAsString();
System.out.println("----------->" + result);
}
有志之士,共同学习