{"id":9325,"date":"2023-02-13T20:49:12","date_gmt":"2023-02-13T12:49:12","guid":{"rendered":"http:\/\/123.57.164.21\/?p=9325"},"modified":"2023-02-16T21:19:21","modified_gmt":"2023-02-16T13:19:21","slug":"dbunit-%e5%9c%a8spring-boot%e9%a1%b9%e7%9b%ae%e4%b8%ad-%e5%87%ba%e7%8e%b0ambiguoustablenameexception","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=9325","title":{"rendered":"dbunit \u5728spring boot\u9879\u76ee\u4e2d \u51fa\u73b0AmbiguousTableNameException"},"content":{"rendered":"\n<p>\u5f53\u6211\u4eec\u5728Java\u9879\u76ee\u4e2d\uff0c\u4f7f\u7528dbunit\u505aUT\u6d4b\u8bd5\u65f6\uff0c\u6709\u53ef\u80fd\u4f1a\u53d1\u751f\u4e0b\u9762AmbiguousTableNameException\u7684\u9519\u8bef\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>UT\u6d4b\u8bd5\u7c7b\u4ee3\u7801\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@SpringBootTest(classes = ServiceTestApplication.class)\n@ActiveProfiles(\"test\")\n@TestExecutionListeners({ \n\tDependencyInjectionTestExecutionListener.class,\n\tDirtiesContextTestExecutionListener.class,\n    TransactionalTestExecutionListener.class,\n\tMockitoTestExecutionListener.class,\n\tDbUnitTestExecutionListener.class })\n@ComponentScan(value = \"com.xxxx\")\n@AutoConfigureMockMvc\npublic class TestControllerTest {\n\n\t@Autowired\n\tprivate MockMvc mockMvc;\n\n\t@Test\n\t@DatabaseSetup(value = { \"classpath:com\/test_102N.xls\" })\n\t@DatabaseTearDown(value = { \"classpath:databaseTearDown\/DatabaseTearDown.xls\" })\n\tvoid test_102N() throws Exception {\n\n\t\t\/\/ \u30d8\u30c3\u30c0\u30fc\u306e\u60c5\u5831\u306e\u8a2d\u5b9a\n\t\torg.springframework.http.HttpHeaders headers = new HttpHeaders();\n\t\theaders.add(\"x-user-id\", \"123\");\n\t\theaders.add(\"x-system-id\", \"456\");\n\n\t\tMultiValueMap&lt;String, String> params = new LinkedMultiValueMap&lt;>();\n\t\tparams.put(\"testKbn\", Collections.singletonList(\"1\"));\n\n\t\t\/\/ \u30ea\u30af\u30a8\u30b9\u30c8\u3092\u5b9f\u884c\u3059\u308b\n\t\tMvcResult mvcResult = mockMvc.perform(get(\"\/test\").queryParams(params).headers(headers)).andReturn();\n\n\t\tString actualContent = mvcResult.getResponse().getContentAsString(Charset.forName(\"UTF-8\"));\n\t\t\/\/ \u30ea\u30bf\u30fc\u30f3\u30b9\u30c6\u30fc\u30bf\u30b9\u304c\u6b63\u3057\u3044\u304b\u3069\u3046\u304b\u3092\u5224\u65ad\u3059\u308b\n\t\tint status = mvcResult.getResponse().getStatus();\n\t\tassertEquals(HttpStatus.OK.value(), status);\n\t}\n\n}<\/pre>\n\n\n\n<p><strong>application-test.yaml<\/strong>\uff1a<\/p>\n\n\n\n<p>\u6ce8\u610f\uff1a\u8fd9\u91cc\u7528jdbc:postgresql:\/\/localhost:5432\/postgres?currentSchema=ut_db \u6307\u5b9a\u8fde\u63a5\u7684Schema\u4e0d\u8d77\u4f5c\u7528<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">spring.datasource:\n  url: jdbc:postgresql:\/\/localhost:5432\/postgres\n  username: dummy\n  password: dummyvalue\n  driver-class-name: org.postgresql.Driver\n\nspring.main.allow-circular-references: true\n\nlogging:\n  level:\n    com.jtb.nucleus: debug<\/pre>\n\n\n\n<p><strong>\u5f02\u5e38\u4fe1\u606f\uff1a<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/02\/\u56fe\u7247-21-1024x263.png\" alt=\"\" class=\"wp-image-9326\" width=\"682\" height=\"174\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/\u56fe\u7247-21-1024x263.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/\u56fe\u7247-21-300x77.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/\u56fe\u7247-21-768x197.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/\u56fe\u7247-21-1536x394.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/\u56fe\u7247-21-830x213.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/\u56fe\u7247-21-230x59.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/\u56fe\u7247-21-350x90.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/\u56fe\u7247-21-480x123.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/\u56fe\u7247-21.png 1948w\" sizes=\"(max-width: 682px) 100vw, 682px\" \/><\/figure><\/div>\n\n\n\n<p><strong>\u5206\u6790\u539f\u56e0<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u5f53\u4e00\u4e2a\u5b9e\u4f8b\u4e0b\u5b58\u5728\u591a\u4e2a\u6570\u636e\u5e93\uff0c\u800c\u4e14\u8fd9\u4e2a\u6570\u636e\u5e93\u4e2d\u5b58\u5728\u76f8\u540c\u540d\u79f0\u7684\u8868\uff0cDbunit\u4f1a\u62a5\u4e00\u4e2a\u4e0d\u80fd\u533a\u5206\u662f\u54ea\u4e2a\u8868\u7684\u9519\u8bef\u3002<\/p>\n\n\n\n<p>\u5728\u6570\u636e\u5e93\u4e2d<strong>\u5b58\u5728\u4e24\u4e2a\u540c\u540d\u7684\u6570\u636e\u8868<\/strong>\uff0c\u540c\u540d-&gt;\u4e0d\u533a\u5206\u5927\u5c0f\u5199\u7684\u540c\u540d\uff0c\u6bd4\u5982user\u548cUSER\u4e5f\u662f\u540c\u540d\u7684\uff0c\u7279\u522b\u7684\u662f\u8fd9\u4e24\u4e2a\u540c\u540d\u6570\u636e\u8868\u53ef\u80fd\u662f\u5b58\u5728\u672c\u5730\u4e2d\u4e24\u4e2a\u4e0d\u540c\u7684Schema\u4e2d\u3002<\/p>\n\n\n\n<p><strong>\u5728\u6ca1\u6709\u6307\u5b9aSchema\u7684\u60c5\u51b5\u4e0b\uff0cDbUnit\u4f1a\u9ed8\u8ba4\u626b\u63cf\u6574\u4e2a\u5b9e\u4f8b\u3002<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/02\/\u56fe\u7247-22.png\" alt=\"\" class=\"wp-image-9328\" width=\"395\" height=\"344\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/\u56fe\u7247-22.png 870w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/\u56fe\u7247-22-300x262.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/\u56fe\u7247-22-768x671.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/\u56fe\u7247-22-830x725.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/\u56fe\u7247-22-230x201.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/\u56fe\u7247-22-350x306.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/\u56fe\u7247-22-480x419.png 480w\" sizes=\"(max-width: 395px) 100vw, 395px\" \/><\/figure><\/div>\n\n\n\n<p><strong>\u89e3\u51b3\u65b9\u6cd5<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<ul><li>\u91cd\u547d\u540d\u4f60\u5f53\u524d\u9879\u76ee\u7684\u6570\u636e\u8868\uff0c\u4f7f\u5176\u4e0d\u4e0e\u6570\u636e\u5e93\u4e2d\u5176\u4ed6\u6570\u636e\u8868\u91cd\u540d\uff0c\u5982user\u547d\u540d\u4e3at_user\u3002<\/li><li>\u5220\u9664\u5176\u4ed6\u6570\u636e\u5e93\u4e2d\u7684\u91cd\u540d\u6570\u636e\u8868\uff0c\u4f7f\u4f60\u5f53\u524d\u6570\u636e\u8868\u547d\u540d\u552f\u4e00\u3002<\/li><li>\u5047\u5982\u5728\u8fde\u63a5\u6570\u636e\u5e93\u65f6\u672a\u6307\u5b9a\u5230\u54ea\u4e2a\u5177\u4f53\u7684schema\uff0c\u90a3\u4e48\u5c31\u6307\u5b9aschema\u3002<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>\u6307\u5b9aschema\u7684\u4f8b\u5b50<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u521b\u5efadb\u8fde\u63a5Configuration\u7c7b\uff08\u6ce8\u610fTestConfiguration\u6ce8\u89e3\uff0c\u56e0\u4e3a\u8be5\u914d\u7f6e\u53ea\u60f3\u5728Test\u7c7b\u6709\u6548\uff09<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">package com.xxx.config;\n\nimport javax.sql.DataSource;\n\nimport org.springframework.context.annotation.Bean;\nimport org.springframework.context.annotation.Configuration;\n\nimport com.github.springtestdbunit.bean.DatabaseConfigBean;\nimport com.github.springtestdbunit.bean.DatabaseDataSourceConnectionFactoryBean;\n\n\n\/\/@Configuration\n@TestConfiguration\npublic class DbUnitConfig {\n\n    @Bean(\"dbUnitConnection\")\n    DatabaseDataSourceConnectionFactoryBean getTestConnection(DataSource dataSource) {\n        DatabaseDataSourceConnectionFactoryBean bean = new DatabaseDataSourceConnectionFactoryBean();\n        bean.setDataSource(dataSource);\n        DatabaseConfigBean databaseConfigBean = new DatabaseConfigBean();\n        databaseConfigBean.setAllowEmptyFields(true);\n        bean.setDatabaseConfig(databaseConfigBean);\n        bean.setSchema(\"ut_db\");\n\n        return bean;\n    }\n}<\/pre>\n\n\n\n<p>\u5728\u6d4b\u8bd5\u7c7b\u901a\u8fc7DbUnitConfiguration\u6307\u5b9a\u6570\u636e\u6e90<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@SpringBootTest(classes = ServiceTestApplication.class)\n@ActiveProfiles(\"test\")\n@TestExecutionListeners({ \n\tDependencyInjectionTestExecutionListener.class,\n\tDirtiesContextTestExecutionListener.class,\n    TransactionalTestExecutionListener.class,\n\tMockitoTestExecutionListener.class,\n\tDbUnitTestExecutionListener.class })\n@ComponentScan(value = \"com.xxxx\")\n@AutoConfigureMockMvc\n@DbUnitConfiguration(dataSetLoader = XlsDataSetLoader.class, databaseConnection = { \"dbUnitConnection\" })\n\npublic class TestControllerTest {\n\n\t@Autowired\n\tprivate MockMvc mockMvc;\n\n\t@Test\n\t@DatabaseSetup(value = { \"classpath:com\/test_102N.xls\" })\n\t@DatabaseTearDown(value = { \"classpath:databaseTearDown\/DatabaseTearDown.xls\" })\n\tvoid test_102N() throws Exception {\n\n\t\t\/\/ \u30d8\u30c3\u30c0\u30fc\u306e\u60c5\u5831\u306e\u8a2d\u5b9a\n\t\torg.springframework.http.HttpHeaders headers = new HttpHeaders();\n\t\theaders.add(\"x-user-id\", \"123\");\n\t\theaders.add(\"x-system-id\", \"456\");\n\n\t\tMultiValueMap&lt;String, String> params = new LinkedMultiValueMap&lt;>();\n\t\tparams.put(\"testKbn\", Collections.singletonList(\"1\"));\n\n\t\t\/\/ \u30ea\u30af\u30a8\u30b9\u30c8\u3092\u5b9f\u884c\u3059\u308b\n\t\tMvcResult mvcResult = mockMvc.perform(get(\"\/test\").queryParams(params).headers(headers)).andReturn();\n\n\t\tString actualContent = mvcResult.getResponse().getContentAsString(Charset.forName(\"UTF-8\"));\n\t\t\/\/ \u30ea\u30bf\u30fc\u30f3\u30b9\u30c6\u30fc\u30bf\u30b9\u304c\u6b63\u3057\u3044\u304b\u3069\u3046\u304b\u3092\u5224\u65ad\u3059\u308b\n\t\tint status = mvcResult.getResponse().getStatus();\n\t\tassertEquals(HttpStatus.OK.value(), status);\n\t}\n\n}<\/pre>\n\n\n\n<p><strong>\u8865\u5145<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u53e6\u5916\uff0cDbUnitConfig\u4f5c\u4e3aTestConfiguration\uff0c\u4f1a\u5728\u6d4b\u8bd5\u7c7b\u542f\u52a8\u65f6\u88ab\u626b\u63cf\u52a0\u8f7d\uff0c\u4f1a\u521b\u5efa @Bean(&#8220;dbUnitConnection&#8221;)\uff0c\u4f46\u662f\u8fd9\u4e2aBean\u9700\u8981\u521b\u5efa\u4e00\u4e2aDataSource dataSource\u8fde\u63a5\uff0c\u4f46\u662f\u5047\u5982\u6211\u4eec\u7684\u6d4b\u8bd5\u5de5\u7a0b\u4e0d\u9700\u8981\u8fde\u63a5\u6570\u636e\u5e93\uff0c\u800c\u6ca1\u6709\u8bbe\u7f6e\u6570\u636e\u5e93\u7684\u914d\u7f6e\uff0c\u542f\u52a8\u6d4b\u8bd5\u7c7b\u65f6\u4f1a\u5bfc\u81f4\u51fa\u4e0b\u9762\u7684\u9519\u8bef\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Description:\n\nParameter 0 of method getTestConnection in DbUnitConfig required a bean of type 'javax.sql.DataSource' that could not be found.\n\n\nAction:\n\nConsider defining a bean of type 'javax.sql.DataSource' in your configuration.\n<\/pre>\n\n\n\n<p>\u89e3\u51b3\u65b9\u6cd5\u662f\u6211\u4eec\u53ef\u4ee5\u5728\u6d4b\u8bd5\u7c7b\u52a0\u4e0a\uff0c\u6765mock\u4e00\u4e2aDataSource<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@MockBean\nDataSource dataSource;<\/pre>\n\n\n\n<p>\u5b8c\u6574\u4ee3\u7801<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@SpringBootTest(classes = ServiceTestApplication.class)\n@ActiveProfiles(\"test\")\n@TestExecutionListeners({ \n\tDependencyInjectionTestExecutionListener.class,\n\tDirtiesContextTestExecutionListener.class,\n    TransactionalTestExecutionListener.class,\n\tMockitoTestExecutionListener.class,\n\tDbUnitTestExecutionListener.class })\n@ComponentScan(value = \"com.xxxx\")\n@AutoConfigureMockMvc\n@DbUnitConfiguration(dataSetLoader = XlsDataSetLoader.class, databaseConnection = { \"dbUnitConnection\" })\n\npublic class TestControllerTest {\n\n\t\/\/ \u8fd9\u90e8\u5206\u5f88\u91cd\u8981\n @MockBean\n\tDataSource dataSource;\n\n\t@Autowired\n\tprivate MockMvc mockMvc;\n\n\t@Test\n\t@DatabaseSetup(value = { \"classpath:com\/test_102N.xls\" })\n\t@DatabaseTearDown(value = { \"classpath:databaseTearDown\/DatabaseTearDown.xls\" })\n\tvoid test_102N() throws Exception {\n\n\t\t\/\/ \u30d8\u30c3\u30c0\u30fc\u306e\u60c5\u5831\u306e\u8a2d\u5b9a\n\t\torg.springframework.http.HttpHeaders headers = new HttpHeaders();\n\t\theaders.add(\"x-user-id\", \"123\");\n\t\theaders.add(\"x-system-id\", \"456\");\n\n\t\tMultiValueMap&lt;String, String> params = new LinkedMultiValueMap&lt;>();\n\t\tparams.put(\"testKbn\", Collections.singletonList(\"1\"));\n\n\t\t\/\/ \u30ea\u30af\u30a8\u30b9\u30c8\u3092\u5b9f\u884c\u3059\u308b\n\t\tMvcResult mvcResult = mockMvc.perform(get(\"\/test\").queryParams(params).headers(headers)).andReturn();\n\n\t\tString actualContent = mvcResult.getResponse().getContentAsString(Charset.forName(\"UTF-8\"));\n\t\t\/\/ \u30ea\u30bf\u30fc\u30f3\u30b9\u30c6\u30fc\u30bf\u30b9\u304c\u6b63\u3057\u3044\u304b\u3069\u3046\u304b\u3092\u5224\u65ad\u3059\u308b\n\t\tint status = mvcResult.getResponse().getStatus();\n\t\tassertEquals(HttpStatus.OK.value(), status);\n\t}\n\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u6211\u4eec\u5728Java\u9879\u76ee\u4e2d\uff0c\u4f7f\u7528dbunit\u505aUT\u6d4b\u8bd5\u65f6\uff0c\u6709\u53ef\u80fd\u4f1a\u53d1\u751f\u4e0b\u9762AmbiguousTableNameEx [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,27],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/9325"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9325"}],"version-history":[{"count":8,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/9325\/revisions"}],"predecessor-version":[{"id":9349,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/9325\/revisions\/9349"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9325"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9325"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9325"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}