13

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)

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

What is going wrong here?

4 Answers 4

20

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

<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.16 and objenesis 2.1 to your project too.

More details here

Sign up to request clarification or add additional context in comments.

1 Comment

This is not working in 2022. I'm experiencing this issue and any of the solutions worked for me. Using the last version of these dependencies, I got a "Dependency not found" in intelliJ.
2

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/

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

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

Comments

1

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

1 Comment

baeldung.com/mockito-core-vs-mockito-all "As we explored above, mockito-core is the main artifact of Mockito. Newer versions don't release mockito-all anymore. Henceforth, we should only use mockito-core."
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.