本文介绍了Maven 3 和 JUnit 4 编译问题:包 org.junit 不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Maven 构建一个简单的 Java 项目.在我的 pom 文件中,我将 JUnit 4.8.2 声明为唯一的依赖项.Maven 仍然坚持使用 JUnit 3.8.1 版.我该如何解决?

I am trying to build a simple Java project with Maven. In my pom-file I declare JUnit 4.8.2 as the only dependency. Still Maven insists on using JUnit version 3.8.1. How do I fix it?

问题表现为编译失败:包 org.junit 不存在".这是因为我的源代码中的 import 语句.JUnit 4.* 中正确的包名称是 org.junit.* 而在版本 3.* 中它是 junit.framework.*

The problem manifests itself in a compilation failure: "package org.junit does not exist". This is because of the import statement in my source code. The correct package name in JUnit 4.* is org.junit.* while in version 3.* it is junit.framework.*

我想我已经在 http://maven.apache.org/plugins/maven-surefire-plugin/examples/junit.html 但那里的建议似乎是针对 Maven 专家的.我不明白该怎么做.

I think I have found documentation on the root of the problem on http://maven.apache.org/plugins/maven-surefire-plugin/examples/junit.html but the advice there seems to be meant for Maven experts. I did not understand what to do.

推荐答案

@Dennis Roberts:您说得对:我的测试类位于 src/main/java.此外,JUnit 的 POM 中范围"元素的值是测试",尽管它应该是这样.问题是我在 Eclipse 中创建测试类时很草率,导致它被创建在 src/main/java 的 src/test/java 中.在运行mvn eclipse:eclipse"后,这在 Eclipse 的项目资源管理器视图中变得更容易看到,但是您的评论让我首先看到它.谢谢.

@Dennis Roberts: You were absolutely right: My test class was located in src/main/java. Also the value of the "scope" element in the POM for JUnit was "test", although that is how it is supposed to be. The problem was that I had been sloppy when creating the test class in Eclipse, resulting in it being created in src/main/java insted of src/test/java. This became easier to see in Eclipse's Project Explorer view after running "mvn eclipse:eclipse", but your comment was what made me see it first. Thanks.

这篇关于Maven 3 和 JUnit 4 编译问题:包 org.junit 不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 08:21