开发者

Ibatis2 and test context

开发者 https://www.devze.com 2022-12-11 10:42 出处:网络
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\".

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开发者_Go百科/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.


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:

<sqlMapConfig><sqlMap resource="META-INF/ibatis/mapping/MyObject.xml"/></sqlMapConfig>

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.


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


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:

Remember that iBATIS loads resources from the class path, so be sure to add the 'Account.xml' file to the class path.

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)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号