设为首页 加入收藏

TOP

PowerMock测试 mock hbase连接
2019-04-14 13:51:42 】 浏览:21
Tags:PowerMock 测试 mock hbase 连接
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010030022/article/details/78843277

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("co
		    

m.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>

编程开发网
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Hbase   MapReduce例子 下一篇HBase之——协处理编程

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

array(4) { ["type"]=> int(8) ["message"]=> string(24) "Undefined variable: jobs" ["file"]=> string(32) "/mnt/wp/cppentry/do/bencandy.php" ["line"]=> int(217) }