无法在 spock 中使用注解 @Autowired 注入 JpaRepository

Cannot inject JpaRepository using annotation @Autowired in spock

我在 spock 中进行了以下测试 class。

@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 单元测试中,我遇到了同样的问题。有没有人在项目中遇到过同样的问题?

一个可能的原因

... 对于此行为,您的 spock-spring maven 依赖项是:

  • 完全缺失(在您的 pom 中)。
  • corrupted/broken 在您的本地 Maven 存储库中。

要解决此问题,请:

  • 确保在您的 pom 中声明依赖项。
  • ..并且在您的本地存储库中可靠地可用。