V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
yumemor
V2EX  ›  Java

为什么使用 Junit Test 必须要写上全部配置文件?

  •  
  •   yumemor · 2017 年 3 月 1 日 · 3888 次点击
    这是一个创建于 3241 天前的主题,其中的信息可能已经有所发展或是发生改变。

    难道不能直接写上 web.xml 中的一个配置文件就行了嘛? 主要的配置文件里面有其它配置文件的引用地址 。

    但是在单元测试的时候,为什么必须要这样写?

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = {"classpath:springmybatis/spring-mvc.xml","classpath:springmybatis/spring-mybatis.xml","classpath:springmybatis/spring-shiro-web.xml","classpath:springmybatis/mybatis-config.xml"})
    

    感觉很恶心啊!

    理想状态:

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("classpath:springmybatis/spring-mvc.xml")
    

    但是这样写会说:NoSuchBeanDefinitionException

    web.xml 文件都是通过主要的 spring-mvc.xml 来进行加载的,为什么 Junit 不行?这能不能算是一个功能缺陷?

    9 条回复    2017-03-07 14:06:53 +08:00
    Septembers
        1
    Septembers  
       2017 年 3 月 1 日   ❤️ 1
    这个不是 JUnit 的功能
    这是 Spring 的功能 您应该阅读 Spring 关于 Test 的文档
    SoloCompany
        2
    SoloCompany  
       2017 年 3 月 1 日
    标题背锅侠
    wc951
        3
    wc951  
       2017 年 3 月 1 日
    我记得配置文件可以用通配符的, ContextConfiguration 这个注解是 spring test 里的吧
    orcusfox
        4
    orcusfox  
       2017 年 3 月 1 日
    写一个 total 的 xml 引用全部 xml 就好 本来就应该这么干 或者用 annotation
    Cbdy
        5
    Cbdy  
       2017 年 3 月 1 日 via Android
    如果测试比较多,可以自定义一个注解
    Michaelssss
        6
    Michaelssss  
       2017 年 3 月 2 日
    。。。。楼主绝对没读文档。。。另外 Spring 的 Xml 也不熟。。。
    yumemor
        7
    yumemor  
    OP
       2017 年 3 月 2 日
    @Michaelssss 嗯 .... 所以 你知道吗?
    Michaelssss
        8
    Michaelssss  
       2017 年 3 月 3 日
    @yumemor 事实上你可以直接 classpath:/xxxx/*.xml 直接就全部引入了。。。和 web.xml 的用法一样,另外是 spring-mvc.xml 你应该没用 classpath 引入的,而是直接写了相对路径
    iminto
        9
    iminto  
       2017 年 3 月 7 日
    这个是你自己不熟。
    关于   ·   帮助文档   ·   自助推广系统   ·   博客   ·   API   ·   FAQ   ·   Solana   ·   1168 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 23:36 · PVG 07:36 · LAX 15:36 · JFK 18:36
    ♥ Do have faith in what you're doing.