【问题标题】:SpringBoot disabling DataSourceAutoconfigure error while running Junit TestsSpringBoot 在运行 Junit 测试时禁用 DataSourceAutoconfigure 错误
【发布时间】:2020-10-17 22:17:01
【问题描述】:

我的 Springboot 应用程序工作正常,它连接到数据源。对于 Junit,我通过排除 DatasourceAutoConfiguration、DataSourceTransactionManagerConfiguration、HibernateJpaAutoConfiguration 类来禁用 DataSource 的自动配置,以避免 SpringBoot 自动配置 DataSource。

pom.xml

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
<dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-jpa</artifactId>
  <version>2.2.5.RELEASE</version>
<dependency>
<dependency>
  <groupId>com.microsoft.sqlserver</groupId>
  <artifactId>mssql-jdbc</artifactId>
  <version>8.2.2.jre8</version>
<dependency>

主类

@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan({"com.basepackage"})
public class SampleClass{
  psvm(){
  SpringApplication.run(SampleClass.class,args);
}
}

JuniTestClass

@RunWith(SpringRunner.class)
@SpringBootTest(classes=SampleClass.class)
@AutoConfigureMockMvc
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class, DataSourceTransactionManagerConfiguration.class, HibernateJpaAutoConfiguration.class})
public class SampleControllerTest {
  @MockBean
  private SampleService service;

  @Test
  public void fetchUsers() {
    Mockito.when(service.retrieveUsers().thenReturn(new SampleResponse());
}

}

在 mvn 测试中,测试脚本运行但失败,因为 Springboot 没有自动配置 SampleRepository bean(可能是因为排除了 Datasource 和 HibernateJpa 自动配置类)

错误

Caused by UnsatisfiedDependencyException: Error creating bean with name ServiceDAOImpl, nested exception No qualifying bean of type com.basepackage.repository.SampleRepository' : expected atleast 1 bean which qualifies ...................

如果我删除 AutoConfig Exclusion,Junit 测试在配置数据源的本地工作区中工作正常(尽管不应建立与 DB 的 JUnit 连接)。问题是我正在设置 devops 管道,在“Maven 测试”期间,SpringBoot 自动配置数据源,并且到 Jenkins 到数据库的连接失败。

我想在 Junit - Maven 测试和实际 SpringBoot jar 构建期间禁用自动配置,应该启用自动配置。

有人可以告诉我如何使用注释来实现这一点吗?

【问题讨论】:

    标签: spring-boot junit spring-boot-test spring-autoconfiguration


    【解决方案1】:

    为您的数据库禁用自动配置是一回事,但您的应用程序类依赖(我猜)来自 Spring Data 的 JPA 存储库(他们使用 @Autowired 注入它)来工作。

    如果您现在禁用数据库的自动配置,您的 JPA 存储库将无法由 Spring Data 引导,因此您的服务类在启动时会失败,因为它们需要一个可用的实例:... expected at least 1 bean which qualifies

    我猜你有三个选择来解决这个问题:

    1. 在测试期间使用Testcontainers 提供一个真实 数据库,并再次启用数据相关部分的自动配置。这将是最佳解决方案,因为您可以使用您在生产中使用的相同数据库来测试您的应用程序。
    2. 继续禁用自动配置,但为您的测试提供手动数据源配置:
    @SpringBootTest
    class ApplicationIntegrationTest {
    
      @TestConfiguration
      public static class TestConfig {
    
        @Bean
        public DataSource dataSource() {
          return DataSourceBuilder
            .create()
            .password("YOUR PASSWORD")
            .url("YOUR MSSQL URL")
            .username("YOUR USERNAME")
            .build();
        }
      }
    }
    
    1. 对所有存储库使用@MockBean。这将允许您在没有任何数据库的情况下完全工作,但您必须使用 Mockito 为自己定义与存储库的任何交互:
    @SpringBootTest
    class SpringBootStackoverflowApplicationTests {
    
      @MockBean
      private SampleRepository sampleRepository;
      
      @MockBean
      private OtherRepository otherRepository;
      
      // your tests
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-01
      • 2018-07-06
      • 2020-11-06
      • 1970-01-01
      相关资源
      最近更新 更多