当前位置:网站首页>Strange things in unit testing

Strange things in unit testing

2022-06-09 08:57: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(" To configure 1");
    vo.setValue("111");
    vo.setVisible(true);
    if (configService.createConfig(vo)>0){
    
      ConfigDO v= configService.getConfigByKey("key1");
      System.out.println(v);
    }   
  }
}

Test a unit , A very normal section of test code , Above . Error when loading , It seems that the data source failed to load

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="")}

In fact, the data source configuration is correct .
This is quoted later MockBean The example is OK 了 , And this example is just mq Notification component , Independent of data source . It's not that the tester feels confused .

@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(" To configure 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://yzsam.com/2022/160/202206090845309354.html