在测试过程中省略Spring Data JPA自动装配

时间:2019-03-01 14:27:37

标签: spring unit-testing spring-boot mockito autowired

我有一个Spring Boot应用程序,该应用程序使用一个库(我自己的库),该库具有Spring Data JPA依赖项。 在该库中,我正在使用JPA存储库接口和一个使用JpaSpecificationExecutor接口的自定义存储库片段。

我在库中有一个@EnableJpaRepositories的配置,如下所示:

@EnableJpaRepositories(basePackages = "my.package.domain.jpa.repo", bootstrapMode= BootstrapMode.LAZY)
@EntityScan(basePackageClasses = {LookupConfig.class, LookupMapping.class})
@Configuration
public class LookupJpaConfiguration {}

我还声明了(未生成)与实体类相同的包中的实体类的元模型(在类名称后附加_

主应用程序也是@SpringBootApplication,以spring-boot-starter-parent:2.1.0作为父级。该应用程序将库用作依赖项,并@Autowire使用库中的类。该应用程序本身并不直接依赖于Spring Data。

问题是,我无法运行单元测试而不添加数据源的属性

这是测试类的声明的样子:

@ExtendWith({SpringExtension.class, MockitoExtension.class})
public class OrderStatusProcessorTest {

    // Uses the library with JPA dependency
    @MockBean private LookupService lookupService;

    // The class being tested; uses LookupService
    private final OrderStatusProcessor orderStatusProcessor;

    public OrderStatusProcessorTest() {
        orderStatusProcessor = new OrderStatusProcessor(lookupService);

    }
...

我已经尝试过@MockDataSourceEntityManagerFactory的使用,但是这些都不起作用。

图书馆的pom文件

<properties>
    <spring-data-releasetrain.version>Lovelace-SR5</spring-data-releasetrain.version>
</properties>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.3.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

在当前设置下,我看到的错误如下-

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)
    ...

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Unsatisfied dependency expressed through method 'entityManagerFactory' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'entityManagerFactoryBuilder' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Unsatisfied dependency expressed through method 'entityManagerFactoryBuilder' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaVendorAdapter' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.JpaVendorAdapter]: Factory method 'jpaVendorAdapter' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class

如果我正确提供了数据源属性,它将起作用。但是我想要的是在不实际连接数据库的情况下运行测试。

让我知道是否需要提供更多信息。预先感谢!

0 个答案:

没有答案