单元测试:测试代码启动报错

Blade 未结 1 1402

一、该问题的重现步骤是什么?

  1. 在框架“test”目录下新建测试代码文件,详情如下:

    @RunWith(SpringJUnit4ClassRunner.class)
    @SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    public class RoleTest {
     @Test
     public void maina() {

         System.out.println("用户模块单元测试启动...\n\n");


  2. 运行待测试方法

    image.png



二、你期待的结果是什么?实际看到的又是什么?

启动成功,并正常console输出“用户模块单元测试启动”日志。

三、你正在使用的是什么产品,什么版本?在什么操作系统上?

产品:baldex-boot ;版本:2.6.0.RELEASE ;操作系统:Windows10 专业版 64bit

四、请提供详细的错误堆栈信息,这很重要。

具体的报错信息:
java.lang.IllegalStateException: Failed to load ApplicationContext

 

         at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125)

         at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:108)

         at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118)

         at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)

         at org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener.prepareTestInstance(SpringBootDependencyInjectionTestExecutionListener.java:43)

         at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:246)

         at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227)

         at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)

         at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)

         at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)

         at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246)

         at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)

         at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)

         at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)

         at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)

         at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)

         at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)

         at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)

         at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)

         at org.junit.runners.ParentRunner.run(ParentRunner.java:363)

         at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)

         at org.junit.runner.JUnitCore.run(JUnitCore.java:137)

         at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)

         at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)

         at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)

         at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

Caused by: org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 'redisCacheManager' defined in class path resource [org/springblade/core/redis/config/BladeRedisCacheAutoConfiguration.class]: Cannot register bean definition [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=true; factoryBeanName=org.springblade.core.redis.config.BladeRedisCacheAutoConfiguration; factoryMethodName=redisCacheManager; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springblade/core/redis/config/BladeRedisCacheAutoConfiguration.class]] for bean 'redisCacheManager': There is already [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springblade.core.jwt.config.JwtRedisConfiguration; factoryMethodName=cacheManager; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springblade/core/jwt/config/JwtRedisConfiguration.class]] bound.

         at org.springframework.beans.factory.support.DefaultListableBeanFactory.registerBeanDefinition(DefaultListableBeanFactory.java:911)

         at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForBeanMethod(ConfigurationClassBeanDefinitionReader.java:273)

         at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.java:141)

         at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(ConfigurationClassBeanDefinitionReader.java:117)

         at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:327)

         at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:232)

         at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:275)

         at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:95)

         at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:706)

         at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:532)

         at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141)

         at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:744)

         at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:391)

         at org.springframework.boot.SpringApplication.run(SpringApplication.java:312)

         at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:121)

         at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)

         at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:117)

         ... 25 more

五、若有更多详细信息,请在下面提供。

推测:org.springblade.core.redis.config.BladeRedisCacheAutoConfigurationorg.springblade.core.jwt.config.JwtRedisConfiguration文件下同名bean redisCacheManager一个@Primary@ConditionalOnMissingBean冲突

1条回答
  • 2020-12-16 16:53

    看一下文档对应章节,需要用到专用的@BladeBootTest注解

    image.png

    0 讨论(0)
提交回复