使用 Mockito 隔离测试 Fragment 类



添加了@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(recipe)也会被调用。也许我做错了。

@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(...)该方法可以在从displayRecipeData(...)调用的RecyclerView上进行设置。我想嘲笑这些调用,因为我不想在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();
}
}

单独测试上述内容的最佳方法是什么?

非常感谢您的任何建议。

为了防止调用真正的方法,请使用:Mockito.doNothing().when(spy).onRecipeItem();

这里有如何使用它的最小示例:

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

}

我想

嘲笑这些调用,因为我不想在RecyclerView上调用真正的对象。我只是想验证一下,setupDataBinding()被召唤了。

您还没有创建足够的接缝来执行此操作。

如果声明一个描述"设置数据绑定"将如何发生的协定,该怎么办?换句话说,如果使用方法void setupDataBinding(...)创建接口怎么办?然后RecipeListView会将该接口的实例作为依赖项保存。因此,RecipeListView永远不会知道这种设置将如何发生:它知道一件事 - 他持有的依赖关系已经"签署了合同"并承担了执行工作的责任。

通常,您会通过构造函数传递该依赖项,但因为Fragment是特定情况,您可以在onAttach()中获取依赖项:

interface Setupper {
void setupDataBinding(List<Recipe> recipes, ...);
}
class RecipeListView extends ... {
Setupper setupper;
@Override public void onAttach(Context context) {
super.onAttach(context);
// Better let the Dependency Injection tool (e.g. Dagger) provide the `Setupper`
// Or initialize it here (which is not recommended)
Setupper temp = ...
initSetupper(temp);
}
void initSetupper(Setupper setupper) {
this.setupper = setupper;
}
@Override
public void displayRecipeData(List<Recipe> recipes) {
// `RecipeListView` doesn't know what exactly `Setupper` does
// it just delegates the work
setupper.setupDataBinding(recipes, ...);
recipeItemListener.onRecipeItem();
}
}

这对你有什么好处?现在你有一个接缝。现在你依赖于实现,你依赖于合同。

public class RecipeListViewTest {
@Mock Setupper setupper;
List<Recipe> recipe = ...; // initialize, no need to mock it
...
private RecipeListView fragment;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
fragment = new RecipeListView();
fragment.initSetupper(setupper);
}
@Test
public void testShouldGetAllRecipes() {
fragment.displayRecipeData(recipes);
// You do not care what happens behind this call
// The only thing you care - is to test whether is has been executed
verify(setupper).setupDataBinding(recipe, ...);
// verify(..) is the same as verify(.., times(1))
}
}

我强烈建议Misko Hevery的"编写可测试代码"一书,该书通过示例和简洁的方式说明了所有技术(38页)。

有一个常见的经验法则说:测试单元的功能比测试它如何做要好得多。

考虑到这一点,问自己一个问题——我为什么要首先嘲笑setupDataBinding方法?它不进行任何外部调用,它只更改对象的状态。因此,测试此代码的更好方法是检查它是否以正确的方式更改状态:

@Test
public void testShouldGetAllRecipes() {
fragment.displayRecipeData(recipeList);
// Verifies whether RecipeAdapter has been initialized correctly
RecipeAdapter recipeAdapter = fragment.getRecipeAdapter();
assertNotNull(recipeAdapter);
assertSame(recipeList, recipeAdapter.getRecipeList());
// Verifies whethr RvRecipeList has been initialized correctly 
RvRecipeList rvRecipeList = fragment.getRvRecipeList();
assertNotNull(rvRecipeList);
assertNotNull(rvRecipeList.getLayoutManager());
assertSame(fragment.getRecipeAdapter(), rvRecipeList.getAdapter());
}

这可能需要添加几个 getter/setter,以使整个事情更具可测试性。

相关内容

  • 没有找到相关文章

最新更新