gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 21:15:59 24 4
gpt4 key购买 nike

我有一个 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>

...

这是来自测试/资源的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

这是 ma​​in/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 属性中获取它们。

有什么想法吗?谢谢。

block 引用

最佳答案

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

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

关于maven - 如何在 SpringBoot 应用程序测试上下文中使用来自测试 application.yml 的 spring 数据源设置的参数实例化 groovy.sql.Sql?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58300540/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com