How to skip real call when doing unit test for mapreduce?

I am writing the unit test for the map reduce program, during the setup() in reduce, it has some context and configuration and htable operations, like this:

protected void setup(Context context) {
  try {
    Configuration c = context.getConfiguration();
    table =
        new HTable(c,"TableName");
    table.setAutoFlushTo(false);
  } catch (Exception e) {
    System.out.printly(e.toString());
  }
}

I've tried to use MRUnit, PowerMock(with Mockito) to mock the conext and configuration, but it seems not work.

unit test code:

Reduce.Context c = reducedriver.getContext();
Configuration confReduce = reducedriver.getConfiguration();

confReduce.setStrings("io.serializations",
    confReduce.get("io.serializations"),
    MutationSerialization.class.getName(),
    ResultSerialization.class.getName(),
    KeyValueSerialization.class.getName());

when(c.getConfiguration()).thenReturn(confReduce);

When I run the Unit Test, the context.getConfiguration() seems still try to connect the real hadoop environment and generates lots of zookeeper and connections errors.

How can I fix this problem? thanks.

链接地址: http://www.djcxy.com/p/67710.html

上一篇: 如何单元测试Hadoop MapReduce?

下一篇: 如何在mapreduce进行单元测试时跳过真正的调用?