[rules-users] JUnit testing

Mike Dean mdean77 at comcast.net
Mon May 19 20:20:28 EDT 2008


Thanks to both of you for pursuing this problem.  I have worked around it by
putting my business classes and rules in a separate Drools project, testing
everything there, and then copying them to my RCP.  I look forward to a
better solution!

I must confess Java ignorance, but I use a test suite and try to instantiate
the engine once and then run a series of tests.  My crash occurs before I
get to the individual tests.  However, the setUp routine is static, and your
code work around cannot be executed in a static context.  Where exactly to
you put the test for a null classloader?

Thanks again.  I thought this issue simply died.


Marcus Ilgner wrote:
> 
> Hi Edson,
> 
> thank you very much for testing this! Since I could not discern any
> differences between your approach and mine, I decided to resolve the
> issue manually by inserting a work-around in my test routine which
> sets the context class loader for the current thread before calling
> the method under test.
> 
> if (Thread.currentThread().getContextClassLoader() == null) {
> 
> Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
> }
> 
> My guess is that the custom JUnit 4 class runner
> (SpringJUnit4ClassRunner) somehow is responsible for the faulty
> behaviour.
> 
> Thanks again
> Marcus
> 
> 
> On Mon, May 19, 2008 at 3:09 PM, Edson Tirelli <tirelli at post.com> wrote:
>>
>>    All,
>>
>>    I see that the code in that spot is weak and could be improved.
>> Although,
>> using drools 4.0.7, I successfully created a junit 4 test that I paste
>> bellow. Also, the whole drools code is tested with junit 3 and we have no
>> problems. For instance, look at our integration tests here:
>>
>> http://anonsvn.labs.jboss.com/labs/jbossrules/branches/4.0.x/drools-compiler/src/test/java/org/drools/integrationtests/
>>
>>    The code I used to create the junit 4 test is based on the default
>> example create by the drools plugin + the snippet you showed bellow:
>>
>>     @Test
>>     public void testRuleBase() {
>>         try {
>>
>>             //load up the rulebase
>>             RuleBase ruleBase = readRule();
>>             StatefulSession workingMemory =
>> ruleBase.newStatefulSession();
>>
>>             //go !
>>             Message message = new Message();
>>             message.setMessage(  "Hello World" );
>>             message.setStatus( Message.HELLO );
>>             workingMemory.insert( message );
>>             workingMemory.fireAllRules();
>>             workingMemory.dispose();
>>
>>         } catch (Throwable t) {
>>             t.printStackTrace();
>>             Assert.fail("Something is wrong: "+t.getMessage());
>>         }
>>     }
>>
>>     /**
>>      * Please note that this is the "low level" rule assembly API.
>>      */
>>     private RuleBase readRule() throws Exception {
>>         //read in the source
>>         Reader source = new InputStreamReader(
>> DroolsTest.class.getResourceAsStream( "/Sample.drl" ) );
>>
>>         PackageBuilderConfiguration conf = new
>> PackageBuilderConfiguration();
>>         JavaDialectConfiguration javaConf = (JavaDialectConfiguration)
>> conf.getDialectConfiguration("java");
>>         javaConf.setCompiler(JavaDialectConfiguration.ECLIPSE);
>>         javaConf.setJavaLanguageLevel("1.5");
>>
>>         PackageBuilder builder = new PackageBuilder( conf );
>>         builder.addPackageFromDrl( source );
>>
>>         RuleBase ruleBase = RuleBaseFactory.newRuleBase();
>>         ruleBase.addPackage( builder.getPackage() );
>>         return ruleBase;
>>     }
>>
>>     Hope it helps,
>>       Edson
>>
>>
>> 2008/5/19 Marcus Ilgner <marcus.ilgner at gmail.com>:
>>>
>>> Hi list,
>>>
>>> I'm reviving this thread because I happen to have the same problem.
>>>
>>> On Sun, Mar 23, 2008 at 7:34 PM, Mike Dean <mdean77 at comcast.net> wrote:
>>> >
>>> > Thanks (months later!).  I have cut down my code example below to show
>>> > this
>>> > issue more clearly:
>>> >
>>> >
>>> > John J. Franey wrote:
>>> >>
>>> >> NullPointerException on line 146 means classLoader is null.   That is
>>> >> odd.
>>> >> I have no idea why the code that gets classLoader won't work in
>>> >> eclipse's
>>> >> junit runtime, but does when run in your eclipse plugin.
>>>
>>>  It seems indeed like during JUnit tests, the context classloader of
>>> the thread gets not set (null).
>>>
>>> The code leading up to the problem is as follows:
>>> PackageBuilderConfiguration conf = new PackageBuilderConfiguration();
>>> JavaDialectConfiguration javaConf = (JavaDialectConfiguration)
>>> conf.getDialectConfiguration("java");
>>> javaConf.setCompiler(JavaDialectConfiguration.ECLIPSE);
>>> javaConf.setJavaLanguageLevel("1.5");
>>> RuleBase ruleBase = RuleBaseFactory.newRuleBase();
>>>
>>> The resulting stacktrace is:
>>> java.lang.NullPointerException
>>>        at
>>> org.drools.RuleBaseConfiguration.instantiateClass(RuleBaseConfiguration.java:569)
>>>        at
>>> org.drools.RuleBaseConfiguration.determineConsequenceExceptionHandler(RuleBaseConfiguration.java:561)
>>>        at
>>> org.drools.RuleBaseConfiguration.init(RuleBaseConfiguration.java:220)
>>>        at
>>> org.drools.RuleBaseConfiguration.<init>(RuleBaseConfiguration.java:133)
>>>        at
>>> org.drools.common.AbstractRuleBase.<init>(AbstractRuleBase.java:147)
>>>        at
>>> org.drools.reteoo.ReteooRuleBase.<init>(ReteooRuleBase.java:124)
>>>        at
>>> org.drools.reteoo.ReteooRuleBase.<init>(ReteooRuleBase.java:101)
>>>        at
>>> org.drools.RuleBaseFactory.newRuleBase(RuleBaseFactory.java:57)
>>>        at
>>> org.drools.RuleBaseFactory.newRuleBase(RuleBaseFactory.java:38)
>>>
>>> Has anyone set up Drools and JUnit successfully? Maybe we forgot to
>>> set up the environment properly?
>>>
>>> Best regards
>>> Marcus
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
> 
> 

-- 
View this message in context: http://www.nabble.com/Re%3AJUnit-testing-tp14164710p17330884.html
Sent from the drools - user mailing list archive at Nabble.com.




More information about the rules-users mailing list