Spring单元测试

环境 spring + mybatis 。mybatis采用自动扫描Mapper接口生成代理注入Spring。spring事务采用AOP实现。
1,引入Spring测试jarspring-test-4.1.6.RELEASE
2,单元测试Service如下:

package com.order.service;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
import com.***.common.result.GridResult;
import com.***.order.dao.pojo.OrderBuyer;
import com.***.order.service.OrderBuyerService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring/applicationContext*.xml")
@Transactional
public class OrderBuyerServiceTest {
    @Autowired
    private OrderBuyerService service;
    @Test
    public void testQueryAll() throws Exception {
        GridResult<OrderBuyer> grid = service.querySellOrders(new OrderBuyer(), 1, 10);
        System.out.println("测试" + grid.toString());
    }

}

事务方法测试,因为单元测试默认Rollback的,所以关闭Rollback

@Test
    @Transactional
    @Rollback(false)
    public void testSaveOrder() throws Exception {
        int i = farmOrderService.saveOrder(farmOrder);
        Assert.assertEquals(i, 100);
    }

3,单元测试Controller
具体内容,参考官方文档:
http://docs.spring.io/spring/docs/3.2.13.RELEASE/spring-framework-reference/htmlsingle/#spring-mvc-test-framework
http://www.cnblogs.com/naruto469/p/4087821.html 出错后参考博客

/** * 此处必须引入,否则报错status() get() 方法找不到 */
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
/** * 此处必须引入,否则报错status() get() 方法找不到 */
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
//@RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration("classpath:spring/applicationContext*.xml")
//@EnableWebMvc
//@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration("classpath:spring/applicationContext*.xml")
@EnableWebMvc
public class AppFundControllerTest {
    @Autowired
    private WebApplicationContext wac;
    private MockMvc mockMvc;
    private static final Logger log = LoggerFactory.getLogger(AppFundControllerTest.class);
    @Before
    public void setup() throws Exception {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
        // this.mockMvc = MockMvcBuilders.standaloneSetup(new AppFundController()).build();
    }
    @Test
    public void testReceive() throws Exception {
        // 1、mockMvc.perform执行一个请求;
        // 2、MockMvcRequestBuilders.get("/user/1")构造一个请求
        // 3、ResultActions.andExpect添加执行完成后的断言
        // 4、ResultActions.andDo添加一个结果处理器,表示要对结果做点什么事情,比如此处使用MockMvcResultHandlers.print()输出整个响应结果信息。
        // 5、ResultActions.andReturn表示执行完成后返回相应的结果。
        // MvcResult result =
        // mockMvc.perform(MockMvcRequestBuilders.get("/****/appfindtopfundclass.html"))
        // // .andExpect(MockMvcResultMatchers.view().name("user/view"))
        // // .andExpect(MockMvcResultMatchers.model().attributeExists("user"))
        // // .andDo(MockMvcResultHandlers.print())
        // .andReturn();
        MvcResult result = this.mockMvc
                .perform(get("/***App/appfindtopfundclass.html").accept(MediaType.ALL).param("id", "256"))
                .andExpect(status().isOk()).andDo(MockMvcResultHandlers.print()).andReturn();
        log.debug("查询结果: " + result.getResponse().getHeader("Content-Type"));
    }
}

你可能感兴趣的:(Spring单元测试)