Martin
Martin

Reputation: 91

Unable to import mockito in Eclipse - disappeared immediately

I am trying to do a mock test of my code with mockito. In my POM.xml, the following dependency is stated as below.

<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-all</artifactId>
    <version>1.9.5</version>
</dependency>

However, when I am trying to import the mockito as below, it disappeared immediately and I cannot do anything with it:

import static org.mockito.Mockito.*;

I don't know if there is anything missing in my Eclipse or not. Is there any suggestion so that I can fix it?

"The import org.mockito cannot be resolved" just appeared on the error box.

Upvotes: 3

Views: 14487

Answers (2)

Akshay Chopra
Akshay Chopra

Reputation: 1253

I made a very silly mistake. The Junit test cases that I was writing were placed under src/main instead of src/test folder. Hence, even the dependency was added in the pom.xml file, it was saying it cannot be resolved. Just make sure your .java file for Junit test cases is under src/test/

Upvotes: 4

Marcus Biel
Marcus Biel

Reputation: 440

"it disappeared immediately and I cannot do anything with it". This sounds like Eclipse save action + organize imports to me, removing imports not used. Try to remove this Eclipse save action as follows: Window->Preferences->Java->Editor->Save Actions->Configure (right hand side)-> Go to last tab "Unnecessary Code". Remove "Remove unused import". Alternatively, write the code using this static import first and than add the static import afterwards. Last alternative - add the import and the usage of this import in one step, without saving in between, this should prevent the auto save action to be called.

Regarding:

I just discovered that there is no "org.mockito.Mockito" in my maven >dependency. How could I get it?

In Eclipse Project Explorer, go Maven Dependencies of your Java project. Go to the mockito jar somewhere at the bottom. Open it up, select the org.mockito package, there should be the Mockito.class there, I am pretty sure (it sounds like you got class and package name confused).

Upvotes: 0

Related Questions