JUNIT+SPRINGMVC的mock测试

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = { "classpath:spring/spring-xxx.xml" })//加载spring配置文件
public class OrganizationControllerTest {

    @Resource
    private OrganizationController controller;//待测试的spring ctroller

    private MockMvc                mock;//需要导入spring-test-3.2.x.jar

    @Before
    public void setup() {
        mock = MockMvcBuilders.standaloneSetup(controller).build();
    }

    /**
     * Test method for
     * {@link com.jplatformx.webapp.biz.account.controllers.DepartmentController#post(com.jplatformx.component.security.beans.Department)}
     * .
     * @throws Exception
     */
    @Test
    public void testPost() throws Exception {
        final ResultActions ra = this.mock.perform(MockMvcRequestBuilders.get(
                "/action/organization/post").param("departmentId", "1").param(
                "staffId", "1"));
        final MvcResult result = ra.andReturn();
        assertThat(result.getResponse().getStatus(), is(200));//判断响应状态是否正常
    }

你可能感兴趣的:(springmvc,junit,mock,springmvc,junit,mock)