【问题标题】:How do I trap a function inputs using powermock?如何使用 powermock 捕获函数输入?
【发布时间】:2016-11-10 16:41:11
【问题描述】:

假设我有如下测试功能:

boolean MyFunction (String input1, Text rowkey) {
   int a = 10;
   a = a + 10;
   return context.write(rowkey,a);
}

请注意,context.write 是一个写入数据库的函数。

我想模拟该函数并检查传递给它的输入是否正确。我该怎么做?

基本上,我可以执行以下操作(我似乎无法开始工作):

   PowerMockito.when(Context.write((Text) anyObject(),
  (int) anyObject())).then(compareResult(input1,input2));

 private Answer<Boolean> compareResults(input1, input2) {
     AssertTrue(input1,this.Test1Input1AcceptanceCriteria)
     AssertTrue(input2,this.Test1Input2AcceptanceCriteria)
 }

【问题讨论】:

    标签: dictionary mapreduce hbase powermock powermockito


    【解决方案1】:

    你不应该这样做!

    假设 context 是您的封闭类的 field,那么您“简单地”必须找到一种方法来提供 mocked 版本将这样一个 context 对象添加到您的测试类中。

    这里的典型方式:依赖注入

    喜欢:

    public class Foo {
       private final Context context;
    
       // some external constructor that you would use to create Foo objects
       public Foo() { this (new Context(...)); }
    
       // an internall ctor, used by your unit tests
       Foo(Context context) { this.context = context;  }
    

    然后,您可以编写单元测试,例如

    @Test
    public void testMyFunction() {
      Context context = ... create some mock
      Foo underTest = new Foo(context);
      underTest.myFunction(...
    

    使用上述方法,您对 Power 模拟的全部需求都消失了。您可以使用 Mokito 或 EasyMock 等“普通”模拟框架来准备/验证您的上下文对象!

    你看,最后你的问题是你创建的代码只是难以测试,除非你开始考虑依赖注入。如果您对测试很认真,请观看这​​些videos;它们向您广泛介绍了如何实际创建可测试的代码。

    【讨论】:

      【解决方案2】:

      这是我自己制定的解决方案,并且可以根据需要完美运行。在没有 mrunit 帮助的情况下,我也使用这种技术有效地测试了我所有的 map-reduce 代码。

      @Test
      public void testMyFunction () throws Exception {
         Context testContext = mock(Context.class);
         MyFunction (input1, rowkey); // Call the function under test
         // Now here is the magic. We use the verify technique to test 
         // the mocked class method while checking for equality with the acceptance criteria.
         // In essence, the key is trap these underlying functions and ensure that
         // they are passed in the right input from your function under test.
         int expected_input_to_write = 10
         Mockito.verify(testContext).write(eq((Object) new Text("test_string")), eq((Object) expected_input_to_write )); 
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-08-17
        • 2021-09-30
        • 1970-01-01
        • 1970-01-01
        • 2017-04-12
        相关资源
        最近更新 更多