<i id='hVRXY'><tr id='hVRXY'><dt id='hVRXY'><q id='hVRXY'><span id='hVRXY'><b id='hVRXY'><form id='hVRXY'><ins id='hVRXY'></ins><ul id='hVRXY'></ul><sub id='hVRXY'></sub></form><legend id='hVRXY'></legend><bdo id='hVRXY'><pre id='hVRXY'><center id='hVRXY'></center></pre></bdo></b><th id='hVRXY'></th></span></q></dt></tr></i><div id='hVRXY'><tfoot id='hVRXY'></tfoot><dl id='hVRXY'><fieldset id='hVRXY'></fieldset></dl></div>

    <legend id='hVRXY'><style id='hVRXY'><dir id='hVRXY'><q id='hVRXY'></q></dir></style></legend>
      <tfoot id='hVRXY'></tfoot>

      • <bdo id='hVRXY'></bdo><ul id='hVRXY'></ul>

      <small id='hVRXY'></small><noframes id='hVRXY'>

      1. 在 AndroidTestCase 中调用 MockitoAnnotations.initMocks() 时

        时间:2023-09-25
          <bdo id='ObgGG'></bdo><ul id='ObgGG'></ul>
          <tfoot id='ObgGG'></tfoot>
            <tbody id='ObgGG'></tbody>

          1. <i id='ObgGG'><tr id='ObgGG'><dt id='ObgGG'><q id='ObgGG'><span id='ObgGG'><b id='ObgGG'><form id='ObgGG'><ins id='ObgGG'></ins><ul id='ObgGG'></ul><sub id='ObgGG'></sub></form><legend id='ObgGG'></legend><bdo id='ObgGG'><pre id='ObgGG'><center id='ObgGG'></center></pre></bdo></b><th id='ObgGG'></th></span></q></dt></tr></i><div id='ObgGG'><tfoot id='ObgGG'></tfoot><dl id='ObgGG'><fieldset id='ObgGG'></fieldset></dl></div>

                1. <small id='ObgGG'></small><noframes id='ObgGG'>

                  <legend id='ObgGG'><style id='ObgGG'><dir id='ObgGG'><q id='ObgGG'></q></dir></style></legend>

                  本文介绍了在 AndroidTestCase 中调用 MockitoAnnotations.initMocks() 时的 NPE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  尝试在我的 AndroidTestCase 中使用 mockito.我将依赖项添加到 build.gradle:

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

                  final DEXMAKER_VERSION = '1.2'
                  
                  dependencies {
                      // ...
                      androidTestCompile "com.google.dexmaker:dexmaker:${DEXMAKER_VERSION}"
                      androidTestCompile "com.google.dexmaker:dexmaker-mockito:${DEXMAKER_VERSION}"
                      androidTestCompile 'org.mockito:mockito-core:1.10.19'
                  
                  }
                  

                  带有 mockito 初始化的 TestCase:

                  The TestCase with the mockito initialization:

                  public class UsersListPresenterTest extends AndroidTestCase {
                  
                      public void setUp() throws Exception {
                          MockitoAnnotations.initMocks(this);
                      }
                  
                      public void testInitialize() throws Exception {
                  
                      }
                  }
                  

                  但是,一旦我向类添加任何属性,甚至在添加任何注释之前,测试就会开始崩溃:

                  But as soon as I add any attribute to the class, even before adding any annotation the test start to crash:

                  public class UsersListPresenterTest extends AndroidTestCase {
                  
                      String mockString;
                  
                      public void setUp() throws Exception {
                          MockitoAnnotations.initMocks(this);
                      }
                  
                      public void testInitialize() throws Exception {
                  
                      }
                  }
                  

                  使用以下堆栈跟踪

                  java.lang.NullPointerException: Attempt to invoke virtual method 
                      'java.lang.Class java.lang.Object.getClass()' on a null object reference
                  at com.google.dexmaker.mockito.DexmakerMockMaker.getInvocationHandlerAdapter(DexmakerMockMaker.java:80)
                  at com.google.dexmaker.mockito.DexmakerMockMaker.getHandler(DexmakerMockMaker.java:75)
                  at org.mockito.internal.util.MockUtil.isMockitoMock(MockUtil.java:74)
                  at org.mockito.internal.util.MockUtil.isMock(MockUtil.java:66)
                  at org.mockito.internal.configuration.injection.scanner.MockScanner.isMockOrSpy(MockScanner.java:86)
                  at org.mockito.internal.configuration.injection.scanner.MockScanner.preparedMock(MockScanner.java:72)
                  at org.mockito.internal.configuration.injection.scanner.MockScanner.scan(MockScanner.java:61)
                  at org.mockito.internal.configuration.injection.scanner.MockScanner.addPreparedMocks(MockScanner.java:47)
                  at org.mockito.internal.configuration.InjectingAnnotationEngine.injectMocks(InjectingAnnotationEngine.java:96)
                  at org.mockito.internal.configuration.InjectingAnnotationEngine.processInjectMocks(InjectingAnnotationEngine.java:62)
                  at org.mockito.internal.configuration.InjectingAnnotationEngine.process(InjectingAnnotationEngine.java:56)
                  at org.mockito.MockitoAnnotations.initMocks(MockitoAnnotations.java:108)
                  at com.myproject.presentation.UsersListPresenterTest.setUp(UsersListPresenterTest.java:28)
                  at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
                  at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
                  at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
                  at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1853)
                  

                  我做错了什么?

                  推荐答案

                  你可以试试替换

                  MockitoAnnotations.initMocks(this);
                  

                  有了这个

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

                  它对我有用.在此处查看参考

                  这篇关于在 AndroidTestCase 中调用 MockitoAnnotations.initMocks() 时的 NPE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:mockito 有与 jMock 的 States 等效的成语吗? 下一篇:如何使用 Mockito 或任何其他相关的 Java 框架来模

                  相关文章

                  最新文章

                  <legend id='e6iMt'><style id='e6iMt'><dir id='e6iMt'><q id='e6iMt'></q></dir></style></legend>

                2. <small id='e6iMt'></small><noframes id='e6iMt'>

                    <i id='e6iMt'><tr id='e6iMt'><dt id='e6iMt'><q id='e6iMt'><span id='e6iMt'><b id='e6iMt'><form id='e6iMt'><ins id='e6iMt'></ins><ul id='e6iMt'></ul><sub id='e6iMt'></sub></form><legend id='e6iMt'></legend><bdo id='e6iMt'><pre id='e6iMt'><center id='e6iMt'></center></pre></bdo></b><th id='e6iMt'></th></span></q></dt></tr></i><div id='e6iMt'><tfoot id='e6iMt'></tfoot><dl id='e6iMt'><fieldset id='e6iMt'></fieldset></dl></div>
                  1. <tfoot id='e6iMt'></tfoot>

                    • <bdo id='e6iMt'></bdo><ul id='e6iMt'></ul>