junit测试中,当无法连接hbase集群,使用PowerMock测试 mock hbase连接,mock类如下:
/**
* QueryCanTask Tester.
*
* @author <Authors name>
* @since 12/18/2017
* @version 1.0
*/
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class) //委派给SpringJUnit4ClassRunner
@PowerMockIgnore("javax.management.*")
@PrepareForTest({ HBaseConfiguration.class, ConnectionFactory.class, Configuration.class,
Table.class, ResultScanner.class})
//@SuppressStaticInitializationFor("com.xxxx")//阻止静态代码块运行
@ContextConfiguration(locations = { "classpath:applicationContext/*.xml" })
public class QueryCanTaskTest
{
private Configuration configuration = PowerMockito.mock(Configuration.class);
private Connection connection = PowerMockito.mock(Connection.class);
private Table table = PowerMockito.mock(Table.class);
@Before
public void before() throws Exception
{
//初始化静态变量
new SystemCfgConstants();
PowerMockito.when(HBaseConfiguration.create()).thenReturn(configuration);
PowerMockito.when(ConnectionFactory.createConnection(configuration)).thenReturn(connection);
PowerMockito.when(Connections.getTable(TableInfo.TABLE)).thenReturn(table);
PowerMockito.when(Connections.getTable(RawDataTable.tableName)).thenReturn(table);
PowerMockito.when(table.getScanner(scan)).thenReturn(yourest);
}
@After
public void after() throws Exception
{
}
/**
*
* Method:
*
*/
@Test
public void testQueryList() throws Exception
{
//TODO test
}
}
在hbase-site.xml添加如下配置,跳过版本检查
<property>
<name>hbase.defaults.for.version.skip</name>
<value>true</value>
<description>
Set to true to skip the 'hbase.defaults.for.version' check.
Setting this to true can be useful in contexts other than
the other side of a maven generation; i.e. running in an
ide. You'll want to set this boolean to true to avoid
seeing the RuntimException complaint: "hbase-default.xml file
seems to be for and old version of HBase (0.92.1), this
version is X.X.X-SNAPSHOT"
</description>
</property>