spring 使用mockito 5.1配置基于XML的bean

yhxst69z  于 2024-01-05  发布在  Spring
关注(0)|答案(1)|浏览(162)

我想在Spring项目中更新Mockito,但在Mockito 5.1.0或更高版本中出现了一些错误。
看起来Mockito用varargs添加了新的工厂方法,而我的基于XML的配置没有选择旧的方法。
我有两个问题。
1.在这种情况下(详见下文),为什么Spring选择另一种方法?
1.我如何告诉Spring调用重载的预期方法?
下面是一个示例代码和配置。(完整:https://github.com/hmmnrst/spring-experiments/tree/mockito-bean

  1. @ExtendWith(SpringExtension.class)
  2. @ContextConfiguration(locations = "classpath:testApplicationContext.xml")
  3. public class MainTest {
  4. @Autowired
  5. ApiClient apiClient;
  6. @Test
  7. public void test() {
  8. assertThat(apiClient).isNotNull();
  9. System.out.println(apiClient);
  10. }
  11. }

个字符
当我使用Mockito 5.0.0时,此测试通过。当我使用Mockito 5.1.0时,此测试失败,但有以下例外。

  1. Jul 10, 2023 9:02:27 PM org.springframework.context.support.AbstractApplicationContext refresh
  2. WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'apiClient' defined in class path resource [testApplicationContext.xml]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.lang.Object]: Factory method 'mock' threw exception; nested exception is java.lang.IllegalArgumentException: Please don't pass any values here. Java will detect class automagically.
  3. Jul 10, 2023 9:02:27 PM org.springframework.test.context.TestContextManager prepareTestInstance
  4. SEVERE: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@2de23121] to prepare test instance [org.example.MainTest@1d207fad]
  5. java.lang.IllegalStateException: Failed to load ApplicationContext
  6. at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:98)
  7. at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:124)
  8. at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118)
  9. at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
  10. at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:248)
  11. at org.springframework.test.context.junit.jupiter.SpringExtension.postProcessTestInstance(SpringExtension.java:138)
  12. at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeTestInstancePostProcessors$10(ClassBasedTestDescriptor.java:377)
  13. at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.executeAndMaskThrowable(ClassBasedTestDescriptor.java:382)
  14. at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeTestInstancePostProcessors$11(ClassBasedTestDescriptor.java:377)
  15. at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197)
  16. at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:179)
  17. at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1625)
  18. at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509)
  19. at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
  20. at java.base/java.util.stream.StreamSpliterators$WrappingSpliterator.forEachRemaining(StreamSpliterators.java:310)
  21. at java.base/java.util.stream.Streams$ConcatSpliterator.forEachRemaining(Streams.java:735)
  22. at java.base/java.util.stream.Streams$ConcatSpliterator.forEachRemaining(Streams.java:734)
  23. at java.base/java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:762)
  24. at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.invokeTestInstancePostProcessors(ClassBasedTestDescriptor.java:376)
  25. at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$instantiateAndPostProcessTestInstance$6(ClassBasedTestDescriptor.java:289)
  26. at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
  27. at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.instantiateAndPostProcessTestInstance(ClassBasedTestDescriptor.java:288)
  28. at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$testInstancesProvider$4(ClassBasedTestDescriptor.java:278)
  29. at java.base/java.util.Optional.orElseGet(Optional.java:364)
  30. at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$testInstancesProvider$5(ClassBasedTestDescriptor.java:277)
  31. at org.junit.jupiter.engine.execution.TestInstancesProvider.getTestInstances(TestInstancesProvider.java:31)
  32. at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$prepare$0(TestMethodTestDescriptor.java:105)
  33. at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
  34. at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.prepare(TestMethodTestDescriptor.java:104)
  35. at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.prepare(TestMethodTestDescriptor.java:68)
  36. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$prepare$2(NodeTestTask.java:123)
  37. at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
  38. at org.junit.platform.engine.support.hierarchical.NodeTestTask.prepare(NodeTestTask.java:123)
  39. at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:90)
  40. at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
  41. at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
  42. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
  43. at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
  44. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
  45. at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
  46. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
  47. at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
  48. at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
  49. at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
  50. at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
  51. at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
  52. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
  53. at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
  54. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
  55. at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
  56. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
  57. at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
  58. at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
  59. at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
  60. at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:35)
  61. at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
  62. at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:54)
  63. at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:147)
  64. at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:127)
  65. at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:90)
  66. at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:55)
  67. at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:102)
  68. at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:54)
  69. at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:114)
  70. at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:86)
  71. at org.junit.platform.launcher.core.DefaultLauncherSession$DelegatingLauncher.execute(DefaultLauncherSession.java:86)
  72. at org.apache.maven.surefire.junitplatform.LazyLauncher.execute(LazyLauncher.java:50)
  73. at org.apache.maven.surefire.junitplatform.JUnitPlatformProvider.execute(JUnitPlatformProvider.java:184)
  74. at org.apache.maven.surefire.junitplatform.JUnitPlatformProvider.invokeAllTests(JUnitPlatformProvider.java:148)
  75. at org.apache.maven.surefire.junitplatform.JUnitPlatformProvider.invoke(JUnitPlatformProvider.java:122)
  76. at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:385)
  77. at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:162)
  78. at org.apache.maven.surefire.booter.ForkedBooter.run(ForkedBooter.java:507)
  79. at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:495)
  80. Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'apiClient' defined in class path resource [testApplicationContext.xml]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.lang.Object]: Factory method 'mock' threw exception; nested exception is java.lang.IllegalArgumentException: Please don't pass any values here. Java will detect class automagically.
  81. at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:658)
  82. at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:638)
  83. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1352)
  84. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1195)
  85. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582)
  86. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
  87. at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
  88. at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
  89. at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
  90. at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
  91. at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:955)
  92. at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:920)
  93. at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
  94. at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:127)
  95. at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)
  96. at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:276)
  97. at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:244)
  98. at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:141)
  99. at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:90)
  100. ... 73 more
  101. Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.lang.Object]: Factory method 'mock' threw exception; nested exception is java.lang.IllegalArgumentException: Please don't pass any values here. Java will detect class automagically.
  102. at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
  103. at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653)
  104. ... 91 more
  105. Caused by: java.lang.IllegalArgumentException: Please don't pass any values here. Java will detect class automagically.
  106. at org.mockito.Mockito.mock(Mockito.java:1986)
  107. at org.mockito.Mockito.mock(Mockito.java:1940)
  108. at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  109. at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
  110. at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  111. at java.base/java.lang.reflect.Method.invoke(Method.java:568)
  112. at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
  113. ... 92 more

9cbw7uwe

9cbw7uwe1#

我在从Mockito 4.8.1升级到Mockito 5.7.0时遇到了同样的问题
要修复它,您必须将type=java.lang.Class属性添加到constructor-arg标记:

  1. <bean id="apiClient" class="org.mockito.Mockito" factory-method="mock">
  2. <constructor-arg type="java.lang.Class" value="org.example.ApiClient" />
  3. </bean>

字符串
希望这对你有帮助
问候

相关问题