使用TestNG測試含有MyBatis的項目時,可能出現找不到XML文件的異常
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):
- 假設你的MyBatis對應的XML文件都放在 src/main/resources/mapper 下
- 編譯後 target/classes 文件夾中會包含 mapper 文件夾
- target/test-classes 文件夾中沒有 mapper 文件夾 !所以報錯
解決方法
pom.xml 文件中將 Test 的 resources 目錄指向 src/main/resources
<build>
<testResources>
<!--測試的時候直接讀取src/main/resources的資源文件-->
<testResource>
<directory>${project.basedir}/src/main/resources</directory>
</testResource>
</testResources>
</build>