HBase的安装和部署在这里就不赘述了,一般情况下是不需要对HBase进行mock测试,由于官方对此没有做过多介绍,所以和大家分享一下。
Maven依赖
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>${hbase.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-shaded-testing-util</artifactId>
<version>${hbase.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-zookeeper</artifactId>
<version>${hbase.version}</version>
<scope>provided</scope>
<type>test-jar</type>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-common</artifactId>
<version>${hbase.version}</version>
<scope>provided</scope>
<type>test-jar</type>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-testing-util</artifactId>
<version>${hbase.version}</version>
<scope>test</scope>
</dependency>
Mock测试示例
public class HBaseMockTest {
private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
private static final String NAMESPACE = "TEST";
private static final TableName TABLE_NAME =
TableName.valueOf(TestMetaRepair.class.getSimpleName());
private static final byte[] family = Bytes.toBytes("test");
private Table table;
@BeforeClass
public static void beforeClass() throws Exception {
TEST_UTIL.startMiniCluster(3);
}
@AfterClass
public static void afterClass() throws Exception {
TEST_UTIL.shutdownMiniCluster();
}
@Before
public void setup() throws Exception {
table = TEST_UTIL.createMultiRegionTable(TABLE_NAME, family, 5);
TEST_UTIL.waitUntilAllRegionsAssigned(TABLE_NAME);
}
@After
public void tearDown() throws Exception {
TEST_UTIL.deleteTable(TABLE_NAME);
}
@Test
public void test() throws Exception {
// table.put(...);
}
}