zoukankan      html  css  js  c++  java
  • spring mvc 和junit 4集成的注意点

    常规步骤:

    1、导入jar包,主要有两个,spring-test 和 junit4,主要用maven管理,直接依赖即可。可以在这个网站上进行查找或下载:http://mvnrepository.com/

    2、编写基类,这也不用每个测试类都要实现一遍,代码如下:

    /**
     * 显示基类,主要就是用来加载配置文件的 
     * @author joy
     *
     */
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration({"classpath:/spring/applicationContext-base.xml","classpath:springmvc-config.xml","classpath:sqlMapConfig.xml"})
    public class BaseJunit4Test extends AbstractJUnit4SpringContextTests{
      //do nothing
    }

    3、编写测试类,注意继承基类

    import static org.junit.Assert.assertEquals;
    
    import org.junit.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    
    import com.wei.interfaces.UserService;
    import com.wei.junit.BaseJunit4Test;
    
    public class UserServiceImplTest extends BaseJunit4Test {
        
        @Autowired
        private UserService userService;
        
        @Test
        public void testGetUserById(){
            assertEquals(userService.getUserById(1).getLoginName(), "admin");
        }       
    }

    注意点:

      1、已经有了一个完整mvc层级架构,这样依赖的时候不会报无法依赖注入的错误;

      2、要添加servlet-api,否则会报如下错误:

      

      have no idea why is that,知道的朋友恳请指教。。

  • 相关阅读:
    bzoj4513: [Sdoi2016]储能表
    bzoj4000: [TJOI2015]棋盘
    bzoj3067: Hyperdrome
    bzoj4943: [Noi2017]蚯蚓
    bzoj4044: [Cerc2014] Virus synthesis
    bzoj3676: [Apio2014]回文串
    bzoj4543: [POI2014]Hotel加强版
    bzoj1921: [Ctsc2010]珠宝商
    bzoj4754: [Jsoi2016]独特的树叶
    作图的配色
  • 原文地址:https://www.cnblogs.com/bookwed/p/4553812.html
Copyright © 2011-2022 走看看