如何在 SpringBoot 应用程序测试上下文中使用来自测试 application.yml 的 spring 数据源设置的参数实例化 groovy.sql.Sql?

How to instantiate groovy.sql.Sql with parameters from spring datasource setup from test application.yml in a SpringBoot application test context?

我有一个 SpringBoot maven 项目,它有一个存储库相关的 jar 子模块,它存储了与数据库相关的执行,存储库 类 和 JdbcTemplate 等

我想用 Spock 测试数据库 Groovy。

这是我的pom.xml:

...

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <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-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
    </dependency>

    <dependency>
        <groupId>com.microsoft.sqlserver</groupId>
        <artifactId>mssql-jdbc</artifactId>
    </dependency>

    <!-- Spock Dependencies -->
    <dependency>
        <groupId>org.spockframework</groupId>
        <artifactId>spock-spring</artifactId>
        <scope>test</scope>
    </dependency>
    <!-- enables mocking of classes (in addition to interfaces) -->
    <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib-nodep</artifactId>
        <scope>test</scope>
    </dependency>
    <!-- enables mocking of classes without default constructor (together with CGLIB) -->
    <dependency>
        <groupId>org.objenesis</groupId>
        <artifactId>objenesis</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.codehaus.groovy.modules.http-builder</groupId>
        <artifactId>http-builder</artifactId>
        <scope>test</scope>
    </dependency>
    <!-- Spock End -->
</dependencies>

<build>
    <finalName>${project.artifactId}</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <includes>
                    <include>**/*Spec.*</include>
                    <include>**/*Test.*</include>
                </includes>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.gmavenplus</groupId>
            <artifactId>gmavenplus-plugin</artifactId>
            <version>1.5</version>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>testCompile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>test-jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

...

这是来自 test/resources 的 application.yml:

spring:
  datasource:
    platform: sqlserver
    driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
    url: jdbc:sqlserver://localhost;databaseName=mydatabase;integratedSecurity=true

server:
  address: 127.0.0.1
  port: 9000

这是 main/java/mypackage 中的 应用程序配置:

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class RepositoryConfig {

}

这是 groovy 测试规范 应该设置 groovy.sql.Sql:

@SpringBootTest(classes = RepositoryConfig.class, webEnvironment = WebEnvironment.MOCK)
@Configuration
@EnableConfigurationProperties
@Transactional
class BaseSpringBootTestSpec extends Specification {

    String url
    String username
    String password
    String driverClassName

    protected groovy.sql.Sql sql

    def setup() {
        sql = groovy.sql.Sql.newInstance(url, username, password, driverClassName)
    }

    def cleanup() {
        sql.close()
    }

}

但在这里我得到 NullPointerException,因为属性都是空的(url、driverClassName 等)。 而且我不知道如何从文本上下文中的 yml 属性中获取它们。

有什么想法吗?谢谢。

块引用

您永远不会告诉 Spring 实际注入这些值。在您的字段中添加 @Value 注释应该可以解决您的问题。例如:

@Value("#{spring.datasource.url}")
String url