给Fitnesse添加调用多参数fixture的调用方法

修改文件:fitnesse.slim.fixtureInteraction.DefaultInteraction.java

修改如下三处内容:

(注意只支持仅含有一个参数,且该参数是多参数的fixture)

 1 protected Method findMatchingMethod(String methodName, Class<?> k, int nArgs) {
 2         Method[] methods = k.getMethods();
 3         if(methods == null) {
 4             methods = k.getMethods();
 5         }
 6 
 7         for (Method method : methods) {
 8             boolean hasMatchingName = method.getName().equals(methodName);
 9             if(hasMatchingName && method.isVarArgs()){
10                 return method;
11             }
12             boolean hasMatchingArguments = method.getParameterTypes().length == nArgs;
13             if (hasMatchingName && hasMatchingArguments) {
14                 return method;
15             }
16         }
17         return null;
18     }
1 protected Object[] convertArgs(Method method, Object[] args) {
2         Type[] argumentParameterTypes=null;
3         if(method.isVarArgs()){
4             return args;        
5         }else{
6             argumentParameterTypes = method.getGenericParameterTypes();
7         }    
8         return ConverterSupport.convertArgs(args, argumentParameterTypes);
9     }
 1 public Object methodInvoke(Method method, Object instance, Object... convertedArgs) throws Throwable {
 2         try {
 3             if(method.isVarArgs()){
 4                 String[] s = new String[convertedArgs.length];
 5                 int i =0;
 6                 for(Object arg:convertedArgs){
 7                     s[i]=arg.toString();
 8                     i++;
 9                 }
10                 return method.invoke(instance, (Object)s);
11             }else{
12                 return method.invoke(instance, convertedArgs);
13             }
14             
15         } catch (InvocationTargetException e) {
16             if(e.getCause() != null){
17                 throw e.getCause();
18             }else{
19                 throw e.getTargetException();
20             }
21         }
22     }

 使用例子:

posted @ 2017-03-07 13:47  月色深潭  阅读(536)  评论(0编辑  收藏  举报