Testing 简单模拟-如何?
我正在尝试easyMock测试几个类/接口方法。方法,尝试捕获参数,但得到一个或其他错误。如果我只记录一个期望值,它甚至不会捕获参数管道中的任何内容,如果我使用以下方法,我会得到如下错误代码Testing 简单模拟-如何?,testing,junit,mocking,easymock,Testing,Junit,Mocking,Easymock,我正在尝试easyMock测试几个类/接口方法。方法,尝试捕获参数,但得到一个或其他错误。如果我只记录一个期望值,它甚至不会捕获参数管道中的任何内容,如果我使用以下方法,我会得到如下错误代码 @Test public void testFireChannelInitializer() throws Exception { expect(c.pipeline()).andReturn(pipeline).times(1); channelListener.fireChannelI
@Test
public void testFireChannelInitializer() throws Exception
{
expect(c.pipeline()).andReturn(pipeline).times(1);
channelListener.fireChannelInitializer(EasyMock.capture(pipe), serverHandler);
EasyMock.replay(c, pipeline, channelListener);
initializer.initChannel(c);
verifyAll();
assertEquals(4, pipe.getValues().size());
assertTrue(pipe.getValues().get(0) instanceof LoggingHandler);
assertTrue(pipe.getValues().get(0) instanceof ObjectEncoder);
assertTrue(pipe.getValues().get(0) instanceof ObjectDecoder);
assertTrue(pipe.getValues().get(0) instanceof ServerHandler);
}
导致错误
testFireChannelInitializer(com.obolus.generic.impl.DefaultChannelListenerTest)
时间流逝:3.812秒easymock网站有一个新版本,但他们最近重新编辑了他们的网站,指南不像以前那么完整了 我认为你的问题可能是你必须做一个捕获和参数匹配 从用户指南: 匹配任何值,但在捕获参数中捕获该值以供以后访问。您可以执行和(someMatcher(…),capture(c))从方法的特定调用中捕获参数。您还可以指定CaptureType,告知给定捕获应保留第一个、最后一个、全部或无捕获值 因此,您可能需要执行
和(capture(..),paramMatcher)
EasyMock还有一个恼人的API“特性”,如果在方法调用中使用一个参数匹配器,那么所有参数也必须包装在匹配器中,即使它是eq()
。我想这就是你的特例所抱怨的。所以我认为这是你的两个问题
我不确定你的方法签名是什么样子的,所以我假设它是
void fireChannelInitializer(Object, ServerHandler);
使用静态导入导入EasyMock.*
channelListener.fireChannelInitializer(
and(capture(pipe), isA(Object.class)), //captures the argument to `pipe` Capture object
eq(serverHandler));
我能够用这个
channelListener.fireChannelInitializer(capture(pipe)、eq(serverHandler))来测试这一点代码>。但问题是,我只能针对检查instanceof断言它,但它没有任何值。您如何创建pipe
Capture对象?您是否尝试过使用和()
构造?我是这样使用的Capture pipe=Capture.newInstance(CaptureType.ALL)代码>。是的,我也试过用。