[rules-users] Testing individual rules

Andrew Waterman andrew.waterman at gmail.com
Wed Dec 2 12:05:48 EST 2009


Thanks Greg, this is really helpful to me.

I do mostly functional tests on the rules that we use to drive our  
games/models.  As we trigger JMS messages within our rulebase; I use  
the JMSTestCaseAdapter (JUnit 3 compatible) to capture sent messages  
and ensure that functional conditions conform to what I expect.   
Outside of that, my tests pretty much follow a similar format to that  
suggested by Macon below, although I'm not using the simulation and  
testing framework, just handles through my own Game objects (which do  
the work of handling WorkingMemory, KnowledgeBase and fact  
insertion).  EasyMock sounds like a very useful suggestion for mocking  
up the possibly complex graphs that are inserted into working memory.

Anyway, my two cents.

best wishes,

Andrew

On Dec 2, 2009, at 10:43 AM, Greg Barton wrote:

> You can also implement various listeners, (WorkingMemoryListener,  
> AgendaEventListener, and other such goodies in package  
> org.drools.event) register them with your session, and make  
> assertions over the execution data they gather.  You can track such  
> things as:
>
> Did rule A fire?  How many times?
>
> Did rule A fire on fact B?
>
> You can even go down to the level of "did rule sequence A fire in  
> the right order?" assertions, but usually such tests are too  
> brittle, and miss the point of rules anyway. :)
>
> --- On Wed, 12/2/09, Asif Iqbal <Asif.Iqbal at infor.com> wrote:
>
>> From: Asif Iqbal <Asif.Iqbal at infor.com>
>> Subject: Re: [rules-users] Testing individual rules
>> To: "Rules Users List" <rules-users at lists.jboss.org>
>> Date: Wednesday, December 2, 2009, 10:15 AM
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> Thanks will
>> look into it.
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> From:
>> rules-users-bounces at lists.jboss.org
>> [mailto:rules-users-bounces at lists.jboss.org]
>> On Behalf Of
>> Pegram, Macon
>>
>> Sent: 02
>> December 2009 16:08
>>
>> To: Rules
>> Users List
>>
>> Subject: Re:
>> [rules-users] Testing
>> individual rules
>>
>>
>>
>>
>>
>> Yes…
>>
>>
>>
>>
>>
>> We’ve
>> used a combination of JUnit, and
>> Easymock to test individual rules.  At a very high
>> level, we do the
>> following:
>>
>>
>>
>>
>> -
>> In
>> the
>> static setupBeforeClass() method we construct a
>> KnowledgeBuilder, load any
>> necessary rule files, and construct a KnowledgeBase using
>> the KnowledgeBuilder.
>>
>> -
>> In
>> the
>> setup() method we initialize any create EasyMock objects,
>> create a
>> KnowledgeSession, attach any globals and setup a
>> KnowledgeBase audit logger
>> (KnowledgeRuntimeLoggerFactory),
>>
>> -
>> In
>> the
>> individual test we construct an ArrayList of Command
>> objects to do fact
>> insertion, rule firing, and run a query (to extract
>> inserted or modified facts
>> from the Working Memory) like so:
>>
>> o
>> Cmds.add
>> (CommandFactory.newInsert(aFact))
>>
>> o
>> Cmds.add(CommandFactory.newFireAllRules())
>>
>>
>> o
>> Cmds.add(CommandFactory.newQuery(xxx,
>> yyyy));
>>
>> -
>> We
>> batch
>> execute the commands (see session.execute()) and use the
>> ExecutionResults to
>> extract query results.
>>
>> -
>> Finally
>> we use the JUnit framework to assert expectations that come
>> out of our
>> ExecutionResults.
>>
>>
>>
>>
>> I’ve
>> left a lot of details out, but if you
>> do Javadoc lookups on the API’s I’ve mentioned
>> above most of it should be
>> clear.
>>
>>
>>
>>
>> We’ve
>> written over 1,000 unit tests this
>> way, and they run quite fast.
>>
>>
>>
>>
>> Macon
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> From:
>> rules-users-bounces at lists.jboss.org
>> [mailto:rules-users-bounces at lists.jboss.org] On
>> Behalf Of Asif Iqbal
>>
>> Sent:
>> Wednesday, December 02, 2009
>> 6:59 AM
>>
>> To:
>> rules-users at lists.jboss.org
>>
>> Subject:
>> [rules-users] Testing
>> individual rules
>>
>>
>>
>>
>>
>> Hi,
>>
>>
>>
>> This is general question, is it
>> possible to test
>> individual rules in a rules file? And using what, I am
>> familiar with JUnit, but
>> have seen Fit being used in examples.
>>
>>
>>
>> regards
>>
>>
>>
>>
>>
>>
>>
>> -----Inline Attachment Follows-----
>>
>> _______________________________________________
>> rules-users mailing list
>> rules-users at lists.jboss.org
>> https://lists.jboss.org/mailman/listinfo/rules-users
>>
>
>
>
>
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users





More information about the rules-users mailing list