close

[Solved] How to fix “The import org.mockito cannot be resolved”?

Hello Guys, How are you all? Hope You all Are Fine. Today I am facing the following error How to fix “The import org.mockito cannot be resolved”? in Java. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How to fix “The import org.mockito cannot be resolved” Error Occurs?

Today I am facing the following error How to fix “The import org.mockito cannot be resolved”? in Java.

How To Solve fix “The import org.mockito cannot be resolved” Error ?

  1. How To Solve fix “The import org.mockito cannot be resolved” Error ?

    To Solve fix “The import org.mockito cannot be resolved” Error Output folder: to a separate target fold different from the default target fold, for example: target/test-classes

  2. How to fix “The import org.mockito cannot be resolved”?

    To Solve fix “The import org.mockito cannot be resolved” Error Output folder: to a separate target fold different from the default target fold, for example: target/test-classes

Solution 1

In Project, Go to: Build Path –> Configuration Path,

In Java Build Path, Go to: Source

In both directories src/test/java and src/test/resource, set:

Output folder: to a separate target fold different from the default target fold, for example: target/test-classes

Contain Test Resources: Yes

Solution 2


You are trying to import the wrong packages

This is what should be in your pom.xml file under dependencies

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

And simple test should look like this

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class InjectMocksTest {

@Mock
private ClassB classB;
@InjectMocks
private ClassA classA;

@Test
public void test() {
    Mockito.doReturn("Hi").when(classB).message();

    Assert.assertEquals("Hi David!", classA.greeting("David"));

}

private static class ClassA {

    private final ClassB classB;

    private ClassA(ClassB classB) {
        this.classB = classB;
    }

    public String greeting(String name) {
        return classB.message() + " " + name + "!";
    }
}

private static class ClassB {
    public String message() {
        return "Hello";
    }
}

}

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read