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);
}

 

posted @ 2018-01-28 13:20  antlord  阅读(128)  评论(0编辑  收藏  举报