ssm(6)spring-test

本文详细介绍了如何使用Spring Test框架进行Spring MVC控制器的单元测试。通过一个具体的例子,展示了如何设置WebApplicationContext,创建MockMvc对象,并执行POST请求的测试。此外,还提供了处理请求和断言响应的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration//注解该注解之后才能获取@Autowired的context
@ContextConfiguration({"classpath:application.xml","classpath:springmvc.xml"})//添加配置
public class SpringTestContorllerTest {
    @Autowired
    WebApplicationContext  context;

    MockMvc  mockMvc;

    @Before
    public  void  initMockMvc(){
        mockMvc=  MockMvcBuilders.webAppContextSetup(context).build();
    }

    @Test
    public  void  testLogin() throws Exception {
        MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/modelAttribute").param("pwd", "123")
                .param("pwd", "123456")).andReturn();
        MockHttpServletRequest request = mvcResult.getRequest();
        MockHttpServletResponse response = mvcResult.getResponse();
    }
}

 待续:spring -test

转载于:https://www.cnblogs.com/gg128/p/9795340.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值