简体   繁体   English

如何在不调用规则的情况下测试 drool 文件中存在的函数?

[英]How to test functions present in the drool file without calling rule?

I want to do junit for my rule files.我想为我的规则文件做 junit。 My rule file broadly has two things:我的规则文件大致有两件事:

  1. a rule规则
  2. a helper function (in drools, not Java) which I am calling from then section of a rule一个辅助函数(在 drools 中,而不是 Java),我从规则的那个部分调用

Now I want to test(assert) this function isolatedly based on the test case.现在我想根据测试用例单独测试(断言)这个函数 is there any way possible to do this?有没有办法做到这一点?

function void print(String string){
            System.out.println(string);
            return true;
 }

rule "XXYJK"
dialect "mvel"
salience 10
when
 objofTheclass : exampleClass() eval
 (
  objofTheclass.isKeyMatched("XXYJK")

 )
then
print("XXYJK");
end

Now I can call this rule from java code like this way.现在我可以像这样从java代码中调用这个规则。

statelessSession.setAgendaFilter(new RuleNameEqualsAgendaFilter("XXYJK"));

statelessSession.executeWithResults(rulesEngineParameters);

Now I want to do similar things without calling the rule itself or executing the whole drl file .现在我想在不调用规则本身或执行整个 drl 文件的情况下做类似的事情。 Only the print() function I want to call.只有我想调用的 print() 函数。

If you just want to unit test your function in the rule, I'd suggest moving it to a static method in a separate class file.如果您只想在规则中对您的函数进行单元测试,我建议将其移动到单独的类文件中的静态方法。 You can easily unit test that.您可以轻松地对其进行单元测试。

eg class with print as static method例如带有打印作为静态方法的类

package my.package
public class MyFunctions {
    public static void print(String string){
        System.out.println(string);
    }
}

You can then import the static method as a function in your rule by replacing your function definition with an import statement:然后,您可以通过将函数定义替换为 import 语句,将静态方法作为规则中的函数导入:

import function my.package.MyFunctions.print

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM