zoukankan      html  css  js  c++  java
  • 使用Spring配合Junit进行单元测试的总结

     最近公司的项目和自己的项目中都用到了spring集成junit进行单元测试,总结一下几种基本的用法:
      1.直接对spring中注入的bean进行测试(以DAO为例):
      在测试类上添加@RunWith注解指定使用springJunit的测试运行器,@ContextConfiguration注解指定测试用的spring配置文件的位置
      之后我们就可以注入我们需要测试的bean进行测试,Junit在运行测试之前会先解析spring的配置文件,初始化spring中配置的bean
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations={"classpath*:spring-config-test.xml"})
    public class TestProjectDao {
    @Autowired
    ProjectDao projectDao;
    @Test
    public void testCreateProjectCode(){
    long applyTime = System.currentTimeMillis();
    Timestamp ts = new Timestamp(applyTime);
    Map codeMap = projectDao.generateCode("5", "8",ts,"院内");
    String projectCode = (String)codeMap.get("_project_code");
    Timestamp apply_time = (Timestamp)codeMap.get("_apply_time");
    System.out.print(projectCode);
    System.out.print(apply_time.toString());
    Assert.assertTrue(projectCode.length()==12);
    }
      2.对springMVC进行测试:
      spring3.2之后出现了org.springframework.test.web.servlet.MockMvc 类,对springMVC单元测试进行支持
      样例如下:
    package com.jiaoyiping.baseproject;
    import com.jiaoyiping.baseproject.privilege.controller.MeunController;
    import com.jiaoyiping.baseproject.training.bean.Person;
    import junit.framework.Assert;
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    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.ResultActions;
    import org.springframework.test.web.servlet.setup.MockMvcBuilders;
    import org.springframework.web.servlet.ModelAndView;
    import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
    import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
    /**
    * Created with IntelliJ IDEA.
    * User: 焦一平
    * Date: 14-9-25
    * Time: 下午6:45
    * To change this template use File | Settings | File Templates.
    */
    @RunWith(SpringJUnit4ClassRunner.class)
    @WebAppConfiguration
    //@ContextConfiguration(classes = {WebMvcConfig.class, MockDataConfig.class})
    @ContextConfiguration(locations={"classpath:/spring/applicationContext.xml", "classpath*:mvc-dispatcher-servlet.xml"})
    public class TestMockMvc {
    @Autowired
    private org.springframework.web.context.WebApplicationContext context;
    MockMvc mockMvc;
    @Before
    public void before() {
    //可以对所有的controller来进行测试
    mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
    //仅仅对单个Controller来进行测试
    // mockMvc = MockMvcBuilders.standaloneSetup(new MeunController()).build();
    }
    @Test
    public void testGetMenu(){
    try {
    System.out.println("----------------------------");
    ResultActions actions =
    this.mockMvc.perform(get("/menu/manage.action"));
    System.out.println(status());//
    System.out.println(content().toString());
    actions.andExpect(status().isOk());
    //            actions.andExpect(content().contentType("text/html"));
    System.out.println("----------------------------");
    } catch (Exception e) {
    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }
    }
  • 相关阅读:
    Optimization on content service with local search in cloud of clouds
    译:滑雪租赁问题(ski rental problem)
    计算机专业顶级学术会议
    论文WAN Optimized Replication of Backup Datasets Using Stream-Informed Delta Compression
    FADE:云存储中数据安全删除
    HTML 标签补充
    python mongodb 的调试
    django MultiValueDictKeyError 错误处理
    Using mongo in django to develop web app with python
    使用list和tuple
  • 原文地址:https://www.cnblogs.com/ceshi2016/p/6247648.html
Copyright © 2011-2022 走看看