【问题标题】:Cannot inject JpaRepository using annotation @Autowired in spock无法在 spock 中使用注解 @Autowired 注入 JpaRepository
【发布时间】: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


【解决方案1】:

一个可能的原因

...对于这种行为,您的spock-spring maven-dependency 是:

  • 完全丢失(在您的 pom 中)。
  • 在您的本地 maven 存储库中损坏/损坏。

要解决此问题,请:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-19
    • 2012-05-01
    • 2014-10-25
    相关资源
    最近更新 更多