spring mvc 3.2 测试
程序员文章站
2022-06-08 13:25:40
...
@ContextConfiguration(locations = {"classpath:spring-config.xml","classpath:dispatcher-servlet.xml"}) @RunWith(SpringJUnit4ClassRunner.class) @Transactional @TransactionConfiguration(transactionManager = "txManager", defaultRollback = true) public class BaseTest { }
public class AcctUserActionTest extends BaseTest { @Resource private AcctUserAction acctUserAction; @Test public void testSaveUser() throws Exception { // 执行URI对应的action MockMvc mockMvc = MockMvcBuilders.standaloneSetup(acctUserAction).build(); ResultActions ra = mockMvc.perform(MockMvcRequestBuilders .post("/user/saveUser") .accept(MediaType.APPLICATION_JSON) .param("username", "xxx") .param("password", "123123") .param("lastLoginDate", "2012-12-12") .param("lastLoginIp", "127.0.0.1")); MvcResult mr = ra.andReturn(); String result = mr.getResponse().getForwardedUrl(); Assert.assertEquals("/common/success",result); } }