Ant 构建 Junit 测试 Autowire 失败
当我在 eclipse 中单独运行它们时,项目中配置的 junit 工作正常,但是当我使用 junit 目标运行 Ant 构建时,测试类会被执行,但会为自动装配组件抛出 NPE。我已经指定了下面的上下文配置,它应该负责查找上下文并从中自动装配 bean。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:DaoContextTest.xml" })
我的 ant 构建 junit 目标如下所示:
<target name="junit" depends="build">
<junit printsummary="yes" haltonfailure="yes">
<classpath refid="classpath-test" />
<classpath location="${webclasses.dir}" />
<formatter type="plain" usefile="false"/>
<batchtest fork="yes" todir="${test.classes}">
<fileset dir="${src.dir}">
<include name="**/*Test.java" />
</fileset>
</batchtest>
</junit>
</target>
我在 ant 调试模式下验证了类路径,并且它具有此 xml 文件的正确位置。我想如果 xml 丢失,它会抱怨无法加载上下文,并且如果我的 DaoContext xml 有不正确的 bean 配置,那么当我手动运行它时,我的 junit 将不会运行,所以我怀疑它一定是 Ant 特有的东西。
我得到的例外是自动装配组件上的 NPE。我正在使用 ant 1.9.6,我尝试将 ant-junit.1.9 jar 复制到 web-inf lib,但这没有帮助。如果有人能指出我正确的方向,我将不胜感激? 我发现类似的帖子这里但是它仍然开放。
The junits configured in project works fine when I run them within eclipse individually but when I run the Ant build with junit goal the test classes gets executed but throws NPE for the autowired components. I have specified below context configuration which should take care of finding the context and autowiring the beans from it.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:DaoContextTest.xml" })
My ant build junit goal looks as below:
<target name="junit" depends="build">
<junit printsummary="yes" haltonfailure="yes">
<classpath refid="classpath-test" />
<classpath location="${webclasses.dir}" />
<formatter type="plain" usefile="false"/>
<batchtest fork="yes" todir="${test.classes}">
<fileset dir="${src.dir}">
<include name="**/*Test.java" />
</fileset>
</batchtest>
</junit>
</target>
I verified the classpath in ant debug mode and it has the right location of this xml file. I suppose if the xml was missing it would have complained failed to load context and if my DaoContext xml had incorrect bean configuration then my junit wouldnt have run when I run it manually so I suspect it must be something specific to Ant.
And the exception I get is NPE on the autowired components. I am using ant 1.9.6, I tried copying the ant-junit.1.9 jar to the web-inf lib but that did not help. Much appreciated if anyone can please point me in the right direction?
I found similiar post here but it is still open.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案是这个和这个。
简而言之:Ant 默认使用 Junit3 测试运行程序,因此要强制执行 Junit4 运行程序,需要在每个 Junit 类中包含以下方法。
Solution was this and this.
In brief: Ant is using Junit3 test runner by default so to enforce Junit4 runner need to include below method in each of the Junit classes.