Friday 23 July 2021

Android MVP presenter unit test with Mockito causes "Wanted but not invoked" error

I know it was asked before, but i am currently diving into testing and i have the struggle to unit test presenter in MVP pattern with Mockito

My code setup:
Item class

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
}

Contractor:

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 class:

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

Interactor class:

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);
            }
        });
    }

Presenter class:

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);
}
}

So i try to ran some unit test on presenter, but they constanlty fail and i keep getting next error

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

Unit test class:

@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 ());
    }
}

I try to understand what i am doing wrong and how to fix this issue, but as for now i am ran out of ideas. I will aprecciate any help. Thanks in the adavance

UPD: in all cases in main activity presenter get called in onClick Main Activity class:

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
    }
}
}


from Android MVP presenter unit test with Mockito causes "Wanted but not invoked" error

No comments:

Post a Comment