Issue
Added @VisibleForTesting
and protected. My test can now this method:
@VisibleForTesting
protected void setupDataBinding(List<Recipe> recipeList) {
recipeAdapter = new RecipeAdapter(recipeList);
RecyclerView.LayoutManager layoutManager
= new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
rvRecipeList.setLayoutManager(layoutManager);
rvRecipeList.setAdapter(recipeAdapter);
}
Updated the test case using spy object: However, the real setupDataBinding(recipe) is getting called even when I created a mock of he spy that will get called. Maybe I am doing this wrong.
@Test
public void testShouldGetAllRecipes() {
RecipeListView spy = Mockito.spy(fragment);
doNothing().when(spy).setupDataBinding(recipe);
fragment.displayRecipeData(recipe);
verify(recipeItemClickListener, times(1)).onRecipeItemClick();
}
I am trying to test the methods in my Fragment
class as below. However, I am trying to mock out the methods to verify that the methods are called the correct number of times. However, the problem is I have a private
method setupDataBinding(...)
that setups on the RecyclerView
that is called from displayRecipeData(...)
. I want to mock these calls as I don't want to call the real object on the RecyclerView
. I just want to verify that setupDataBinding(...)
gets called.
I have tried using spy and VisibleForTesting
, but still not sure how to do this.
I am trying to test the Fragment in isolation.
public class RecipeListView
extends MvpFragment<RecipeListViewContract, RecipeListPresenterImp>
implements RecipeListViewContract {
@VisibleForTesting
private void setupDataBinding(List<Recipe> recipeList) {
recipeAdapter = new RecipeAdapter(recipeList);
RecyclerView.LayoutManager layoutManager
= new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
rvRecipeList.setLayoutManager(layoutManager);
rvRecipeList.setAdapter(recipeAdapter);
}
@Override
public void displayRecipeData(List<Recipe> recipeList) {
/* Verify this get called only once */
setupDataBinding(recipeList);
recipeItemListener.onRecipeItem();
}
}
This is how I am testing. I have added the VisibleForTesting
thinking I could help. And I have tried using the spy.
public class RecipeListViewTest {
private RecipeListView fragment;
@Mock RecipeListPresenterContract presenter;
@Mock RecipeItemListener recipeItemListener;
@Mock List<Recipe> recipe;
@Before
public void setup() {
MockitoAnnotations.initMocks(RecipeListViewTest.this);
fragment = RecipeListView.newInstance();
}
@Test
public void testShouldGetAllRecipes() {
fragment.displayRecipeData(recipe);
RecipeListView spy = Mockito.spy(fragment);
verify(recipeItemListener, times(1)).onRecipeItem();
}
}
What would be the best way to test the above in isolation?
Many thanks for any advice.
Solution
to prevent real method being called use: Mockito.doNothing().when(spy).onRecipeItem();
here you have minimum sample how to use it:
public class ExampleUnitTest {
@Test
public void testSpyObject() throws Exception {
SpyTestObject spyTestObject = new SpyTestObject();
SpyTestObject spy = Mockito.spy(spyTestObject);
Mockito.doNothing().when(spy).methodB();
spy.methodA();
Mockito.verify(spy).methodB();
}
public class SpyTestObject {
public void methodA() {
methodB();
}
public void methodB() {
throw new RuntimeException();
}
}
}
Answered By - RadekJ
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.