gpt4 book ai didi

android - Mockito 输出 "wanted but not invoked, Actually there were zero interactions with this mock"

转载 作者:行者123 更新时间:2023-11-29 18:58:17 24 4
gpt4 key购买 nike

我收到这个错误:

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 一直返回 null。我试着查看以下链接 https://stackoverflow.com/questions/20551926/exception-mockito-wanted-but-not-invoked-actually-there-were-zero-interaction?rq=1 但无法真正找到解决我的问题的方法。

最佳答案

看起来你在 mock 你的存储库,所以你需要为演示者调用存储库方法时提供一个行为:

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 调用该 View 方法。当演示者调用存储库上的 getMyDriveItemSelectedPathUri 方法时,您告诉框架 (mockito) 返回模拟的 URI。然后,当您测试 View 中的方法是否被演示者调用时,真正的测试就来了。

关于android - Mockito 输出 "wanted but not invoked, Actually there were zero interactions with this mock",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49518892/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com