Maven 3和JUnit 4编译问题:包org.junit不存在

发布于 2024-11-04 01:00:28 字数 475 浏览 1 评论 0 原文

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

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

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

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?

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.*

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(17

零崎曲识 2024-11-11 01:00:29

我也遇到了同样的问题,如下所示。

输入图片此处的说明

要解决此问题,请将以下行添加到应用程序级别 build.gradle 中的 dependencies 部分。

compile 'junit:junit:4.12'
androidTestCompile 'com.android.support.test:runner:0.5'

Gradle 构建随后报告以下警告。

Warning:Conflict with dependency 'com.android.support:support-annotations'. 
Resolved versions for app (25.1.0) and test app (23.1.1) differ. 
See http://g.co/androidstudio/app-test-app-conflict for details.

为了解决此警告,以下部分添加到应用程序级别的 build.gradle 中。

configurations.all {
    resolutionStrategy {
        force 'com.android.support:support-annotations:23.1.1'
    }
}

Me too had the same problem as shown below.

enter image description here

To resolve the issue, below lines are added to dependencies section in the app level build.gradle.

compile 'junit:junit:4.12'
androidTestCompile 'com.android.support.test:runner:0.5'

Gradle build then reported following warning.

Warning:Conflict with dependency 'com.android.support:support-annotations'. 
Resolved versions for app (25.1.0) and test app (23.1.1) differ. 
See http://g.co/androidstudio/app-test-app-conflict for details.

To solve this warning, following section is added to the app level build.gradle.

configurations.all {
    resolutionStrategy {
        force 'com.android.support:support-annotations:23.1.1'
    }
}
看轻我的陪伴 2024-11-11 01:00:29

我有一个类似的问题,Eclipse 编译我的代码很好,但每次编译测试时 Maven 都会失败,尽管 JUnit 在我的依赖项列表中并且测试位于 /src/test/java/ 中。

就我而言,我的依赖项列表中的 JUnit 版本错误。我编写了 JUnit4 测试(带注释),但将 JUnit 3.8.x 作为我的依赖项。在 JUnit 3.8.x 和 4 版本之间,他们将包名称从 junit.framework 更改为 org.junit,这就是 Maven 仍然无法使用 JUnit jar 进行编译的原因。

我仍然不完全确定为什么 Eclipse 能够成功编译。它必须在类路径中的某个位置拥有自己的 JUnit4 副本。希望这个替代解决方案对人们有用。在遵循上面亚瑟的链接后,我找到了这个解决方案。

I had a similar problem of Eclipse compiling my code just fine but Maven failed when compiling the tests every time despite the fact JUnit was in my list of dependencies and the tests were in /src/test/java/.

In my case, I had the wrong version of JUnit in my list of dependencies. I wrote JUnit4 tests (with annotations) but had JUnit 3.8.x as my dependency. Between version 3.8.x and 4 of JUnit they changed the package name from junit.framework to org.junit which is why Maven still breaks compiling using a JUnit jar.

I'm still not entirely sure why Eclipse successfully compiled. It must have its own copy of JUnit4 somewhere in the classpath. Hope this alternative solution is useful to people. I reached this solution after following Arthur's link above.

半枫 2024-11-11 01:00:29

我也遇到了这个问题 - 我试图从源中提取一个对象,它在测试代码中工作,但在 src 代码中不起作用。为了进一步测试,我从测试中复制了一段代码并将其放入 src 代码中,然后立即删除了 JUnit 行,这样我就知道了测试如何拉入对象。然后突然我的代码无法编译。
问题是,当我放入代码时,Eclipse 帮助解析了所有类,因此我有来自我的 src 代码的 JUnit 调用,这是不正确的。我应该注意到顶部有关未使用的导入的警告,但我忽略了看到它们。
当我删除了 src 文件中未使用的 JUnit 导入后,一切都运行得很好。

I also ran into this issue - I was trying to pull in an object from a source and it was working in the test code but not the src code. To further test, I copied a block of code from the test and dropped it into the src code, then immediately removed the JUnit lines so I just had how the test was pulling in the object. Then suddenly my code wouldn't compile.
The issue was that when I dropped the code in, Eclipse helpfully resolved all the classes so I had JUnit calls coming from my src code, which was not proper. I should have noticed the warnings at the top about unused imports, but I neglected to see them.
Once I removed the unused JUnit imports in my src file, it all worked beautifully.

梦罢 2024-11-11 01:00:29

如果您在 src/main/java 中有代码,请找到此错误的唯一解决方案

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.9.1</version>
</dependency>

Find the one solution for this error if you have code in src/main/java Utils

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.9.1</version>
</dependency>
祁梦 2024-11-11 01:00:29

更改 junit 版本为我解决了这个问题。似乎 3.8.1 版本在我的情况下不起作用。更改为 4.12 后修复了问题

Changing the junit version fixed this for me. Seems like version 3.8.1 didn't work in my case. Issue fixed upon changing it to 4.12

亢潮 2024-11-11 01:00:29

我遇到了这个问题,这就是我解决它的方法:

上下文:

  • SpringBoot应用程序
  • 使用maven管理多个模块
  • 在根POM的dependencyManagement中添加junit的maven依赖项(而不是< code>dependency,它们的差异可以在此处找到)
  • 打算测试根目录之一内的类或文件夹模块的子模块

PS:如果您的情况与上述上下文不符,则此解决方案可能无法解决您的问题。

步骤

  1. 右键单击要测试的类或文件夹:
    输入图片此处描述
  2. 选择更多运行/调试 -> 修改运行配置
  3. 将模块选项从根模块更改为要测试的选项
    输入图片此处描述

I met this problem, this is how I soloved it:

Context:

  • SpringBoot application
  • Use maven to manage multiple modules
  • Add junit's maven dependency in root POM's dependencyManagement(rather than dependencies, their differences can be found here)
  • Intend to test class or folder inside one of the root module's child module

PS: If your situation does not match the context above, this solution may not solve your problem.

Steps

  1. right click at the class or folder you want to test:
    enter image description here
  2. Choose More Run/Debug -> Modify Run Configuration
  3. Change the module option to the one you want to test from root module
    enter image description here
王权女流氓 2024-11-11 01:00:29

默认情况下,maven 分别查看这些文件夹中的 java 和测试类 -
src/main/java 和 src/test/java

当使用 source 下的测试类指定 src 且 pom.xml 中 junit 依赖项的范围被提及为 test - org.unit 将不会被 maven 找到。

By default , maven looks at these folders for java and test classes respectively -
src/main/java and src/test/java

When the src is specified with the test classes under source and the scope for junit dependency in pom.xml is mentioned as test - org.unit will not be found by maven.

北陌 2024-11-11 01:00:28

只是为了获得完整解决方案的答案来帮助访问者:

您所需要做的就是将 junit 依赖项添加到 pom.xml 中。不要忘记test

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.11</version>
  <scope>test</scope>
</dependency>

Just to have an answer with the complete solution to help the visitors:

All you need to do is add the junit dependency to pom.xml. Don't forget the <scope>test</scope>

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.11</version>
  <scope>test</scope>
</dependency>
一生独一 2024-11-11 01:00:28

@Dennis Roberts:你完全正确:我的测试类位于 src/main/java 中。此外,JUnit 的 POM 中“scope”元素的值是“test”,尽管它应该是这样的。问题是我在 Eclipse 中创建测试类时比较草率,导致它被创建在 src/main/java 而不是 src/test/java 中。运行“mvn eclipse:eclipse”后,在 Eclipse 的 Project Explorer 视图中更容易看到这一点,但您的评论让我首先看到它。谢谢。

@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.

薯片软お妹 2024-11-11 01:00:28

我的问题是我的 pom.xml 中的一行,我有行 ${basedir}/src 删除此行使 Maven 使用常规结构解决我的问题的文件夹

my problem was a line inside my pom.xml i had the line <sourceDirectory>${basedir}/src</sourceDirectory> removing this line made maven use regular structure folders which solves my issue

脸赞 2024-11-11 01:00:28

删除 pom.xml 中 junit 的范围标签有效。

removing the scope tag in pom.xml for junit worked..

绳情 2024-11-11 01:00:28

将此依赖项添加到您的 pom.xml 文件中:

http: //mvnrepository.com/artifact/junit/junit-dep/4.8.2

<!-- https://mvnrepository.com/artifact/junit/junit-dep -->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit-dep</artifactId>
    <version>4.8.2</version>
</dependency>

Add this dependency to your pom.xml file:

http://mvnrepository.com/artifact/junit/junit-dep/4.8.2

<!-- https://mvnrepository.com/artifact/junit/junit-dep -->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit-dep</artifactId>
    <version>4.8.2</version>
</dependency>
预谋 2024-11-11 01:00:28

我也有同样的问题。我所做的只是 - 从 pom.xml 文件中,我删除了 junit 3.8 的依赖项,并添加了 junit 4.8 的新依赖项。然后我做了 maven clean 和 maven install 。它成功了。为了验证,在maven安装之后,我去了项目->属性-构建路径->maven依赖项,看到现在junit 3.8 jar消失了!,而是列出了junit 4.8 jar。凉爽的!!。现在我的测试运行得非常顺利..希望这能有所帮助..

I had the same problem. All i did was - From the pom.xml file i deleted the dependency for junit 3.8 and added a new dependency for junit 4.8. Then i did maven clean and maven install. It did the trick. To verify , after maven install i went project->properties-build path->maven dependencies and saw that now the junit 3.8 jar is gone !, instead junit 4.8 jar is listed. cool!!. Now my test runs like a charm.. Hope this helps somehow..

带刺的爱情 2024-11-11 01:00:28

我的文件位于正确的位置,只需从 JUnit 依赖项条目中删除 test 即可解决问题(我使用的是 JUnit 4.12)。我相信,在 test 范围内,依赖关系在编译阶段就被忽略了。现在,即使我调用 mvn test ,一切都正常。

I had my files at the correct places, and just removing <scope>test</scope> from the JUnit dependency entry solved the problem (I am using JUnit 4.12). I believe that with the test scope the dependency was just being ignored during the compilation phase. Now everything is working even when I call mvn test.

无语# 2024-11-11 01:00:28

我的案例只是一个简单的疏忽。

我将 JUnit 依赖项声明放在 节点下的 中,而不是 中POM 文件。正确的做法是:

<project>
<!-- Other elements -->
    <dependencies>
    <!-- Other dependencies-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>
    </dependencies>
<project>

My case was a simple oversight.

I put the JUnit dependency declaration inside <dependencies> under the <dependencyManagement/> node instead of <project/> in the POM file. Correct way is:

<project>
<!-- Other elements -->
    <dependencies>
    <!-- Other dependencies-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>
    </dependencies>
<project>
千年*琉璃梦 2024-11-11 01:00:28

我在父项目注入依赖项的“test-utils”项目(向 JUnit 添加功能、规则和断言)子项目中遇到了非常类似的问题。
依赖于 org.junit.rules 包的类位于 src/main/java 中。

因此,我在没有测试范围的情况下添加了对 junit 的依赖,它解决了问题:

test-util 项目的 pom.xml :

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
</dependency>

父项目的 pom.xml :

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <scope>test</scope>
</dependency>

I had a quite similar problem in a "test-utils" project (adding features, rules and assertions to JUnit) child of a parent project injecting dependencies.
The class depending on the org.junit.rules package was in src/main/java.

So I added a dependency on junit without test scope and it solved the problem :

pom.xml of the test-util project :

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
</dependency>

pom.xml of the parent project :

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <scope>test</scope>
</dependency>
千里故人稀 2024-11-11 01:00:28

你是如何声明版本的?

<version>4.8.2</version>

请注意此声明解释的含义 此处(请参阅注释)

当为 Junit 声明“正常”版本(例如 3.8.2)时,在内部表示为为“允许任何内容,但更喜欢 3.8.2”。这意味着当检测到冲突时,Maven 可以使用冲突算法来选择最佳版本。如果您指定 [3.8.2],则意味着仅使用 3.8.2,而不会使用其他版本。

要强制使用版本 4.8.2,请尝试

<version>[4.8.2]</version>

因为您的项目中没有任何其他依赖项,所以应该'任何冲突都不会导致您的问题。如果您能够从存储库获取此版本,第一个声明应该适合您。您是否从父 pom 继承依赖项?

How did you declare the version?

<version>4.8.2</version>

Be aware of the meaning from this declaration explained here (see NOTES):

When declaring a "normal" version such as 3.8.2 for Junit, internally this is represented as "allow anything, but prefer 3.8.2." This means that when a conflict is detected, Maven is allowed to use the conflict algorithms to choose the best version. If you specify [3.8.2], it means that only 3.8.2 will be used and nothing else.

To force using the version 4.8.2 try

<version>[4.8.2]</version>

As you do not have any other dependencies in your project there shouldn't be any conflicts that cause your problem. The first declaration should work for you if you are able to get this version from a repository. Do you inherit dependencies from a parent pom?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文