How do you unit test Jboss Rules (Drools) rules?
Asked Answered
G

2

6

I have written a few Drools rules for my Seam application and am wondering how I go about unit testing these. Does anybody have any sample code that can show me how to go about doing this?

Grieco answered 30/7, 2009 at 12:57 Comment(4)
I have actually figured this out for myself. Just wanted to contribute the question, and also the answer to the site for anyone else wondering about this.Grieco
so answer your own question. You might get a self-learner badge ;)Nolte
@samuelcarrijo - I meant to, just forgot. Thanks for the reminder ;)Grieco
Now if you guys could only up vote the answer I can get that badge ;)Grieco
G
7

Add the following code to a unit test (JUnit, TestNG, etc):

PackageBuilder builder = new PackageBuilder();

builder.addPackageFromDrl(new InputStreamReader(getClass().getResourceAsStream( "rules.drl")));

PackageBuilderErrors errors = builder.getErrors();

Assert.assertEquals(0, errors.getErrors().length);

RuleBase ruleBase  = RuleBaseFactory.newRuleBase();
ruleBase.addPackage(pkg);

StatefullSession session = ruleBase.newStatefulSession(false);

MyFactObject myFact = new MyFactObject();

session.insert(myFact);
session.fireAllRules();
Grieco answered 30/7, 2009 at 13:37 Comment(1)
I have to wait 2 days before I can do so ;)Grieco
K
1

Consider some testing 'sugar' with a Junit Extensions library - droolsassert.

Knap answered 20/5, 2019 at 20:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.