[rules-users] JUnit testing
Edson Tirelli
tirelli at post.com
Mon May 19 09:09:25 EDT 2008
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
>
--
Edson Tirelli
JBoss Drools Core Development
Office: +55 11 3529-6000
Mobile: +55 11 9287-5646
JBoss, a division of Red Hat @ www.jboss.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20080519/e6f66ff9/attachment.html
More information about the rules-users
mailing list