eclipse NoClassDefFoundError:Mockito Bytebuddy

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37420865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 23:11:19  来源:igfitidea点击:

NoClassDefFoundError: Mockito Bytebuddy

javaeclipsemavenmockito

提问by efong5

I recently added Mockito to a maven project on eclipse, by adding the external jar "mockito-core-2.0.53-beta.jar", and upon attempting to create my first mock object (Line two in the function)

我最近通过添加外部 jar“mockito-core-2.0.53-beta.jar”并尝试创建我的第一个模拟对象(函数中的第二行)将 Mockito 添加到 Eclipse 上的 Maven 项目中

enter image description here

在此处输入图片说明

And upon running it, the console prints out the first line, then throws this error:

在运行它时,控制台打印出第一行,然后抛出这个错误:

enter image description here

在此处输入图片说明

It seems like previously there was a similar issue, but it was supposedly fixed internally. https://github.com/raphw/byte-buddy/issues/99

之前好像也有类似的问题,但据说是在内部修复的。https://github.com/raphw/byte-buddy/issues/99

What is going wrong here?

这里出了什么问题?

回答by Nicolas Filotto

You simply forgot to add the dependencies to your project which are according to the pom file:

您只是忘记根据 pom 文件将依赖项添加到您的项目中:

<dependency>
  <groupId>net.bytebuddy</groupId>
  <artifactId>byte-buddy</artifactId>
  <version>1.3.16</version>
  <scope>runtime</scope>
</dependency>
<dependency>
  <groupId>org.objenesis</groupId>
  <artifactId>objenesis</artifactId>
  <version>2.1</version>
  <scope>runtime</scope>
</dependency>

In other words you need to add byte-buddy 1.3.16and objenesis 2.1to your project too.

换句话说,您也需要将byte-buddy 1.3.16和添加objenesis 2.1到您的项目中。

More details here

更多细节在这里

回答by Miguel Gomes

There is a post that explain the problem pretty well, you can find it here:

有一个帖子很好地解释了这个问题,你可以在这里找到它:

https://solidsoft.wordpress.com/2012/09/11/beyond-the-mockito-refcard-part-3-mockito-core-vs-mockito-all-in-mavengradle-based-projects/

https://solidsoft.wordpress.com/2012/09/11/beyond-the-mockito-refcard-part-3-mockito-core-vs-mockito-all-in-mavengradle-based-projects/

If you are not using gradle or maven and you are just using mockit-core you should add these dependencies:

如果你没有使用 gradle 或 maven 而你只是使用 mockit-core,你应该添加这些依赖项:

<dependency>
  <groupId>net.bytebuddy</groupId>
  <artifactId>byte-buddy</artifactId>
  <version>1.7.9</version>
  <scope>runtime</scope>
</dependency>
<dependency>
  <groupId>org.objenesis</groupId>
  <artifactId>objenesis</artifactId>
  <version>2.4</version>
  <scope>runtime</scope>
</dependency>

回答by Jyotirup

Instead adding

而是添加

mockito-core

模拟核心

better option will to add

更好的选择将添加

mockito-all

模拟一切

Refer to this link https://mvnrepository.com/artifact/org.mockito/mockito-all/2.0.2-beta

请参阅此链接https://mvnrepository.com/artifact/org.mockito/mockito-all/2.0.2-beta

回答by deveduardo

java.lang.NoClassDefFoundError

java.lang.NoClassDefFoundError

This indicates that in your .jar (org.mockito), you don't have that class. This usually occurs when you have more than one .jar (with a different version) in your classpath. You could check that.

这表明在您的 .jar (org.mockito) 中,您没有那个类。当您的类路径中有多个 .jar(具有不同版本)时,通常会发生这种情况。你可以检查一下。