简体   繁体   English

验证是否在 Android Studio 测试(JUnit 和 mockito)中调用视图方法

[英]Verifying whether a view method is called or not in Android Studio Testing (JUnit and mockito)

Sorry for silly questions, I am new to Testing in Android studio and not able to find solutions online.抱歉,愚蠢的问题,我是在 Android 工作室进行测试的新手,无法在线找到解决方案。 Well I am trying to verify that a method in view is called whenever the method in presenter is called or not.好吧,我正在尝试验证是否在调用演示者中的方法时调用视图中的方法。

    @Override
    public void chooseImage() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        view.galleryImageSelected(intent);
    }

The above snippet is a method in my Presenter Class, and as you can see, I am calling a interface method of view from this function.上面的代码片段是我的Presenter Class中的一个方法,如你所见,我正在从这个function调用视图的接口方法。 Now I have to verify/test that this snippet works perfectly.现在我必须验证/测试这个片段是否完美运行。

@RunWith(MockitoJUnitRunner.class)
public class ExampleUnitTest {

    @Mock
    private File storageDir;
    @Mock
    private PackageManager pManager;
    @Mock
    private Context appContext;
    @Mock
    private MainActivityContract.View view;
    @InjectMocks
    private MainActivityPresenter presenter;
    @Captor
    private ArgumentCaptor<Intent> captor;

    @Before
    public void setUp() throws Exception {
        presenter = new MainActivityPresenter(storageDir, pManager, appContext, view);
    }

    @Test
    public void shouldCallIntentWhenClickedOnCameraClickButton() throws IOException {

        presenter.chooseImage();
        verify(view).galleryImageSelected(captor.capture());
        Intent intent = captor.getValue();
        String action = intent.getAction();
        String type = intent.getType();

        assertThat(action, is(Intent.ACTION_GET_CONTENT));
        assertThat(type, is("image/*"));
    }

}

Now to verify view's method, I have to pass the intent in galleryImageSelected() method, but as intent was a local variable, how do I test this?现在要验证视图的方法,我必须在 galleryImageSelected() 方法中传递意图,但由于意图是局部变量,我该如何测试呢? Do I have to mock the variable intent and then pass, and if yes how to mock an intent variable.我是否必须模拟变量意图然后通过,如果是,如何模拟意图变量。

Also if I am doing the following thing:另外,如果我正在做以下事情:

   @Test
    public void shouldCallIntentWhenClickedOnCameraClickButton() throws IOException {
        presenter.chooseImage();
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        verify(view).galleryImageSelected(intent);
    }

I am getting the following error while testing:我在测试时收到以下错误:

Argument(s) are different! Wanted:
view.galleryImageSelected(null);
-> at com.example.imagepicker.ExampleUnitTest.shouldCallIntentWhenClickedOnCameraClickButton(ExampleUnitTest.java:52)
Actual invocation has different arguments:

You cannot mock local variable but you can capture the argument passed, using ArgumentCaptor and then validate argument as -您不能模拟局部变量,但您可以使用ArgumentCaptor捕获传递的参数,然后将参数验证为 -

@RunWith(MockitoJUnitRunner.class)
public class PresenterTest {

  @Mock
  private View view;
  @InjectMocks
  private Presenter presenter;

  @Captor
  private ArgumentCaptor<Intent> captor;

  @Test
  public void shouldCallIntentWhenClickedOnCameraClickButton() {
    presenter.chooseImage();
    verify(view).galleryImageSelected(captor.capture());
    Intent intent = captor.getValue();
    String action = intent.getAction();
    String type = intent.getType();
    assertThat(action, is(Intent.ACTION_GET_CONTENT));
    assertThat(type, is("image/*"));
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM