Groovy closure to implement matcher in Fest

旧时模样 提交于 2020-01-04 13:40:00

问题


I am trying to create a closure matcher for Fest with Groovy (2.1.6) like this:

def matcherLabel = [ isMatching: { JLabel label -> /* do something */ } ] as GenericTypeMatcher<JLabel>

GenericTypeMatcher is an abstract class with one method only to implement (isMatching (T t))

but I get this error:

org.codehaus.groovy.runtime.typehandling.GroovyCastException: Error casting map to org.fest.swing.core.GenericTypeMatcher, Reason: null
at org.codehaus.groovy.runtime.DefaultGroovyMethods.asType(DefaultGroovyMethods.java:7562)

Is it possible what I am trying to do?


回答1:


Your problem is that the GenericTypeMatcher class doesn't have a default, zero-parameter constructor. Cast the map to an interface like ComponentMatcher instead. If you can't use an interface, another alternative is to subclass GenericTypeMatcher and provide a zero-parameter constructor.



来源:https://stackoverflow.com/questions/18451531/groovy-closure-to-implement-matcher-in-fest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!