在 IntelliJ IDEA 中使用 Maven/pom.xml 声明对 Mockito 的依赖

Declaring dependency on Mockito using Maven/pom.xml in IntelliJ IDEA

我对使用 Java 比较陌生,想改进我使用模拟编写的这些单元测试。我听说 mockito 是一个很好的库,所以我试图在我的项目的 pom.xml 文件中声明依赖关系,但是无论我将这些行放在文件中的什么地方,我都会收到错误消息“发现无效内容以元素 'dependency'。我查看了 Mockito 文档,但它们似乎直接跳转到在测试文件顶部导入,而不是实际配置它。

我的 pom.xml 当前文件:

<modelVersion>4.0.0</modelVersion>


<groupId>groupId</groupId>
<artifactId>JAirport</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.10</source>
                <target>1.10</target>
            </configuration>
        </plugin>

    </plugins>
</build>
<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-all</artifactId>
    <version>1.8.4</version>
    <scope>test</scope>
</dependency>
</project>

谢谢!

您需要将 <dependency> 标签包裹在 <dependencies> 标签中

<modelVersion>4.0.0</modelVersion>


<groupId>groupId</groupId>
<artifactId>JAirport</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.10</source>
                <target>1.10</target>
            </configuration>
        </plugin>

    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>1.8.4</version>
        <scope>test</scope>
    </dependency>
</dependencies>
</project>