【发布时间】:2020-04-19 10:33:42
【问题描述】:
我在 spock 中有以下测试类。
@DataJpaTest
@SpringBootTest(classes = MainSpring.class)
@ContextConfiguration
class AccountRepositorySpec extends Specification {
@Autowired
private AccountRepository accountRepository;
def "Example test"(){
given:
int k=1
expect:
1==1
}
}
这是一个存储库 JPA 接口。
@Repository
public interface AccountRepository extends JpaRepository<Account, String> {
}
在调试模式下,每次我运行测试时,accountRepository 都是空的。 可能是什么问题。
这是我的 pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<version>1.1-groovy-2.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<goals>
<goal>addTestSources</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20</version>
<configuration>
<groups>com.microservices.accountservice.UnitTest</groups>
<includes>
<include>**/*Spec.java</include>
<include>**/*Test.java</include>
</includes>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
我以这种方式配置了我的项目,以便运行 Junit 和 Spock 单元测试。我也在使用 postgresql jdbc。问题是我不能使用以下注释在 Spock 测试中注入任何组件,如服务、存储库或 RestControllers。在 Junit 单元测试中,我有同样的问题。有人在项目中遇到过同样的问题吗?
【问题讨论】:
-
gmavenplus-plugin已配置? (maven/gradle?)..有(额外的)@ContextConfiguration任何(隐藏的)目的吗? -
进行调查:尝试自动装配/调试
ApplicationContext... -
@xerx593 我添加了关于 maven 的 pom.xml。
-
@xerx593,一切正常,maven 在导入依赖项时遇到了一些问题。
标签: spring spring-boot spring-data-jpa spock