本文主要是介绍PowerMock测试 mock hbase连接,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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);@Beforepublic 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);}@Afterpublic void after() throws Exception{}/*** * Method: * */@Testpublic 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 thanthe other side of a maven generation; i.e. running in anide. You'll want to set this boolean to true to avoidseeing the RuntimException complaint: "hbase-default.xml fileseems to be for and old version of HBase (0.92.1), thisversion is X.X.X-SNAPSHOT"</description></property>
这篇关于PowerMock测试 mock hbase连接的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!