代码之家  ›  专栏  ›  技术社区  ›  Iker Jimenez

EasyMock:Void方法

  •  66
  • Iker Jimenez  · 技术社区  · 17 年前

    这个类很大,我只使用它的一个方法。 我需要为测试替换此方法的实现,因为我希望它做一些不同的事情,并且我需要能够访问此方法接收的参数。

    我找不到一种方法来做这件事 EasyMock . 我想我知道怎么用它 Mockito doAnswer 但除非绝对必要,否则我不想再添加一个库。

    5 回复  |  直到 9 年前
        1
  •  93
  •   Community Mohan Dere    6 年前

    如果我正确理解您想要做的事情,您应该能够使用 andAnswer() :

    mockObject.someMethod(eq(param1), eq(param2));
    expectLastCall().andAnswer(new IAnswer() {
        public Object answer() {
            //supply your mock implementation here...
            SomeClass arg1 = (SomeClass) getCurrentArguments()[0];
            AnotherClass arg2 = (AnotherClass) getCurrentArguments()[1];
            arg1.doSomething(blah);
            //return the value to be returned by the method (null for void)
            return null;
        }
    });
    

    EasyMock User Guide 解释:

    创建返回值或异常

    有时,我们希望我们的模拟对象返回一个值或抛出一个在实际调用时创建的异常。自EasyMock 2.2以来,返回的对象 expectLastCall() expect(T value) 提供方法 andAnswer(IAnswer answer) 允许[您]指定接口的实现 IAnswer

    安斯韦尔 回调时,传递给模拟调用的参数可通过 EasyMock.getCurrentArguments() . 如果您使用这些,像重新排序参数这样的重构可能会破坏您的测试。你已经被警告了。

        2
  •  23
  •   Community Mohan Dere    6 年前

    如果每次都要调用void方法,然后调用 EasyMock.expectLastCall() replay() ,Easymock将记住每次调用。

    所以我认为您不需要显式地调用 expect() (除 lastCall )因为您不希望从void方法得到任何东西,除了它的调用。

    谢谢你,克里斯!

    “Fun With EasyMock” 由其他StackOverflow用户提供 Burt Beckwith

    基本上,我倾向于使用的流程是:

    1. 创建一个模拟
    2. expect(mock.[method call]).andReturn([result])
    3. 呼叫 mock.[method call] 然后 对于每个预期的无效调用
    4. 呼叫 replay(mock) 从录制模式切换到播放模式
    5. 根据需要注入模拟
    6. 呼叫 verify(mock) 确保所有预期的呼叫都已发生
        3
  •  5
  •   piepera    14 年前

    Captures 类,这是EasyMock 2.4的新特性。

    Capture<ChartPanel> captured = new Capture<ChartPanel>();
    // setChartPanel is going to be called during execution;
    // we want to verify some things about the ChartPanel
    // instance it's invoked with
    chartMock.setChartPanel(capture(captured));
    replay(chartMock);
    
    ufdm.setChartAnnotater(chartMock);
    // afterPropertiesSet triggers the setChartPanel call...
    ufdm.afterPropertiesSet();
    verify(chartMock);
    
    // verify some things about the ChartPanel parameter our
    // mock object was invoked with
    assertSame(plot, captured.getValue().getChart().getPlot());
    
        4
  •  1
  •   Joseph Lust    10 年前

    您可能想查看PowerMock。EasyMock基于代理反射API,这意味着一切都是代理,您只能测试接口,因此只能测试非最终方法和类。这可能对一些人有用,但如果你测试的是竣工的世界,你将需要更多的电力。

    当然,还有另一个方向,就是重写代码以使其更易于测试,如果可能的话,这通常也是一个好主意。

        5
  •  -1
  •   Marc W    17 年前

    在这种情况下,我发现在我的单元测试类中创建一个嵌套类并以这种方式重写具有特殊需求的方法是最好的方法。所以如果你在测试 ClassA 如果该方法包含您需要访问的参数,您可以执行以下操作:

    class MockClassA extends ClassA {
        @Override
        void specialMethod(String param1, String param2) {
            // do logging or manipulation of some sort
            super.specialMethod(param1,param2); // if you need to
        }
    }
    

    在我的单元测试代码中,我只使用这个实例。只需将其视为任何其他模拟对象即可。比混合库容易得多,我同意这可能不是一个好主意。

    推荐文章