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

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

  •  
  •   yumemor · 2017-03-01 18:03:49 +08:00 · 3192 次点击
    这是一个创建于 2610 天前的主题,其中的信息可能已经有所发展或是发生改变。

    难道不能直接写上 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-03-01 18:13:40 +08:00   ❤️ 1
    这个不是 JUnit 的功能
    这是 Spring 的功能 您应该阅读 Spring 关于 Test 的文档
    SoloCompany
        2
    SoloCompany  
       2017-03-01 21:46:12 +08:00
    标题背锅侠
    wc951
        3
    wc951  
       2017-03-01 21:48:21 +08:00
    我记得配置文件可以用通配符的, ContextConfiguration 这个注解是 spring test 里的吧
    napsterwu
        4
    napsterwu  
       2017-03-01 22:41:53 +08:00
    写一个 total 的 xml 引用全部 xml 就好 本来就应该这么干 或者用 annotation
    Cbdy
        5
    Cbdy  
       2017-03-01 23:23:21 +08:00 via Android
    如果测试比较多,可以自定义一个注解
    Michaelssss
        6
    Michaelssss  
       2017-03-02 16:28:37 +08:00
    。。。。楼主绝对没读文档。。。另外 Spring 的 Xml 也不熟。。。
    yumemor
        7
    yumemor  
    OP
       2017-03-02 16:54:51 +08:00
    @Michaelssss 嗯 .... 所以 你知道吗?
    Michaelssss
        8
    Michaelssss  
       2017-03-03 07:29:02 +08:00
    @yumemor 事实上你可以直接 classpath:/xxxx/*.xml 直接就全部引入了。。。和 web.xml 的用法一样,另外是 spring-mvc.xml 你应该没用 classpath 引入的,而是直接写了相对路径
    iminto
        9
    iminto  
       2017-03-07 14:06:53 +08:00
    这个是你自己不熟。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5750 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 01:50 · PVG 09:50 · LAX 18:50 · JFK 21:50
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.