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

16,957

Solution 1

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

Solution 2

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

Solution 3

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;

Solution 4

Open File > Project Structure...

and then add it manually as library dependency:

enter image description here

Share:
16,957

Related videos on Youtube

Sirop4ik
Author by

Sirop4ik

I work as a programmer in one great company. I carry out a serious project on Android, a huge amount of valuable experience, constant research of new technologies and implementation of non-trivial tasks. I mastered a lot of technology libraries. You are welcome to Visit my web-page: https://alextimowenko.wixsite.com/android

Updated on June 04, 2022

Comments

  • Sirop4ik
    Sirop4ik almost 2 years

    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!

  • Sirop4ik
    Sirop4ik over 7 years
    It looks like you use eclipse... I use AndroidStudio
  • Timothy Truckle
    Timothy Truckle over 7 years
    @AlekseyTimoshchenko Yes, I use eclipse. But AS might also have "gradle personality" (or what ever they call it) for projects. As long as you have a "plain Java" Project in AS it will not track your changes to the gradle build file(s).
  • helvete
    helvete about 2 years
    This is the correct answer IMO. Thanks