我的测试方法中有以下JMock期望片段:
context.checking(new Expectations() {{
allowing(listener).tableChanged(with(anyInsertionEvent()));
oneOf(listener).tableChanged(with(aRowChangedEvent(0)));
}});
其中anyInsertionEvent
和aRowChangedEventAs
返回Matcher<TableModelEvent>
的实例。
这取自书&#34; Growing Object-Oriented Software Guided by Tests&#34; (第181页)。
我尝试将此测试转换为Groovy,但我需要的方法是:
org.jmock.Expectations.with(org.hamcrest.Matcher<T>)
被阴影:
org.codehaus.groovy.runtime.DefaultGroovyMethods.with(java.lang.Object,
groovy.lang.Closure<T>)
因此,我在测试期间遇到错误,如:
groovy.lang.MissingMethodException: No signature of method: $Proxy8.tableChanged() is applicable for argument types: (java.lang.Boolean) values: [false]
Possible solutions: tableChanged(javax.swing.event.TableModelEvent)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:55)
at org.codehaus.groovy.runtime.callsite.PojoMetaClassSite.call(PojoMetaClassSite.java:46)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at org.rorick.auctionsniper.ui.SnipersTableModelTest$1.<init>(SnipersTableModelTest.groovy:43)
at org.rorick.auctionsniper.ui.SnipersTableModelTest.setSniperValuesInColumns(SnipersTableModelTest.groovy:42)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:66)
at org.jmock.integration.junit4.JMock$1.invoke(JMock.java:37)
at org.junit.internal.runners.MethodRoadie.runTestMethod(MethodRoadie.java:105)
at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:86)
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:94)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:84)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
at org.junit.internal.runners.JUnit4ClassRunner.invokeTestMethod(JUnit4ClassRunner.java:98)
at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:61)
at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:54)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:52)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:76)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
如何使用适当的with
方法?或者,请以任何其他方式建议解决此问题。
更新:它不是影子。实际调用的方法是Expectations.with(Matcher<Boolean>)
,因此堆栈跟踪中的false
值。因此,方法调度错误。任何想法如何处理?
更新:匹配器方法如下:
public Matcher<TableModelEvent> anyInsertionEvent() {
Matcher<Integer> insertMatcher = equalTo(TableModelEvent.INSERT);
return new FeatureMatcher<TableModelEvent, Integer>(insertMatcher, "is an insertion event", "event type") {
@Override
protected Integer featureValueOf(TableModelEvent actual) {
return actual.getType();
}
};
}
private Matcher<TableModelEvent> aRowChangedEvent(int row) {
return samePropertyValuesAs(new TableModelEvent(model, row));
}
答案 0 :(得分:1)
最后,我找到了解决方法。请参阅下面的检查代码。 new Expectations() {{}}
Java语法已经消失,并被替换为closure:
context.checking {
allowing(listener).tableChanged(argThat(anyInsertionEvent()));
oneOf(listener).tableChanged(argThat(aRowChangedEvent(0)));
}
请注意,with
已替换为argThat
(以Mockito为模型)。 context
不是org.jmock.integration.junit4.JUnit4Mockery
的实例,而是以下类的实例:
class JUnit4GroovyMockery extends JUnit4Mockery {
public void checking(Closure c) {
ClosureExpectations expectations = new ClosureExpectations();
expectations.closureInit(c, expectations);
super.checking(expectations);
}
private static class ClosureExpectations extends Expectations {
void closureInit(Closure cl, Object delegate) {
cl.setDelegate(delegate);
cl.call();
}
public Object argThat(Matcher<?> matcher) {
currentBuilder().addParameterMatcher(matcher);
return null;
}
}
}
这是基于http://docs.codehaus.org/display/GROOVY/Using+JMock+with+Groovy的JUnit4GroovyMockery类。 argThat
是Expectations.with(Matcher<T>)
和Expectations.addParameterMatcher(Matcher<?>)
的复制粘贴。
这对我有用,虽然我对这个解决方案不是很满意;我希望保留with
名称。