ClassCastException: org.springframework.beans.factory.support.NullBean cannot be cast to org.springframework.plugin.core.Plugin

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

ClassCastException: org.springframework.beans.factory.support.NullBean cannot be cast to org.springframework.plugin.core.Plugin

问题

以下是翻译的内容:

我在我的应用程序中有这个测试:

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @WebAppConfiguration
  3. @ContextConfiguration(classes = ApplicationTestConfig.class)
  4. @MockBeans({ @MockBean(SwaggerConfig.class)})
  5. public class AuthorisationControllerTest implements WebMvcConfigurer {
  6. private MockMvc mockMvc;
  7. @Autowired
  8. private WebApplicationContext wac;
  9. @Before
  10. public void setup() {
  11. this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
  12. }
  13. @Test
  14. public void testAuthorisation() throws Exception {
  15. this.mockMvc.perform(get("/test")).andDo(print()).andExpect(status().isOk())
  16. .andExpect(content().string(containsString("Hello, World")));
  17. }
  18. }

但是当我运行测试时,我得到了以下错误:

  1. 15:04:10.417 [main] ERROR org.springframework.test.context.TestContextManager - 在准备测试实例 [eu.europa.ec.oib.kw.guards.controller.AuthorisationControllerTest@6fb219dd] 时,捕获异常的 TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener@7219ec67] 抛出异常。
  2. java.lang.IllegalStateException: 无法加载应用上下文
  3. at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:132)
  4. at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:123)
  5. at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:190)
  6. at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:132)
  7. at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:244)
  8. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227)
  9. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
  10. at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
  11. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
  12. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246)
  13. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
  14. at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
  15. at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
  16. at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
  17. at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
  18. at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
  19. at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
  20. at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
  21. at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
  22. at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
  23. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
  24. at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:365)
  25. at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:273)
  26. at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:238)
  27. at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:159)
  28. at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:384)
  29. at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:345)
  30. at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:126)
  31. at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:418)
  32. Caused by: org.springframework.context.ApplicationContextException: 无法启动 bean 'documentationPluginsBootstrapper';嵌套异常是 java.lang.ClassCastException: org.springframework.beans.factory.support.NullBean 无法转换为 org.springframework.plugin.core.Plugin
  33. at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:185)
  34. at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:53)
  35. at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:360)
  36. at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:158)
  37. at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:122)
  38. at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:895)
  39. at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:554)
  40. at org.springframework.test.context.web.AbstractGenericWebContextLoader.loadContext(AbstractGenericWebContextLoader.java:129)
  41. at org.springframework.test.context.web.AbstractGenericWebContextLoader.loadContext(AbstractGenericWebContextLoader.java:61)
  42. at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:275)
  43. at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:243)
  44. at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)
  45. at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
  46. ... 28 more
  47. Caused by: java.lang.ClassCastException: org.springframework.beans.factory.support.NullBean 无法转换为 org.springframework.plugin.core.Plugin
  48. at org.springframework.plugin.core.PluginRegistrySupport.initialize(PluginRegistrySupport.java:78)
  49. at org.springframework.plugin.core.OrderAwarePluginRegistry.initialize(OrderAwarePluginRegistry.java:132)
  50. at org.springframework.plugin.core.PluginRegistrySupport.getPlugins(PluginRegistrySupport.java:59)
  51. at org.springframework.plugin.core.SimplePluginRegistry.getPlugins(SimplePluginRegistry.java:68)
  52. at springfox.documentation.spring.web.plugins.DocumentationPluginsManager.documentationPlugins(DocumentationPluginsManager.java:96)
  53. at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.start(DocumentationPluginsBootstrapper.java:162)
  54. at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:182)
  55. ... 40 more
  56. 15:04:10.436 [main] DEBUG org.springframework.test.context.support.AbstractDirtiesContextTestExecutionListener - 测试类后:context [DefaultTestContext@769e7ee8 testClass = AuthorisationControllerTest, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@5276e6b0 testClass = AuthorisationControllerTest, locations = '{}', classes = '{
  57. <details>
  58. <summary>英文:</summary>
  59. I have this test in my app:
  60. @RunWith(SpringJUnit4ClassRunner.class)
  61. @WebAppConfiguration
  62. @ContextConfiguration(classes = ApplicationTestConfig.class)
  63. @MockBeans({ @MockBean(SwaggerConfig.class)})
  64. public class AuthorisationControllerTest implements WebMvcConfigurer {
  65. private MockMvc mockMvc;
  66. @Autowired
  67. private WebApplicationContext wac;
  68. @Before
  69. public void setup() {
  70. this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
  71. }
  72. @Test
  73. public void testAuthorisation() throws Exception {
  74. this.mockMvc.perform(get(&quot;/test&quot;)).andDo(print()).andExpect(status().isOk())
  75. .andExpect(content().string(containsString(&quot;Hello, World&quot;)));
  76. }
  77. }
  78. but when I run the test I got this error:
  79. 15:04:10.417 [main] ERROR org.springframework.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener@7219ec67] to prepare test instance [eu.europa.ec.oib.kw.guards.controller.AuthorisationControllerTest@6fb219dd]
  80. java.lang.IllegalStateException: Failed to load ApplicationContext
  81. at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:132)
  82. at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:123)
  83. at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:190)
  84. at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:132)
  85. at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:244)
  86. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227)
  87. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
  88. at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
  89. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
  90. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246)
  91. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
  92. at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
  93. at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
  94. at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
  95. at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
  96. at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
  97. at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
  98. at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
  99. at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
  100. at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
  101. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
  102. at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:365)
  103. at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:273)
  104. at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:238)
  105. at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:159)
  106. at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:384)
  107. at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:345)
  108. at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:126)
  109. at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:418)
  110. Caused by: org.springframework.context.ApplicationContextException: Failed to start bean &#39;documentationPluginsBootstrapper&#39;; nested exception is java.lang.ClassCastException: org.springframework.beans.factory.support.NullBean cannot be cast to org.springframework.plugin.core.Plugin
  111. at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:185)
  112. at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:53)
  113. at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:360)
  114. at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:158)
  115. at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:122)
  116. at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:895)
  117. at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:554)
  118. at org.springframework.test.context.web.AbstractGenericWebContextLoader.loadContext(AbstractGenericWebContextLoader.java:129)
  119. at org.springframework.test.context.web.AbstractGenericWebContextLoader.loadContext(AbstractGenericWebContextLoader.java:61)
  120. at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:275)
  121. at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:243)
  122. at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)
  123. at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
  124. ... 28 common frames omitted
  125. Caused by: java.lang.ClassCastException: org.springframework.beans.factory.support.NullBean cannot be cast to org.springframework.plugin.core.Plugin
  126. at org.springframework.plugin.core.PluginRegistrySupport.initialize(PluginRegistrySupport.java:78)
  127. at org.springframework.plugin.core.OrderAwarePluginRegistry.initialize(OrderAwarePluginRegistry.java:132)
  128. at org.springframework.plugin.core.PluginRegistrySupport.getPlugins(PluginRegistrySupport.java:59)
  129. at org.springframework.plugin.core.SimplePluginRegistry.getPlugins(SimplePluginRegistry.java:68)
  130. at springfox.documentation.spring.web.plugins.DocumentationPluginsManager.documentationPlugins(DocumentationPluginsManager.java:96)
  131. at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.start(DocumentationPluginsBootstrapper.java:162)
  132. at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:182)
  133. ... 40 common frames omitted
  134. 15:04:10.436 [main] DEBUG org.springframework.test.context.support.AbstractDirtiesContextTestExecutionListener - After test class: context [DefaultTestContext@769e7ee8 testClass = AuthorisationControllerTest, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@5276e6b0 testClass = AuthorisationControllerTest, locations = &#39;{}&#39;, classes = &#39;{class com.bonanza.ApplicationTestConfig}&#39;, contextInitializerClasses = &#39;[]&#39;, activeProfiles = &#39;{}&#39;, propertySourceLocations = &#39;{}&#39;, propertySourceProperties = &#39;{}&#39;, contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@491666ad, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@51931956, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@82791545, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@240237d2, org.springframework.test.context.web.socket.MockServerContainerContextCustomizer@212bf671], resourceBasePath = &#39;src/main/webapp&#39;, contextLoader = &#39;org.springframework.test.context.web.WebDelegatingSmartContextLoader&#39;, parent = [null]], attributes = map[[empty]]], class annotated with @DirtiesContext [false] with mode [null].
  135. [ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 10.803 s &lt;&lt;&lt; FAILURE! - in com.bonanza..controller.AuthorisationControllerTest
  136. [ERROR] testAuthorisation(om.bonanza.controller.AuthorisationControllerTest) Time elapsed: 0.049 s &lt;&lt;&lt; ERROR!
  137. java.lang.IllegalStateException: Failed to load ApplicationContext
  138. Caused by: org.springframework.context.ApplicationContextException: Failed to start bean &#39;documentationPluginsBootstrapper&#39;; nested exception is java.lang.ClassCastException: org.springframework.beans.factory.support.NullBean cannot be cast to org.springframework.plugin.core.Plugin
  139. Caused by: java.lang.ClassCastException: org.springframework.beans.factory.support.NullBean cannot be cast to org.springframework.plugin.core.Plugin
  140. [INFO]
  141. </details>
  142. # 答案1
  143. **得分**: 0
  144. 可能您的问题可能与以下代码行有关:
  145. ```java
  146. @MockBeans({ @MockBean(SwaggerConfig.class)})

我认为 Spring 正在尝试模拟您的 Swagger 配置,并且在解析依赖关系时,框架提供了一个 NullBean 引用,而不是所需的对象,即在这种情况下是 org.springframework.plugin.core.Plugin 的实例。

我假设您需要在测试中禁用 Swagger 支持。

如果是这种情况,有一种更简单的方法可以进行操作。

思路是结合 Spring 配置文件。

首先,在您的 Swagger 类配置中定义一个 @Profile 注解,如下:

  1. @Profile("swagger")
  2. @Configuration
  3. @EnableSwagger2
  4. public class SwaggerConfig {
  5. //...
  6. }

这个配置文件应该包含在您的正常应用操作中。如果需要进一步的信息,请告诉我。

然后,在您的测试中,使用 @ActiveProfiles 注解选择要启用的配置文件,例如:

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @WebAppConfiguration
  3. @ContextConfiguration(classes = ApplicationTestConfig.class)
  4. @ActiveProfiles("test")
  5. public class AuthorisationControllerTest implements WebMvcConfigurer {

您可以像示例中那样定义一个名为 test 的单个配置文件,其中包含一组配置文件,或者直接包含适用的配置文件列表,只要确保不包含与 Swagger 配置相关联的那个,例如示例中的 swagger

或者,如果它不会干扰您的其他配置,您可以使用以下配置实现类似的结果:

  1. @Profile("!no-swagger")
  2. @Configuration
  3. @EnableSwagger2
  4. public class SwaggerConfig {
  5. //...
  6. }

以及您的测试:

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @WebAppConfiguration
  3. @ContextConfiguration(classes = ApplicationTestConfig.class)
  4. @ActiveProfiles("no-swagger")
  5. public class AuthorisationControllerTest implements WebMvcConfigurer {
英文:

Probably your problem could be related with the following line:

  1. @MockBeans({ @MockBean(SwaggerConfig.class)})

I think Spring is trying to mock your Swagger configuration and, when resolving dependencies, the framework is providing a NullBean reference instead of the required object, an instance of org.springframework.plugin.core.Plugin in this case.

I assume that you need to disable Swagger support in your tests.

If that is the case, there is a simpler way to proceed.

The idea is combine Spring profiles.

First, define a @Profile annotation in your Swagger class configuration, like:

  1. @Profile(&quot;swagger&quot;)
  2. @Configuration
  3. @EnableSwagger2
  4. public class SwaggerConfig {
  5. //...
  6. }

This profile should be included in your normal application operation. Please, let me know if you need further information on this.

Then, in your test, select the profiles you want to enable with the @ActiveProfiles annotation, for instance:

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @WebAppConfiguration
  3. @ContextConfiguration(classes = ApplicationTestConfig.class)
  4. @ActiveProfiles(&quot;test&quot;)
  5. public class AuthorisationControllerTest implements WebMvcConfigurer {

You can define, as in the example, a single profile of name test that includes a list of profiles, or directly include the list of profiles applicable, just be sure to not include the one associated with the Swagger configuration, swagger in the example.

Or, if it does not interfere with other of your configurations, you can achieve a similar result with the following configuration:

  1. @Profile(&quot;!no-swagger&quot;)
  2. @Configuration
  3. @EnableSwagger2
  4. public class SwaggerConfig {
  5. //...
  6. }

And your test:

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @WebAppConfiguration
  3. @ContextConfiguration(classes = ApplicationTestConfig.class)
  4. @ActiveProfiles(&quot;no-swagger&quot;)
  5. public class AuthorisationControllerTest implements WebMvcConfigurer {

huangapple
  • 本文由 发表于 2020年10月23日 21:09:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/64500674.html
匿名

发表评论

匿名网友

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

确定