का उपयोग करके अलगाव में एक टुकड़ा वर्ग का परीक्षण @VisibleForTesting
और संरक्षित। मेरा परीक्षण अब कर सकते हैं इस विधि:मॉकिटो
@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);
}
अपडेट किया गया परीक्षण का मामला जासूस वस्तु का उपयोग कर: हालांकि, वास्तविक setupDataBinding (नुस्खा) भी कहा जाता हो रही है जब मैं वह की एक नकली बनाया जासूसी कहा जाता हो जाएगा कि। शायद मैं यह गलत कर रहा हूँ।
@Test
public void testShouldGetAllRecipes() {
RecipeListView spy = Mockito.spy(fragment);
doNothing().when(spy).setupDataBinding(recipe);
fragment.displayRecipeData(recipe);
verify(recipeItemClickListener, times(1)).onRecipeItemClick();
}
मैं नीचे के रूप में मेरी Fragment
कक्षा में तरीकों का परीक्षण करने की कोशिश कर रहा हूँ। हालांकि, मैं यह सत्यापित करने के तरीकों का नकल करने की कोशिश कर रहा हूं कि विधियों को सही संख्या कहा जाता है। हालांकि, समस्या यह है कि मेरे पास private
विधि setupDataBinding(...)
है जो RecyclerView
पर सेट है जिसे displayRecipeData(...)
से कहा जाता है। मैं इन कॉलों को मॉक करना चाहता हूं क्योंकि मैं RecyclerView
पर असली ऑब्जेक्ट को कॉल नहीं करना चाहता हूं। मैं बस सत्यापित करना चाहता हूं कि setupDataBinding(...)
कॉल हो जाता है।
मैंने जासूसी और VisibleForTesting
का उपयोग करने का प्रयास किया है, लेकिन अभी भी यह सुनिश्चित नहीं है कि यह कैसे करें।
मैं अलगाव में फ्रैगमेंट का परीक्षण करने की कोशिश कर रहा हूं।
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();
}
}
इस प्रकार मैं परीक्षण कर रहा हूं। मैंने VisibleForTesting
को सोचा है कि मैं मदद कर सकता हूं। और मैंने जासूसी का उपयोग करने की कोशिश की है।
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();
}
}
अलगाव में उपर्युक्त परीक्षण करने का सबसे अच्छा तरीका क्या होगा?
किसी भी सलाह के लिए बहुत धन्यवाद।
'VisibleForTesting' जोड़ना पर्याप्त नहीं है। आपको संरक्षित, पैकेज-निजी या सार्वजनिक के लिए 'setupDataBinding (...)' के लिए एक्सेस संशोधक को भी बदलना होगा। – liminal
@ प्रारंभिक मैंने अपने प्रश्नों को अपने नवीनतम प्रयासों के साथ अपडेट किया है। मैं असली विधि को बिंग से रोकने में असफल रहा हूं, भले ही मैंने इसका एक जासूसी वस्तु बनाया हो। – ant2009