简体   繁体   中英

Trouble importing Spring's ReflectionTestUtils class

I'm working on a multi-module maven project called acme-platform , with the modules set up like so:

  1. acme-admin
  2. acme-common
  3. acme-global
  4. acme-services
  5. acme-client
  6. acme-registration
  7. acme-properties
  8. acme-test

(They are listed in this order in the acme-platform pom.)

In some of the modules, I have been able to use Spring's ReflectionTestUtils class. However, in the last module, acme-test , where I really want to use it, I am unable to. There was no dependency section in the acme-test pom, so I added one. Here is the pom:

<?xml version="1.0" encoding="UTF-8"?>

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>

Before adding the dependency lines, I couldn't import any of Spring's api into my classes. After importing these lines, I was able to access most of the classes, but not all of them, and in particular not ReflectionTestUtils, even though it is part of the spring-test module (as can be verified here ).

I am using Intellij. I have looked at answers to other questions (such as this one ) to make sure I'm updating my dependencies correctly. To no avail.

Does anyone have any idea as to why I can't import org.springframework.test.util.ReflectionTestUtils into acme-test ?

Let me know if you need any aditional information.

EDIT

The version information of the dependencies are not in any of the module poms, but they are specified in the root pom ( acme-platform ). Again, I can import ReflectionTest in the other modules, just not in acme-test . So I deduce from this that as long as the dependency is declared with a specified version in the root pom, it doesn't need a version specified in any of the module poms. (If I'm wrong on this, please correct me.)

ADDITIONAL EDIT

By the way, I can't import junit either.


You need to set the Maven scope to test for both the spring-test and junit dependencies.

For example:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <scope>test</scope>
</dependency>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM