junitdroolsrule-enginedrools-flowjboss-rules

Drools testing with junit


What is the best practice to test drools rules with junit?

Until now we used junit with dbunit to test rules. We had sample data that was put to hsqldb. We had couple of rule packages and by the end of the project it is very hard to make a good test input to test certain rules and not fire others.

So the exact question is that how can I limit tests in junit to one or more certain rule(s) for testing?


Solution

  • Personally I use unit tests to test isolated rules. I don't think there is anything too wrong with it, as long as you don't fall into a false sense of security that your knowledge base is working because isolated rules are working. Testing the entire knowledge base is more important.

    You can write the isolating tests with AgendaFilter and StatelessSession

    StatelessSession session = ruleBase.newStatelessSesssion();
    
    session.setAgendaFilter( new RuleNameMatches("<regexp to your rule name here>") );
    
    List data = new ArrayList();
    ... // create your test data here (probably built from some external file)
    
    StatelessSessionResult result == session.executeWithResults( data );
    
    // check your results here.
    

    Code source: http://blog.athico.com/2007/07/my-rules-dont-work-as-expected-what-can.html