Ibatis2 和测试上下文
我的 Spring 项目中的 Ibatis 遇到了一个愚蠢的配置问题。请不要对我说这一切是如何设置的,我只是遵循“内部项目结构政策”。
这是结构,我们有“src/main/resources/META-INF/”文件夹,其中包含应用程序使用的所有配置文件,然后有一个“src/test/resources/META-INF/”,其中仅包含具有不同设置来运行单元测试的配置文件。
在我们的例子中,只有一个文件,src/main/resources/META-INF/spring/application-config.xml 变成src/测试/resources/META-INF/spring/test-application-config.xml。我不会概述两者之间的细微差别,因为这部分工作得很好。
test-application-config.xml 导入 src/main/resources/META-INF/spring/data-access- config.xml 文件就好了,它依次使用 src/main/resources/META-INF/ibatis/sqlmap-config.xml 成功了……之后就是地狱了。
到目前为止,我们都在使用 Spring 在类路径中查找下一个配置文件,但是当我们点击 sqlmap-config.xml 时,我们将 spring 框架留给了 ibatis 框架,我相信它会加载相对于类路径在其中定义的资源文件(从文档中获取,无论这意味着什么)。
在 sqlmap-config.xml 中定义了一些我们正在使用的资源文件,这些资源文件位于 src/main/resources/META-INF/ibatis/mapping 文件夹。 它们是这样引用的:
<sqlMapConfig><sqlMap resource="/META-INF/ibatis/mapping/MyObject.xml"/></sqlMapConfig>
当我正常运行应用程序时,效果很好,但是当我运行 JUnit 测试用例时,我收到 IO 异常,指出它找不到文件 /META-INF/ibatis/mapping/ MyObject.xml。
我尝试将 sqlmap-config.xml 中的路径更改为“mapping/MyObject.xml”,但这没有帮助。我还尝试使用 Spring 类路径前缀“classpath:META-INF/ibatis/mapping/MyObject.xml”,也不起作用。
任何人都知道如何正确设置 Ibatis,使其适用于应用程序和 junit?
谢谢。
I'm having a stupid configuration issue with Ibatis in my Spring project. Please don't jump on me about how all this was setup, I'm just following the "in house project structure policy".
So here is the structure, we have the "src/main/resources/META-INF/" folder that contains all of our config files used by the application, and then there is a "src/test/resources/META-INF/" that contains only the config files that have different settings to run unit testing.
Well in our case that's only one file, the src/main/resources/META-INF/spring/application-config.xml became the src/test/resources/META-INF/spring/test-application-config.xml. I'm am not going to outline the small differences between the two, because that part works fine.
The test-application-config.xml imports the src/main/resources/META-INF/spring/data-access-config.xml file just fine, which in turns use the src/main/resources/META-INF/ibatis/sqlmap-config.xml successfully... After that is when it goes to Hell.
See up until now we're using Spring to find the next config files in the classpath, but when we hit sqlmap-config.xml we leave the spring framework for the ibatis framework I believe, which loads the resource files defined inside it relative to the classpath (that's taken from the doc, whatever that means).
Inside the sqlmap-config.xml are defined a few resource files we're using that live inside the src/main/resources/META-INF/ibatis/mapping folder.
They are referenced like this:
<sqlMapConfig><sqlMap resource="/META-INF/ibatis/mapping/MyObject.xml"/></sqlMapConfig>
That works fine when I run the app normally, but when I run my JUnit test cases I get an IO exception stating that it can't find the file /META-INF/ibatis/mapping/MyObject.xml.
I've tried to change the path in the sqlmap-config.xml to "mapping/MyObject.xml" but that didn't help. I've also tried to use the Spring classpath prefix "classpath:META-INF/ibatis/mapping/MyObject.xml", didn't work either.
Anyone would have any idea on how to set that Ibatis properly so it works for both the app and the junit?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了解决这个问题,我从src/test/resources/META-INF文件夹中删除了所有Ibatis文件和文件夹。
src/main/resources/META-INF/ibatis/mapping 文件中的 sqlmap-config.xml 现在映射如下:
请注意与我最初的帖子相比,前导“/”消失了……我认为这就是造成差异的原因。
希望这可以帮助任何遇到类似问题的人。
To solve this problem, I removed all the the Ibatis files and folders from the src/test/resources/META-INF folder.
The sqlmap-config.xml in src/main/resources/META-INF/ibatis/mapping file now maps like this:
Please note that compared to my initial post the leading "/" is gone... I think that's what made the difference here.
Hopes this helps anyone running into similar issues.
只是为了看看您所说的是否确实是问题所在。您可能希望将映射 (MyObject.xml) 与 sqlmap-config.xml 放在同一文件夹中。我这样说是因为我也遇到过 spring + ibatis + 单元测试问题。 (请参阅我提出的已解决的问题)
另外,您可能收到 IO 异常,因为映射文件不存在于容器外部(当您运行测试时)。
您还应该发布从
SqlMapClientFactoryBean
创建的 bean 的定义。这应该具有包含 sqlMapConfig xml 路径的 configLocation 属性Just to see whether what you are saying is actually the problem.. you might want to place your mappings (MyObject.xml) in the same folder as sqlmap-config.xml. I say this because I've had my fair share of spring + ibatis + unit testing problems. (see resolved question asked by me)
Also, you might be getting IO exception because the mappings file does not exist outside the container (when you run tests).
You should also post definition for bean created from
SqlMapClientFactoryBean
. This should have configLocation property that contains path to sqlMapConfig xml我遇到了同样的问题,但找不到(快速)解决方案来解释到底出了什么问题。因此我的回答是这样的。
作为 Ibatis 的 Spring 文档说:
在您的情况下,将位于项目的
META-INF
添加到您的 web 项目构建路径,即如果您使用 Eclipse,请设置.classpath
中(这将在 Eclipse 的 Navigator 视图下可见)I had the same problem and could not find a (quick) solution that explained what exactly could be going wrong. Hence my answer.
As Spring documentation for Ibatis says:
In your case by adding
META-INF
to your webproject build path i.e. if you used Eclipse, set<classpathentry kind="src" path="META-INF"/>
in your projects'.classpath
(This will be visible under Navigator view in Eclipse)