gpt4 book ai didi

testing - 简单模拟 - 如何?

转载 作者:行者123 更新时间:2023-11-28 20:41:51 24 4
gpt4 key购买 nike

我正在尝试 easyMock 测试几个类/接口(interface)方法。带有参数的方法, try catch 参数,但出现一个或另一个错误。如果我设法只记录一个期望,它甚至不会在参数管道中捕获任何内容,如果我使用以下方法,我会收到如下代码所示的错误。

@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) Time elapsed: 3.812 sec <<< ERROR! java.lang.IllegalStateException: 2 matchers expected, 1 recorded. This exception usually occurs when matchers are mixed with raw values when recording a method: foo(5, eq(6)); // wrong You need to use no matcher at all or a matcher for every single param: foo(eq(5), eq(6)); // right foo(5, 6); // also right at org.easymock.internal.ExpectedInvocation.createMissingMatchers(ExpectedInvocation.java:51) at org.easymock.internal.ExpectedInvocation.(ExpectedInvocation.java:40) at org.easymock.internal.RecordState.invoke(RecordState.java:78) at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:40) at org.easymock.internal.ObjectMethodsFilter.invoke(ObjectMethodsFilter.java:94) at org.easymock.internal.ClassProxyFactory$MockMethodInterceptor.intercept(ClassProxyFactory.java:97) at com.obolus.generic.impl.DefaultChannelListener$$EnhancerByCGLIB$$2da02970.fireChannelInitializer() at com.obolus.generic.impl.DefaultChannelListenerTest.testFireChannelInitializer(DefaultChannelListenerTest.java:63) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:483) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) at org.junit.runners.ParentRunner.run(ParentRunner.java:300) at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:242) at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:137) at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:483) at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189) at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165) at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85) at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115) at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)

知道哪里出了问题或如何使用简单模拟吗?没有好的文档或示例。

最佳答案

easymock 网站有一个 user guide但是他们最近重新设计了他们的网站,并且该指南不像以前那样完整。

我认为您的问题可能是您必须执行捕获和参数匹配器。

来自用户指南:

Matches any value but captures it in the Capture parameter for later access. You can do and(someMatcher(...), capture(c)) to capture a parameter from a specific call to the method. You can also specify a CaptureType telling that a given Capture should keep the first, the last, all or no captured values.

所以你可能需要做一个and(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));

关于testing - 简单模拟 - 如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29491623/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com