莫米托输出"wanted but not invoked, Actually there were zero interactions with this mock"



我遇到此错误:

Wanted but not invoked:
view.showPlayerActivity(null);
Actually, there were zero interactions with this mock.

我有此代码:

@RunWith(PowerMockRunner.class)
@PrepareForTest(Log.class)
public class MyDriverPresenterTest  {
    @Mock
    private MyDriveRepository mydriveRepository;
    private MyDrivePresenter drivePresenter;
    private Drive drive;
    private List<Drive> driveList;
    @Before
    public void setUpMyRecrdingsPresenter(){
        PowerMockito.mockStatic(Log.class);
        MockitoAnnotations.initMocks(this);
        drivePresenter = new MyDrivePresenter();
        drive = new Drive("Roar",false,1521708960,11);
        driveList.add(drive);
    }
    @Test
    public void testDriveClicked(){
        Uri uri=mydriveRepository.
        getMyDriveItemSelectedPathUri(drive.toFile().getName());
        verify(view).showPlayerActivity(uri);       
    }
}

类MyDrivePresenter具有方法

@Override
    public void onDriveClicked(Drive drive) {
        Uri uri = mydriveRepository.getMyDriveItemSelectedPathUri(drive.toFile().getName());
        Log.i("TAG", "onRecordingClicked: "+uri);
        view.showPlayerActivity(uri);
    }

和mydriverepository具有方法

public Uri getMyDriveItemSelectedPathUri(String name){
        Uri pathUri;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            pathUri = FileProvider.getUriForFile(context,"com.mypackagename",new File(getUserMixDir(),name));
        }else{
            pathUri = Uri.parse("file://"+new File(getUserMixDir(),name));
        }
        return pathUri;
    }

mydriverepository.getMyDriveIteMselectedPathuri一直使我归还我。我尝试查看以下链接https://stackoverflow.com/questions/20551926/exception-mockito-wanted-but-not-invoked-actually-there-were-zero-interaction?rq=1,但找不到解决方案的解决方案。

看起来您在嘲笑自己的存储库,因此您需要为演示者调用存储库方法时提供行为:

when(mydriveRepository.getMyDriveItemSelectedPathUri(anyString())).thenReturn(mockedUri);

,您的测试看起来会如下:

@Mock
MyDriveRepository mydriveRepository;
@Mock
Uri mockedUri;

@Test
public void testDriveClicked() {
    String filePath = "aPAth";
    presenter.onDriveClicked(drive);
    Uri uri = mydriveRepository.getMyDriveItemSelectedPathUri(filePath);
    verify(view).showPlayerActivity(uri);
}

基本上,在这里您可以测试演示者是否使用伪生成的URI调用该视图方法。当主持人在存储库上调用getMyDriveIteMselectedPathuri方法时,您正在告诉框架(Mockito(返回模拟的URI。然后,当您测试主持人是否调用了视图中的方法时,真正的测试会出现。

相关内容

  • 没有找到相关文章

最新更新