V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
yumemor
V2EX  ›  Java

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

  •  
  •   yumemor · Mar 1, 2017 · 4070 views
    This topic created in 3358 days ago, the information mentioned may be changed or developed.

    难道不能直接写上 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 replies    2017-03-07 14:06:53 +08:00
    Septembers
        1
    Septembers  
       Mar 1, 2017   ❤️ 1
    这个不是 JUnit 的功能
    这是 Spring 的功能 您应该阅读 Spring 关于 Test 的文档
    SoloCompany
        2
    SoloCompany  
       Mar 1, 2017
    标题背锅侠
    wc951
        3
    wc951  
       Mar 1, 2017
    我记得配置文件可以用通配符的, ContextConfiguration 这个注解是 spring test 里的吧
    orcusfox
        4
    orcusfox  
       Mar 1, 2017
    写一个 total 的 xml 引用全部 xml 就好 本来就应该这么干 或者用 annotation
    Cbdy
        5
    Cbdy  
       Mar 1, 2017 via Android
    如果测试比较多,可以自定义一个注解
    Michaelssss
        6
    Michaelssss  
       Mar 2, 2017
    。。。。楼主绝对没读文档。。。另外 Spring 的 Xml 也不熟。。。
    yumemor
        7
    yumemor  
    OP
       Mar 2, 2017
    @Michaelssss 嗯 .... 所以 你知道吗?
    Michaelssss
        8
    Michaelssss  
       Mar 3, 2017
    @yumemor 事实上你可以直接 classpath:/xxxx/*.xml 直接就全部引入了。。。和 web.xml 的用法一样,另外是 spring-mvc.xml 你应该没用 classpath 引入的,而是直接写了相对路径
    iminto
        9
    iminto  
       Mar 7, 2017
    这个是你自己不熟。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3418 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 46ms · UTC 11:58 · PVG 19:58 · LAX 04:58 · JFK 07:58
    ♥ Do have faith in what you're doing.