导入 Spring 的 ReflectionTestUtils 类时出现问题

Jam*_*unn 1 java spring intellij-idea spring-test maven

我正在开发一个名为acme-platform的多模块 Maven 项目,模块设置如下:

  1. acme 管理
  2. acme-通用
  3. acme-全球
  4. acme服务
  5. acme-客户端
  6. acme-注册
  7. acme 属性
  8. acme测试

(它们在acme-platform pom中按此顺序列出。)

在某些模块中,我已经能够使用 Spring 的 ReflectionTestUtils 类。然而,在最后一个模块acme-test中,我确实想使用它,但我无法使用。acme-test pom中没有依赖项部分,因此我添加了一个。这是 pom:

<?xml version="1.0" encoding="UTF-8"?>
Run Code Online (Sandbox Code Playgroud)

http://maven.apache.org/xsd/maven-4.0.0.xsd">

<parent>
    <artifactId>acme-platform</artifactId>
    <groupId>com.awesomeness.acme</groupId>
    <version>1.21.0</version>
    <relativePath>../</relativePath>
</parent>

<modelVersion>4.0.0</modelVersion>

<artifactId>acme-test</artifactId>
<version>1.21.0</version>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
    </dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)

在添加依赖行之前,我无法将任何 Spring 的 api 导入到我的类中。导入这些行后,我能够访问大多数类,但不是全部,特别是 ReflectionTestUtils,即使它是 spring-test 模块的一部分(可以在此处验证)。

我正在使用 Intellij。我查看了其他问题(例如这个问题)的答案,以确保我正确更新了我的依赖项。无济于事。

有谁知道为什么我无法导入org.springframework.test.util.ReflectionTestUtilsacme -test

如果您需要任何其他信息,请告诉我。

编辑

依赖项的版本信息不在任何模块 pom 中,但在根 pom ( acme-platform )中指定。同样,我可以在其他模块中导入 ReflectionTest,但不能在acme-test中导入。因此我由此推断,只要在根 pom 中使用指定版本声明依赖项,就不需要在任何模块 pom 中指定版本。(如果我在这方面有错,请纠正我。)

额外编辑

顺便说一下,我也无法导入junit。


Sam*_*nen 5

您需要将 Maven 设置scopetest和依赖项。spring-testjunit

例如:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)