Sirop4ik
Sirop4ik

Reputation: 5263

Why do I get an error "package org.mockito.runners does not exist"?

I have pluged up require dependency

testCompile 'org.mockito:mockito-core:1.10.19'

Then I put my test code to /src/test/java/ directory

then I have tried launch such test

import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

public class PresenterActivityAcceptNotAcceptTest {

@Test
public void emailValidator_CorrectEmailSimple_ReturnsTrue() {
    boolean dd = true;
    assertThat(dd, is(true));
} 

it works properly, but if I add anything witch associated with Mock lib

for example @RunWith

    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.mockito.runners.MockitoJUnitRunner;

    import static org.hamcrest.CoreMatchers.is;
    import static org.hamcrest.MatcherAssert.assertThat;

@RunWith(MockitoJUnitRunner.class)
public class PresenterActivityAcceptNotAcceptTest {


    @Test
    public void emailValidator_CorrectEmailSimple_ReturnsTrue() {
        boolean dd = true;
        assertThat(dd, is(true));
    }

I got such error

Error:Execution failed for task   ':Application:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
Error:(10, 10) error: cannot find symbol class MockitoJUnitRunner
Error:(5, 27) error: package org.mockito.runners does not exist
/home/aleksey/Downloads/NTZ/FittingRoom/Application/src/test/java/com/fittingroom/newtimezone/presenters/PresenterActivityAcceptNotAcceptTest.java

What am I doing wrong?

If I forger about something feel free to ask

Thanks in advance!

Upvotes: 16

Views: 36600

Answers (6)

Pierre C
Pierre C

Reputation: 3478

As stated in Baeldung's article, starting from Mockito version 2.2.20, the package for MockitoJUnitRunner has changed. So change :

import org.mockito.runners.MockitoJUnitRunner;

To :

import org.mockito.junit.MockitoJUnitRunner;

As usual, you have to import the mockito-core library in your build.gradle :

dependencies {
    testImplementation 'org.mockito:mockito-core:4.2.0'
}

As a side note, you will also need to change import for Matchers if you use them. For instance :

From :

import static org.mockito.Matchers.any;

To :

import static org.mockito.Mockito.any;

Upvotes: 14

Michael  Sizonenko
Michael Sizonenko

Reputation: 29

Got the same issue. Attempted to implement the developer.android documentation example.

Fixed by changed org.mockito version to the recent at the time in build.gradle :

dependencies {
    testImplementation 'org.mockito:mockito-core:2.28.2'
}

Upvotes: 2

Ustin
Ustin

Reputation: 588

I also got this exception when I tried to write instrumentalTests in src/androidTest/java/ but by default mockito's scope in dependencies is set as Unit Test implementation (you can see that in File => Project Structure => Modules => Dependencies). I've just changed this parameter to Test implementation and it works!

enter image description here

Upvotes: 0

Javatar
Javatar

Reputation: 2665

Open File > Project Structure...

and then add it manually as library dependency:

enter image description here

Upvotes: 1

Timothy Truckle
Timothy Truckle

Reputation: 15624

does your project look like this?: enter image description here

Upvotes: -1

P S M
P S M

Reputation: 1161

It looks like Gradle is not doing it's job. Manually adding the jars may fixed the problem. How to Download and Install jar go here .

and for download mockito use this link

https://mvnrepository.com/artifact/org.mockito/mockito-core/1.10.19

Upvotes: 7

Related Questions