NPE when calling MockitoAnnotations.initMocks() in AndroidTestCase

前端 未结 4 1776
耶瑟儿~
耶瑟儿~ 2021-01-04 09:04

Trying to use mockito in my AndroidTestCase. I added the dependencies to the build.gradle:

final DEXMAKER_VERSION = \'1.2\'

dependencies {
    // ...
    an         


        
相关标签:
4条回答
  • 2021-01-04 09:57

    Its a bug in dexmaker for which I have submitted a fix: https://github.com/crittercism/dexmaker/pull/24

    0 讨论(0)
  • 2021-01-04 09:57

    For me the solution was to use the Method Mockito.mock() for each Mocking Object instead of using MockitoAnnotations.initMocks(this);

    So for example:

    public class HomePresenterTest {
        private Repository repository;
        private HomePresenter presenter;
    
        @Before
        public void before() {
            repository = mock(Respository.class);
            presenter = new HomePresenter(repository);
        }
    
        //Your tests
    }
    
    0 讨论(0)
  • 2021-01-04 09:58

    I've created an issue there https://github.com/mockito/mockito/issues/392 Original answer with hotfix there https://stackoverflow.com/a/36642606/1224247

    0 讨论(0)
  • 2021-01-04 10:00

    You could try to replace

    MockitoAnnotations.initMocks(this);
    

    with this

    System.setProperty("dexmaker.dexcache", getContext().getCacheDir().getPath());
    

    It works for me. See ref here

    0 讨论(0)
提交回复
热议问题