Witty wit
Witty wit

Reputation: 11

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

I want to do junit for my rule files. 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

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.

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 . Only the print() function I want to call.

Upvotes: 1

Views: 798

Answers (1)

pcoates
pcoates

Reputation: 2307

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.

e.g. 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 function my.package.MyFunctions.print

Upvotes: 1

Related Questions