当前位置:网站首页>单元测试中遇到的怪事

单元测试中遇到的怪事

2022-06-09 08:45:00 OK_boom

@Import(ConfigServiceImpl.class)
public class ConfigServiceTest extends BaseDbUnitTest {
    
  @Resource
  private ConfigServiceImpl configService;
   @Test
  public void testConfig(){
    
    ConfigCreateReqVO vo=new ConfigCreateReqVO();
    vo.setCategory("cust");
    vo.setKey("key1");
    vo.setName("配置1");
    vo.setValue("111");
    vo.setVisible(true);
    if (configService.createConfig(vo)>0){
    
      ConfigDO v= configService.getConfigByKey("key1");
      System.out.println(v);
    }   
  }
}

针对某个单元进行测试,很正常的一段测试代码,如上。加载的时候就出错,貌似数据源加载失败

Positive matches:
-----------------

   DataSourceAutoConfiguration matched:
      - @ConditionalOnClass found required classes 'javax.sql.DataSource', 'org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType' (OnClassCondition)
      - @ConditionalOnMissingBean (types: io.r2dbc.spi.ConnectionFactory; SearchStrategy: all) did not find any beans (OnBeanCondition)

   DataSourceAutoConfiguration.PooledDataSourceConfiguration matched:
      - AnyNestedCondition 1 matched 1 did not; NestedCondition on DataSourceAutoConfiguration.PooledDataSourceCondition.PooledDataSourceAvailable PooledDataSource found supported DataSource; NestedCondition on DataSourceAutoConfiguration.PooledDataSourceCondition.ExplicitType @ConditionalOnProperty (spring.datasource.type) did not find property 'type' (DataSourceAutoConfiguration.PooledDataSourceCondition)
      - @ConditionalOnMissingBean (types: javax.sql.DataSource,javax.sql.XADataSource; SearchStrategy: all) did not find any beans (OnBeanCondition)

   DataSourceConfiguration.Hikari matched:
...
org.springf
ramework.beans.factory.BeanCreationException: Error creating bean with name 'com.freestyle.yudao.module.custom.script.service.script.ConfigServiceTest': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cn.iocoder.yudao.module.infra.service.config.ConfigServiceImpl': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'cn.iocoder.yudao.module.infra.mq.producer.config.ConfigProducer' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {
    @javax.annotation.Resource(shareable=true, lookup="", name="", description="", authenticationType=CONTAINER, type=java.lang.Object.class, mappedName="")}

而事实上数据源配置都是正确的。
后面引用了这个MockBean实例就OK了,而这个实例只是个mq通知组件,跟数据源无关。不是测试仔感觉一脸懵逼。

@Import(ConfigServiceImpl.class)
public class ConfigServiceTest extends BaseDbUnitTest {
    
  @Resource
  private ConfigServiceImpl configService;
  @MockBean
  private ConfigProducer configProducer;
  @Test
  public void testConfig(){
    
    ConfigCreateReqVO vo=new ConfigCreateReqVO();
    vo.setCategory("cust");
    vo.setKey("key1");
    vo.setName("配置1");
    vo.setValue("111");
    vo.setVisible(true);
    if (configService.createConfig(vo)>0){
    
      ConfigDO v= configService.getConfigByKey("key1");
      System.out.println(v);
    }
   verify(configProducer, times(1)).sendConfigRefreshMessage();
  }
}
原网站

版权声明
本文为[OK_boom]所创,转载请带上原文链接,感谢
https://rocklee.blog.csdn.net/article/details/125095940