Spring Boot与TestContainers集成,使用MySql – BeanDefinitionStoreException

huangapple go评论85阅读模式
英文:

Spring Boot Integration Testing with TestContainers with MySql - BeanDefinitionStoreException

问题

I'm trying to implement an integration test using TestContainers. But I'm continuously getting BeanDefinitionStoreException. My Test Class:

  1. @SpringBootTest
  2. @AutoConfigureMockMvc
  3. class OrderServiceApplicationTests {
  4. @Container
  5. static final MySQLContainer mySQLContainer = new MySQLContainer<>("mysql:8.0.30")
  6. .withDatabaseName("testcontainer")
  7. .withUsername("test")
  8. .withPassword("test");
  9. @Autowired
  10. ObjectMapper objectMapper;
  11. @Autowired
  12. private MockMvc mockMvc;
  13. @Autowired
  14. OrderRepository orderRepository;
  15. @DynamicPropertySource
  16. static void setProperties(DynamicPropertyRegistry properties){
  17. properties.add("spring.datasource.url", mySQLContainer::getJdbcUrl);
  18. properties.add("spring.datasource.username", mySQLContainer::getUsername);
  19. properties.add("spring.datasource.password", mySQLContainer::getPassword);
  20. }
  21. @Test
  22. void shouldPlaceOrder() throws Exception {
  23. OrderItemsDto orderItemsDto = OrderItemsDto.builder()
  24. .skuCode("Test SKU")
  25. .price(new BigDecimal(123.32))
  26. .quantity(2).build();
  27. OrderDto orderDto = OrderDto.builder()
  28. .orderItemsDtos(Arrays.asList(orderItemsDto)).build();
  29. String reqString = objectMapper.writeValueAsString(orderDto);
  30. mockMvc.perform(MockMvcRequestBuilders.post("api/order")
  31. .contentType(MediaType.APPLICATION_JSON)
  32. .content(reqString))
  33. .andExpect(status().isCreated());
  34. Assertions.assertTrue(orderRepository.findAll().size() == 1);
  35. }
  36. }

Here is my POM File:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>3.1.0-SNAPSHOT</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.example</groupId>
  12. <artifactId>order-service</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>order-service</name>
  15. <description>Order Service</description>
  16. <properties>
  17. <java.version>17</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-data-jpa</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-web</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>com.mysql</groupId>
  30. <artifactId>mysql-connector-j</artifactId>
  31. <scope>runtime</scope>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.projectlombok</groupId>
  35. <artifactId>lombok</artifactId>
  36. <optional>true</optional>
  37. </dependency>
  38. <dependency>
  39. <groupId>org.springframework.boot</groupId>
  40. <artifactId>spring-boot-starter-test</artifactId>
  41. <scope>test</scope>
  42. </dependency>
  43. <dependency>
  44. <groupId>org.testcontainers</groupId>
  45. <artifactId>mysql</artifactId>
  46. <scope>test</scope>
  47. </dependency>
  48. <dependency>
  49. <groupId>org.testcontainers</groupId>
  50. <artifactId>junit-jupiter</artifactId>
  51. <scope>test</scope>
  52. </dependency>
  53. </dependencies>
  54. <dependencyManagement>
  55. <dependencies>
  56. <dependency>
  57. <groupId>org.testcontainers</groupId>
  58. <artifactId>testcontainers-bom</artifactId>
  59. <version>1.18.0</version>
  60. <type>pom</type>
  61. <scope>import</scope>
  62. </dependency>
  63. </dependencies>
  64. </dependencyManagement>
  65. <build>
  66. <plugins>
  67. <plugin>
  68. <groupId>org.springframework.boot</groupId>
  69. <artifactId>spring-boot-maven-plugin</artifactId>
  70. <configuration>
  71. <excludes>
  72. <exclude>
  73. <groupId>org.projectlombok</groupId>
  74. <artifactId>lombok</artifactId>
  75. </exclude>
  76. </excludes>
  77. </configuration>
  78. </plugin>
  79. </plugins>
  80. </build>
  81. <repositories>
  82. <
  83. <details>
  84. <summary>英文:</summary>
  85. I&#39;m trying to implement an integration test using TestContainers. But I&#39;m continuously getting BeanDefinitionStoreException.
  86. My Test Class
  87. @SpringBootTest
  88. @AutoConfigureMockMvc
  89. class OrderServiceApplicationTests {
  90. @Container
  91. static final MySQLContainer mySQLContainer = new MySQLContainer&lt;&gt;(&quot;mysql:8.0.30&quot;)
  92. .withDatabaseName(&quot;testcontainer&quot;)
  93. .withUsername(&quot;test&quot;)
  94. .withPassword(&quot;test&quot;);
  95. @Autowired
  96. ObjectMapper objectMapper;
  97. @Autowired
  98. private MockMvc mockMvc;
  99. @Autowired
  100. OrderRepository orderRepository;
  101. @DynamicPropertySource
  102. static void setProperties(DynamicPropertyRegistry properties){
  103. properties.add(&quot;spring.datasource.url&quot;, mySQLContainer::getJdbcUrl);
  104. properties.add(&quot;spring.datasource.username&quot;, mySQLContainer::getUsername);
  105. properties.add(&quot;spring.datasource.password&quot;, mySQLContainer::getPassword);
  106. }
  107. @Test
  108. void shouldPlaceOrder() throws Exception {
  109. OrderItemsDto orderItemsDto = OrderItemsDto.builder()
  110. .skuCode(&quot;Test SKU&quot;)
  111. .price(new BigDecimal(123.32))
  112. .quantity(2).build();
  113. OrderDto orderDto = OrderDto.builder()
  114. .orderItemsDtos(Arrays.asList(orderItemsDto)).build();
  115. String reqString = objectMapper.writeValueAsString(orderDto);
  116. mockMvc.perform(MockMvcRequestBuilders.post(&quot;api/order&quot;)
  117. .contentType(MediaType.APPLICATION_JSON)
  118. .content(reqString))
  119. .andExpect(status().isCreated());
  120. Assertions.assertTrue(orderRepository.findAll().size() == 1);
  121. }
  122. }
  123. Here is my POM File
  124. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  125. &lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  126. xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
  127. &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  128. &lt;parent&gt;
  129. &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  130. &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
  131. &lt;version&gt;3.1.0-SNAPSHOT&lt;/version&gt;
  132. &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
  133. &lt;/parent&gt;
  134. &lt;groupId&gt;com.example&lt;/groupId&gt;
  135. &lt;artifactId&gt;order-service&lt;/artifactId&gt;
  136. &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
  137. &lt;name&gt;order-service&lt;/name&gt;
  138. &lt;description&gt;Order Service&lt;/description&gt;
  139. &lt;properties&gt;
  140. &lt;java.version&gt;17&lt;/java.version&gt;
  141. &lt;/properties&gt;
  142. &lt;dependencies&gt;
  143. &lt;dependency&gt;
  144. &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  145. &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
  146. &lt;/dependency&gt;
  147. &lt;dependency&gt;
  148. &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  149. &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
  150. &lt;/dependency&gt;
  151. &lt;dependency&gt;
  152. &lt;groupId&gt;com.mysql&lt;/groupId&gt;
  153. &lt;artifactId&gt;mysql-connector-j&lt;/artifactId&gt;
  154. &lt;scope&gt;runtime&lt;/scope&gt;
  155. &lt;/dependency&gt;
  156. &lt;dependency&gt;
  157. &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
  158. &lt;artifactId&gt;lombok&lt;/artifactId&gt;
  159. &lt;optional&gt;true&lt;/optional&gt;
  160. &lt;/dependency&gt;
  161. &lt;dependency&gt;
  162. &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  163. &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
  164. &lt;scope&gt;test&lt;/scope&gt;
  165. &lt;/dependency&gt;
  166. &lt;dependency&gt;
  167. &lt;groupId&gt;org.testcontainers&lt;/groupId&gt;
  168. &lt;artifactId&gt;mysql&lt;/artifactId&gt;
  169. &lt;scope&gt;test&lt;/scope&gt;
  170. &lt;/dependency&gt;
  171. &lt;dependency&gt;
  172. &lt;groupId&gt;org.testcontainers&lt;/groupId&gt;
  173. &lt;artifactId&gt;junit-jupiter&lt;/artifactId&gt;
  174. &lt;scope&gt;test&lt;/scope&gt;
  175. &lt;/dependency&gt;
  176. &lt;/dependencies&gt;
  177. &lt;dependencyManagement&gt;
  178. &lt;dependencies&gt;
  179. &lt;dependency&gt;
  180. &lt;groupId&gt;org.testcontainers&lt;/groupId&gt;
  181. &lt;artifactId&gt;testcontainers-bom&lt;/artifactId&gt;
  182. &lt;version&gt;1.18.0&lt;/version&gt;
  183. &lt;type&gt;pom&lt;/type&gt;
  184. &lt;scope&gt;import&lt;/scope&gt;
  185. &lt;/dependency&gt;
  186. &lt;/dependencies&gt;
  187. &lt;/dependencyManagement&gt;
  188. &lt;build&gt;
  189. &lt;plugins&gt;
  190. &lt;plugin&gt;
  191. &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  192. &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
  193. &lt;configuration&gt;
  194. &lt;excludes&gt;
  195. &lt;exclude&gt;
  196. &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
  197. &lt;artifactId&gt;lombok&lt;/artifactId&gt;
  198. &lt;/exclude&gt;
  199. &lt;/excludes&gt;
  200. &lt;/configuration&gt;
  201. &lt;/plugin&gt;
  202. &lt;/plugins&gt;
  203. &lt;/build&gt;
  204. &lt;repositories&gt;
  205. &lt;repository&gt;
  206. &lt;id&gt;spring-milestones&lt;/id&gt;
  207. &lt;name&gt;Spring Milestones&lt;/name&gt;
  208. &lt;url&gt;https://repo.spring.io/milestone&lt;/url&gt;
  209. &lt;snapshots&gt;
  210. &lt;enabled&gt;false&lt;/enabled&gt;
  211. &lt;/snapshots&gt;
  212. &lt;/repository&gt;
  213. &lt;repository&gt;
  214. &lt;id&gt;spring-snapshots&lt;/id&gt;
  215. &lt;name&gt;Spring Snapshots&lt;/name&gt;
  216. &lt;url&gt;https://repo.spring.io/snapshot&lt;/url&gt;
  217. &lt;releases&gt;
  218. &lt;enabled&gt;false&lt;/enabled&gt;
  219. &lt;/releases&gt;
  220. &lt;/repository&gt;
  221. &lt;/repositories&gt;
  222. &lt;pluginRepositories&gt;
  223. &lt;pluginRepository&gt;
  224. &lt;id&gt;spring-milestones&lt;/id&gt;
  225. &lt;name&gt;Spring Milestones&lt;/name&gt;
  226. &lt;url&gt;https://repo.spring.io/milestone&lt;/url&gt;
  227. &lt;snapshots&gt;
  228. &lt;enabled&gt;false&lt;/enabled&gt;
  229. &lt;/snapshots&gt;
  230. &lt;/pluginRepository&gt;
  231. &lt;pluginRepository&gt;
  232. &lt;id&gt;spring-snapshots&lt;/id&gt;
  233. &lt;name&gt;Spring Snapshots&lt;/name&gt;
  234. &lt;url&gt;https://repo.spring.io/snapshot&lt;/url&gt;
  235. &lt;releases&gt;
  236. &lt;enabled&gt;false&lt;/enabled&gt;
  237. &lt;/releases&gt;
  238. &lt;/pluginRepository&gt;
  239. &lt;/pluginRepositories&gt;
  240. &lt;/project&gt;
  241. This is my console.
  242. 16:14:14.843 [main] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils -- Could not detect default configuration classes for test class [com.example.orderservice.OrderServiceApplicationTests]: OrderServiceApplicationTests does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
  243. 16:14:14.940 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper -- Found @SpringBootConfiguration com.example.orderservice.OrderServiceApplication for test class com.example.orderservice.OrderServiceApplicationTests
  244. 16:14:15.163 [main] WARN org.testcontainers.utility.TestcontainersConfiguration -- Attempted to read Testcontainers configuration file at file:/C:/Users/binul/.testcontainers.properties but the file was not found. Exception message: FileNotFoundException: C:\Users\binul\.testcontainers.properties (The system cannot find the file specified)
  245. 16:14:15.169 [main] INFO org.testcontainers.utility.ImageNameSubstitutor -- Image name substitution will be performed by: DefaultImageNameSubstitutor (composite of &#39;ConfigurationFileImageNameSubstitutor&#39; and &#39;PrefixingImageNameSubstitutor&#39;)
  246. . ____ _ __ _ _
  247. /\\ / ___&#39;_ __ _ _(_)_ __ __ _ \ \ \ \
  248. ( ( )\___ | &#39;_ | &#39;_| | &#39;_ \/ _` | \ \ \ \
  249. \\/ ___)| |_)| | | | | || (_| | ) ) ) )
  250. &#39; |____| .__|_| |_|_| |_\__, | / / / /
  251. =========|_|==============|___/=/_/_/_/
  252. :: Spring Boot :: (v3.1.0-SNAPSHOT)
  253. 2023-05-10T16:14:15.441+05:30 INFO 13040 --- [ main] c.e.o.OrderServiceApplicationTests : Starting OrderServiceApplicationTests using Java 17.0.7 with PID 13040 (started by binul in D:\Project Docs\R&amp;D\Microservices-Online-Shop\order-service)
  254. 2023-05-10T16:14:15.444+05:30 INFO 13040 --- [ main] c.e.o.OrderServiceApplicationTests : No active profile set, falling back to 1 default profile: &quot;default&quot;
  255. 2023-05-10T16:14:16.013+05:30 INFO 13040 --- [ main] .t.d.DockerMachineClientProviderStrategy : docker-machine executable was not found on PATH ([C:\Program Files\Common Files\Oracle\Java\javapath, C:\Program Files (x86)\Common Files\Oracle\Java\javapath, C:\Windows\system32, C:\Windows, C:\Windows\System32\Wbem, C:\Windows\System32\WindowsPowerShell\v1.0\, C:\Windows\System32\OpenSSH\, C:\Program Files\Git\cmd, C:\Program Files\Java\jdk1.8.0_311\bin, C:\dev_tools\apache-maven-3.8.3\bin, C:\Program Files\PuTTY\, C:\Program Files\dotnet\, C:\Program Files\nodejs\, C:\Program Files\Meld\, C:\Users\binul\AppData\Local\Microsoft\WindowsApps, C:\Users\binul\AppData\Local\Programs\Microsoft VS Code\bin, C:\dev_tools\gradle-6.8.1\bin, C:\Users\binul\AppData\Roaming\npm, C:\Users\binul\.dotnet\tools])
  256. 2023-05-10T16:14:16.014+05:30 ERROR 13040 --- [ main] o.t.d.DockerClientProviderStrategy : Could not find a valid Docker environment. Please check configuration. Attempted configurations were:
  257. As no valid configuration was found, execution cannot continue.
  258. See https://www.testcontainers.org/on_failure.html for more details.
  259. 2023-05-10T16:14:16.027+05:30 WARN 13040 --- [ main] o.s.w.c.s.GenericWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class [com.example.orderservice.OrderServiceApplication]: Error processing condition on org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$EmbeddedDatabaseConfiguration
  260. 2023-05-10T16:14:16.054+05:30 INFO 13040 --- [ main] .s.b.a.l.ConditionEvaluationReportLogger :
  261. Error starting ApplicationContext. To display the condition evaluation report re-run your application with &#39;debug&#39; enabled.
  262. 2023-05-10T16:14:16.088+05:30 ERROR 13040 --- [ main] o.s.boot.SpringApplication : Application run failed
  263. org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class [com.example.orderservice.OrderServiceApplication]: Error processing condition on org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$EmbeddedDatabaseConfiguration
  264. at org.springframework.context.annotation.ConfigurationClassParser.processImports(ConfigurationClassParser.java:524) ~[spring-context-6.0.9-20230509.130721-27.jar:6.0.9-SNAPSHOT]
  265. at org.springframework.context.annotation.ConfigurationClassParser$DeferredImportSelectorGroupingHandler.lambda$processGroupImports$1(ConfigurationClassParser.java:726) ~[spring-context-6.0.9-20230509.130721-27.jar:6.0.9-SNAPSHOT]
  266. at java.base/java.lang.Iterable.forEach(Iterable.java:75) ~[na:na]
  267. at org.springframework.context.annotation.ConfigurationClassParser$DeferredImportSelectorGroupingHandler.processGroupImports(ConfigurationClassParser.java:723) ~[spring-context-6.0.9-20230509.130721-27.jar:6.0.9-SNAPSHOT]
  268. at org.springframework.context.annotation.ConfigurationClassParser$DeferredImportSelectorHandler.process(ConfigurationClassParser.java:694) ~[spring-context-6.0.9-20230509.130721-27.jar:6.0.9-SNAPSHOT]
  269. at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:182) ~[spring-context-6.0.9-20230509.130721-27.jar:6.0.9-SNAPSHOT]
  270. at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:415) ~[spring-context-6.0.9-20230509.130721-27.jar:6.0.9-SNAPSHOT]
  271. at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:287) ~[spring-context-6.0.9-20230509.130721-27.jar:6.0.9-SNAPSHOT]
  272. at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:344) ~[spring-context-6.0.9-20230509.130721-27.jar:6.0.9-SNAPSHOT]
  273. at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:115) ~[spring-context-6.0.9-20230509.130721-27.jar:6.0.9-SNAPSHOT]
  274. at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:747) ~[spring-context-6.0.9-20230509.130721-27.jar:6.0.9-SNAPSHOT]
  275. at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:565) ~[spring-context-6.0.9-20230509.130721-27.jar:6.0.9-SNAPSHOT]
  276. at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:733) ~[spring-boot-3.1.0-20230509.165129-256.jar:3.1.0-SNAPSHOT]
  277. at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:435) ~[spring-boot-3.1.0-20230509.165129-256.jar:3.1.0-SNAPSHOT]
  278. at org.springframework.boot.SpringApplication.run(SpringApplication.java:311) ~[spring-boot-3.1.0-20230509.165129-256.jar:3.1.0-SNAPSHOT]
  279. at org.springframework.boot.test.context.SpringBootContextLoader.lambda$loadContext$3(SpringBootContextLoader.java:137) ~[spring-boot-test-3.1.0-20230509.165129-256.jar:3.1.0-SNAPSHOT]
  280. at org.springframework.util.function.ThrowingSupplier.get(ThrowingSupplier.java:58) ~[spring-core-6.0.9-20230509.130721-27.jar:6.0.9-SNAPSHOT]
  281. at org.springframework.util.function.ThrowingSupplier.get(ThrowingSupplier.java:46) ~[spring-core-6.0.9-20230509.130721-27.jar:6.0.9-SNAPSHOT]
  282. at org.springframework.boot.SpringApplication.withHook(SpringApplication.java:1405) ~[spring-boot-3.1.0-20230509.165129-256.jar:3.1.0-SNAPSHOT]
  283. at org.springframework.boot.test.context.SpringBootContextLoader$ContextLoaderHook.run(SpringBootContextLoader.java:545) ~[spring-boot-test-3.1.0-20230509.165129-256.jar:3.1.0-SNAPSHOT]
  284. at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:137) ~[spring-boot-test-3.1.0-20230509.165129-256.jar:3.1.0-SNAPSHOT]
  285. at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:108) ~[spring-boot-test-3.1.0-20230509.165129-256.jar:3.1.0-SNAPSHOT]
  286. at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:184) ~[spring-test-6.0.9-20230509.130721-27.jar:6.0.9-SNAPSHOT]
  287. at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:118) ~[spring-test-6.0.9-20230509.130721-27.jar:6.0.9-SNAPSHOT]
  288. at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:127) ~[spring-test-6.0.9-20230509.130721-27.jar:6.0.9-SNAPSHOT]
  289. at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:191) ~[spring-test-6.0.9-20230509.130721-27.jar:6.0.9-SNAPSHOT]
  290. at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:130) ~[spring-test-6.0.9-20230509.130721-27.jar:6.0.9-SNAPSHOT]
  291. at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:241) ~[spring-test-6.0.9-20230509.130721-27.jar:6.0.9-SNAPSHOT]
  292. at org.springframework.test.context.junit.jupiter.SpringExtension.postProcessTestInstance(SpringExtension.java:138) ~[spring-test-6.0.9-20230509.130721-27.jar:6.0.9-SNAPSHOT]
  293. at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeTestInstancePostProcessors$10(ClassBasedTestDescriptor.java:377) ~[junit-jupiter-engine-5.9.3.jar:5.9.3]
  294. at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.executeAndMaskThrowable(ClassBasedTestDescriptor.java:382) ~[junit-jupiter-engine-5.9.3.jar:5.9.3]
  295. at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeTestInstancePostProcessors$11(ClassBasedTestDescriptor.java:377) ~[junit-jupiter-engine-5.9.3.jar:5.9.3]
  296. at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197) ~[na:na]
  297. at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:179) ~[na:na]
  298. at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1625) ~[na:na]
  299. at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509) ~[na:na]
  300. at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499) ~[na:na]
  301. at java.base/java.util.stream.StreamSpliterators$WrappingSpliterator.forEachRemaining(StreamSpliterators.java:310) ~[na:na]
  302. at java.base/java.util.stream.Streams$ConcatSpliterator.forEachRemaining(Streams.java:735) ~[na:na]
  303. at java.base/java.util.stream.Streams$ConcatSpliterator.forEachRemaining(Streams.java:734) ~[na:na]
  304. at java.base/java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:762) ~[na:na]
  305. at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.invokeTestInstancePostProcessors(ClassBasedTestDescriptor.java:376) ~[junit-jupiter-engine-5.9.3.jar:5.9.3]
  306. at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$instantiateAndPostProcessTestInstance$6(ClassBasedTestDescriptor.java:289) ~[junit-jupiter-engine-5.9.3.jar:5.9.3]
  307. at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.9.3.jar:1.9.3]
  308. at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.instantiateAndPostProcessTestInstance(ClassBasedTestDescriptor.java:288) ~[junit-jupiter-engine-5.9.3.jar:5.9.3]
  309. at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$testInstancesProvider$4(ClassBasedTestDescriptor.java:278) ~[junit-jupiter-engine-5.9.3.jar:5.9.3]
  310. at java.base/java.util.Optional.orElseGet(Optional.java:364) ~[na:na]
  311. at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$testInstancesProvider$5(ClassBasedTestDescriptor.java:277) ~[junit-jupiter-engine-5.9.3.jar:5.9.3]
  312. at org.junit.jupiter.engine.execution.TestInstancesProvider.getTestInstances(TestInstancesProvider.java:31) ~[junit-jupiter-engine-5.9.3.jar:5.9.3]
  313. at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$prepare$0(TestMethodTestDescriptor.java:105) ~[junit-jupiter-engine-5.9.3.jar:5.9.3]
  314. at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.9.3.jar:1.9.3]
  315. at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.prepare(TestMethodTestDescriptor.java:104) ~[junit-jupiter-engine-5.9.3.jar:5.9.3]
  316. at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.prepare(TestMethodTestDescriptor.java:68) ~[junit-jupiter-engine-5.9.3.jar:5.9.3]
  317. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$prepare$2(NodeTestTask.java:123) ~[junit-platform-engine-1.9.3.jar:1.9.3]
  318. at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.9.3.jar:1.9.3]
  319. at org.junit.platform.engine.support.hierarchical.NodeTestTask.prepare(NodeTestTask.java:123) ~[junit-platform-engine-1.9.3.jar:1.9.3]
  320. at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:90) ~[junit-platform-engine-1.9.3.jar:1.9.3]
  321. at java.base/java.util.ArrayList.forEach(ArrayList.java:1511) ~[na:na]
  322. at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41) ~[junit-platform-engine-1.9.3.jar:1.9.3]
  323. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155) ~[junit-platform-engine-1.9.3.jar:1.9.3]
  324. at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.9.3.jar:1.9.3]
  325. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141) ~[junit-platform-engine-1.9.3.jar:1.9.3]
  326. at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137) ~[junit-platform-engine-1.9.3.jar:1.9.3]
  327. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139) ~[junit-platform-engine-1.9.3.jar:1.9.3]
  328. at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.9.3.jar:1.9.3]
  329. at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138) ~[junit-platform-engine-1.9.3.jar:1.9.3]
  330. at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95) ~[junit-platform-engine-1.9.3.jar:1.9.3]
  331. at java.base/java.util.ArrayList.forEach(ArrayList.java:1511) ~[na:na]
  332. at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41) ~[junit-platform-engine-1.9.3.jar:1.9.3]
  333. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155) ~[junit-platform-engine-1.9.3.jar:1.9.3]
  334. at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.9.3.jar:1.9.3]
  335. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141) ~[junit-platform-engine-1.9.3.jar:1.9.3]
  336. at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137) ~[junit-platform-engine-1.9.3.jar:1.9.3]
  337. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139) ~[junit-platform-engine-1.9.3.jar:1.9.3]
  338. at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.9.3.jar:1.9.3]
  339. at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138) ~[junit-platform-engine-1.9.3.jar:1.9.3]
  340. at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95) ~[junit-platform-engine-1.9.3.jar:1.9.3]
  341. at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:35) ~[junit-platform-engine-1.9.3.jar:1.9.3]
  342. at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57) ~[junit-platform-engine-1.9.3.jar:1.9.3]
  343. at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:54) ~[junit-platform-engine-1.9.3.jar:1.9.3]
  344. at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:147) ~[junit-platform-launcher-1.9.3.jar:1.9.3]
  345. at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:127) ~[junit-platform-launcher-1.9.3.jar:1.9.3]
  346. at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:90) ~[junit-platform-launcher-1.9.3.jar:1.9.3]
  347. at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:55) ~[junit-platform-launcher-1.9.3.jar:1.9.3]
  348. at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:102) ~[junit-platform-launcher-1.9.3.jar:1.9.3]
  349. at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:54) ~[junit-platform-launcher-1.9.3.jar:1.9.3]
  350. at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:114) ~[junit-platform-launcher-1.9.3.jar:1.9.3]
  351. at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:86) ~[junit-platform-launcher-1.9.3.jar:1.9.3]
  352. at org.junit.platform.launcher.core.DefaultLauncherSession$DelegatingLauncher.execute(DefaultLauncherSession.java:86) ~[junit-platform-launcher-1.9.3.jar:1.9.3]
  353. at org.junit.platform.launcher.core.SessionPerRequestLauncher.execute(SessionPerRequestLauncher.java:53) ~[junit-platform-launcher-1.9.3.jar:1.9.3]
  354. at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:69) ~[junit5-rt.jar:na]
  355. at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) ~[junit-rt.jar:na]
  356. at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) ~[junit-rt.jar:na]
  357. at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) ~[junit-rt.jar:na]
  358. Caused by: java.lang.IllegalStateException: Error processing condition on org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$EmbeddedDatabaseConfiguration
  359. at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:60) ~[spring-boot-autoconfigure-3.1.0-20230509.165129-256.jar:3.1.0-SNAPSHOT]
  360. at org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:108) ~[spring-context-6.0.9-20230509.130721-27.jar:6.0.9-SNAPSHOT]
  361. at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:219) ~[spring-context-6.0.9-20230509.130721-27.jar:6.0.9-SNAPSHOT]
  362. at org.springframework.context.annotation.ConfigurationClassParser.processMemberClasses(ConfigurationClassParser.java:365) ~[spring-context-6.0.9-20230509.130721-27.jar:6.0.9-SNAPSHOT]
  363. at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:265) ~[spring-context-6.0.9-20230509.130721-27.jar:6.0.9-SNAPSHOT]
  364. at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:243) ~[spring-context-6.0.9-20230509.130721-27.jar:6.0.9-SNAPSHOT]
  365. at org.springframework.context.annotation.ConfigurationClassParser.processImports(ConfigurationClassParser.java:514) ~[spring-context-6.0.9-20230509.130721-27.jar:6.0.9-SNAPSHOT]
  366. ... 93 common frames omitted
  367. Caused by: java.lang.IllegalStateException: Previous attempts to find a Docker environment failed. Will not retry. Please see logs and check configuration
  368. at org.testcontainers.dockerclient.DockerClientProviderStrategy.getFirstValidStrategy(DockerClientProviderStrategy.java:212) ~[testcontainers-1.18.0.jar:1.18.0]
  369. at org.testcontainers.DockerClientFactory.getOrInitializeStrategy(DockerClientFactory.java:150) ~[testcontainers-1.18.0.jar:1.18.0]
  370. at org.testcontainers.DockerClientFactory.dockerHostIpAddress(DockerClientFactory.java:323) ~[testcontainers-1.18.0.jar:1.18.0]
  371. at org.testcontainers.containers.ContainerState.getHost(ContainerState.java:64) ~[testcontainers-1.18.0.jar:na]
  372. at org.testcontainers.containers.MySQLContainer.getJdbcUrl(MySQLContainer.java:104) ~[mysql-1.18.0.jar:na]
  373. at com.example.orderservice.OrderServiceApplicationTests.lambda$setProperties$0(OrderServiceApplicationTests.java:46) ~[test-classes/:na]
  374. at org.springframework.test.context.support.DynamicValuesPropertySource.getProperty(DynamicValuesPropertySource.java:43) ~[spring-test-6.0.9-20230509.130721-27.jar:6.0.9-SNAPSHOT]
  375. at org.springframework.core.env.PropertySourcesPropertyResolver.getProperty(PropertySourcesPropertyResolver.java:85) ~[spring-core-6.0.9-20230509.130721-27.jar:6.0.9-SNAPSHOT]
  376. at org.springframework.boot.context.properties.source.ConfigurationPropertySourcesPropertyResolver$DefaultResolver.getProperty(ConfigurationPropertySourcesPropertyResolver.java:123) ~[spring-boot-3.1.0-20230509.165129-256.jar:3.1.0-SNAPSHOT]
  377. at org.springframework.boot.context.properties.source.ConfigurationPropertySourcesPropertyResolver.findPropertyValue(ConfigurationPropertySourcesPropertyResolver.java:97) ~[spring-boot-3.1.0-20230509.165129-256.jar:3.1.0-SNAPSHOT]
  378. at org.springframework.boot.context.properties.source.ConfigurationPropertySourcesPropertyResolver.getProperty(ConfigurationPropertySourcesPropertyResolver.java:74) ~[spring-boot-3.1.0-20230509.165129-256.jar:3.1.0-SNAPSHOT]
  379. at org.springframework.boot.context.properties.source.ConfigurationPropertySourcesPropertyResolver.getProperty(ConfigurationPropertySourcesPropertyResolver.java:60) ~[spring-boot-3.1.0-20230509.165129-256.jar:3.1.0-SNAPSHOT]
  380. at org.springframework.core.env.AbstractEnvironment.getProperty(AbstractEnvironment.java:552) ~[spring-core-6.0.9-20230509.130721-27.jar:6.0.9-SNAPSHOT]
  381. at org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$EmbeddedDatabaseCondition.hasDataSourceUrlProperty(DataSourceAutoConfiguration.java:156) ~[spring-boot-autoconfigure-3.1.0-20230509.165129-256.jar:3.1.0-SNAPSHOT]
  382. at org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$EmbeddedDatabaseCondition.getMatchOutcome(DataSourceAutoConfiguration.java:139) ~[spring-boot-autoconfigure-3.1.0-20230509.165129-256.jar:3.1.0-SNAPSHOT]
  383. at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:47) ~[spring-boot-autoconfigure-3.1.0-20230509.165129-256.jar:3.1.0-SNAPSHOT]
  384. ... 99 common frames omitted
  385. I highly suspect that this is related to injecting Mysql image into the local environment.
  386. Note : I did the same code to Mongodb and it works fine.
  387. Could anyone had the same issue and resolved ? btw unit testing becoming embarrassing :-(
  388. </details>
  389. # 答案1
  390. **得分**: 1
  391. 我认为你的测试类缺少 `@Testcontainers`。
  392. <details>
  393. <summary>英文:</summary>
  394. I think you are missing `@Testcontainers` over your test class.
  395. </details>
  396. # 答案2
  397. **得分**: 0
  398. 请在类级别添加`@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)`。
  399. <details>
  400. <summary>英文:</summary>
  401. Add `@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)` at class level
  402. </details>
  403. # 答案3
  404. **得分**: 0
  405. 尝试删除 @Container 注释,并将 mySQLContainer.start() 添加到这个方法中。
  406. ```java
  407. @DynamicPropertySource
  408. static void setProperties(DynamicPropertyRegistry properties){
  409. mySQLContainer.start();
  410. properties.add("spring.datasource.url", mySQLContainer::getJdbcUrl);
  411. properties.add("spring.datasource.username", mySQLContainer::getUsername);
  412. properties.add("spring.datasource.password", mySQLContainer::getPassword);
  413. }
英文:

Try to remove @Container annotation and add mySQLContainer.start() to this method.

  1. @DynamicPropertySource
  2. static void setProperties(DynamicPropertyRegistry properties){
  3. mySQLContainer.start();
  4. properties.add(&quot;spring.datasource.url&quot;, mySQLContainer::getJdbcUrl);
  5. properties.add(&quot;spring.datasource.username&quot;, mySQLContainer::getUsername);
  6. properties.add(&quot;spring.datasource.password&quot;, mySQLContainer::getPassword);
  7. }

答案4

得分: 0

根据您提供的堆栈最后一部分,您未提供在PATH上运行并可用的有效Docker安装。

我建议您通过官方方式确保Docker Desktop的安装:https://docs.docker.com/engine/install/

之后,并且如前面所述使用@Testcontainers应该有助于您在测试过程中进一步。

英文:

Based on the final portion of the stack you are not providing a valid Docker installation running and available at your PATH.

I would recommend you to ensure the Docker Desktop installation via official ways at: https://docs.docker.com/engine/install/

After that and using the @Testcontainers as stated before should help you further in the testing process.

答案5

得分: 0

以下是已翻译的内容:

为了进行测试,请尝试在测试类上添加 @SpringBootTest@Testcontainers

但就个人而言,我认为您可以更改您的测试策略,以使测试更加有效。

  • 测试与数据库相关的功能,如 Repository、XXXTemplate,可以使用 @DataJdbcTest 或其他 DataXXXTest 与 testcontainers 数据库进行测试。
  • 模拟 Repository 并使用 @WebMvcTest@WebFluxTest 等针对 Controller/API 进行测试。
  • 使用最新的 Spring WebClient 在集成环境中使用 @SpringBootTest(....RAMDON_PORT) 验证功能。

请查看我的 Spring Boot 示例项目,其中包含测试代码,链接在此处,https://github.com/hantsy/spring6-sandbox/tree/master/boot/src/test/java/com/example/demo

英文:

For your test try to add @SpringBootTest and @Testcontainers on the test class.

But personally I think you could change your testing strategy to make tests more effectively.

  • Test the db related features, such as Repository, XXXTemplate with @DataJdbcTest or other DataXXXTest with a testcontainers database.
  • Mock the Repository and test against the Controller/APIs with @WebMvcTest or @WebFluxTest, etc
  • Use the latest Spring WebClient to verify the functionality in an integration env with @SpringBootTest(....RAMDON_PORT)

Check my Spring Boot example project with testing codes here, https://github.com/hantsy/spring6-sandbox/tree/master/boot/src/test/java/com/example/demo

huangapple
  • 本文由 发表于 2023年5月10日 19:43:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76217978.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定