unit-testing - HBase 应用程序 : Unit testing by mocking the HBase

标签 unit-testing junit hbase mockito powermock

我的应用程序中有一个从 HBase 访问数据的方法。它使用scan方法查询hbase。我想编写单元测试用例来测试这个功能。所以我想模拟 hbase 调用。怎么做?我正在使用 Mockit 进行模拟。

最佳答案

如果您使用的是 Mockito,则可以 stub 您的类以使它们返回您想要的内容。

假设您有一个名为 HBaseHelper 的类和一个名为 getData() 的方法,该方法使用扫描仪从 hbase 检索数据。现在假设您在另一个类中有另一个名为 useData() 的方法:

public String useData() {
  String data = hbaseHelper.getData();

  // ... Do things with data
  return data;
}

如果您使用的是 Mockito,您可以在测试中有效地执行类似的操作以返回虚拟“数据”并测试使用此数据的方法:

import org.mockito.Mock;
import org.mockito.Mockito.when;

@Mock
HBaseHelper hbaseHelper;

@Test
public void testFoo() {
  when(hbaseHelper.getData()).thenReturn("hello world");

  assertThat(useData()).equals("hello world");
}

关于unit-testing - HBase 应用程序 : Unit testing by mocking the HBase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24897408/

相关文章:

java - 如何在 Junit 中测试 ScheduledExecutorService 异常处理?

java - 完整性检查 - 使用 JUNIT 时对象数量显着增加

google-cloud-platform - 如何在 Hbase Row 和 Bigtable Row 上设置 TTL

hadoop - Mapreduce到hbase输出卡在 map 上的比例降低100%

java - 无法启动 HBase shell

node.js - 带有 proxyquire 和 sinon 的 google-geocoder

unit-testing - 测试驱动开发与测试优先开发

c# - Visual Studio 单元测试安全异常

java - 在类路径 :/cucumberOptions 中找不到任何功能

c# - Xamarin Forms 的 FreshMvvm FreshPageModelResolver.ResolvePageModel 用于单元测试