繁体   English   中英

使用 Mockito 的 Android MVP 演示者单元测试导致“需要但未调用”错误

[英]Android MVP presenter unit test with Mockito causes “Wanted but not invoked” error

我知道之前有人问过这个问题,但我目前正在深入测试,我很难用 Mockito 以 MVP 模式对演示者进行单元测试

我的代码设置:
物品类别

public class ItemJSON {

   @SerializedName("title")
    String textHolder;

    @SerializedName("id")
    int factNumber;

    public ItemJSON(String factText, int factNumber) {
        this.textHolder = factText;
        this.factNumber = factNumber;
    }

   //getters and setters
}

承包商:

public interface Contractor {

    interface Presenter {
        void getPosts();
    }

    interface View {
     //parse data to recyclerview on Succesfull call. 
        void parseDataToRecyclerView(List<ItemJSON> listCall);

        void onResponseFailure(Throwable throwable);
    }

    interface Interactor {

        interface onGetPostsListener {
            void onSuccessGetPostCall(List<ItemJSON> listCall);
            void onFailure(Throwable t);
        }


        void getPosts(onGetPostsListener onGetPostsListener);

    }
}

API类:

 @GET("posts")
    Call<List<ItemJSON>> getPost();

交互类:

public class InteractorImpl implements Contractor.Interactor{

    @Override
    public void getPosts(onGetPostsListener onGetPostsListener) {
// NetworkService responsible for seting up Retrofit2
        NetworkService.getInstance().getJSONApi().getPost().enqueue(new Callback<List<ItemJSON>> () {
            @Override
            public void onResponse(@NonNull Call<List<ItemJSON>> call, @NonNull Response<List<ItemJSON>> response) {
                Log.d("OPERATION @GET","CALLBACK SUCCESSFUL");
                onGetPostsListener.onSuccessGetPostCall (response.body ());
            }

            @Override
            public void onFailure(@NonNull Call<List<ItemJSON>>call, @NonNull Throwable t) {
                Log.d("OPERATION @GET","CALLBACK FAILURE");
                onGetPostsListener.onFailure (t);
            }
        });
    }

主讲班:

public class PresenterImpl implements Contractor.Presenter, Contractor.Interactor.onGetPostsListener {

    private final Contractor.View view;
    private final Contractor.Interactor interactor;

    public PresenterImpl (Contractor.View view,Contractor.Interactor interactor){
        this.view = view;
        this.interactor = interactor;
    }

    @Override
    public void getPosts() {
            interactor.getPosts (this);
    }


@Override
public void onSuccessGetPostCall(List<ItemJSON> listCall) {
    view.parseDataToRecyclerView (listCall);
}
}

所以我尝试对演示者进行一些单元测试,但他们不断失败,我不断收到下一个错误

Wanted but not invoked Actually, there were zero interactions with this mock   

单元测试类:

@RunWith (MockitoJUnitRunner.class)
public class ApiMockTest{

    @Mock
    Contractor.View view;

    private PresenterImpl presenter;

    @Captor
    ArgumentCaptor<List<ItemJSON>> jsons;

    @Before
    public void setUp() {
        MockitoAnnotations.openMocks (this);
        presenter = new PresenterImpl (view,new InteractorImpl ());
    }

    @Test
    public void loadPost() {
        presenter.getPosts ();
        verify(view).parseDataToRecyclerView (jsons.capture ());
        Assert.assertEquals (2, jsons.capture ().size ());
    }
}

我试图了解我做错了什么以及如何解决这个问题,但至于现在我已经没有想法了。 我会感谢任何帮助。 提前致谢

UPD:在所有情况下,主活动演示者都会在 onClick 主活动类中被调用:

public class MainActivity extends AppCompatActivity implements Contractor.View {
public Contractor.Presenter presenter;
  @Override
    protected void onCreate(Bundle savedInstanceState) {
presenter = new PresenterImpl (this,new InteractorImpl ());
        binding.getButton.setOnClickListener(view ->presenter.getPosts () );
...//code

  @Override
    public void parseDataToRecyclerView(List<ItemJSON> listCall) {
        adapter.updateList(listCall); //diff call to put data into recyclerview adapter
    }
}
}

我也遇到了这种情况,甚至使用了 mockk 库。 问题是你的方法是一个接口方法。 您需要从实现此接口的视图中实际调用它。

暂无
暂无

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

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