yumemor
V2EX  ›  Java

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

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