JBoss Drools unit testing with junit-drools

Recently I’ve been working with a project heavily using JBoss Drools. I am not Drools expert – I am not also very convinced to this framework, or maybe only to particular use case in this project – and I found it quite difficult to write simple, maintainable unit tests for Drools based business rules.

That’s how junit-drools was born – simple helper library letting you write Drools test without putting too much boilerplate code (as you can find in Drools JBoss Rules 5.X Developer’s Guide examples).junit-drools
 
 

Installation

Add Maven repository and dependency to your pom.xml:

<repository>
    <id>maciejwalkowiak.pl</id>
    <url>https://github.com/maciejwalkowiak/maven-repo/raw/releases/</url>
</repository>
<dependency>
    <groupId>pl.maciejwalkowiak</groupId>
    <artifactId>junit-drools</artifactId>
    <version>1.0</version>
    <scope>test</scope>
</dependency>

Usage

junit-drools provides DroolsJUnitRunner class that handles most boilerplate code you need to write to set up knowledge base and Drools session.

Lets consider following example:

@RunWith(DroolsJUnitRunner.class)
@DroolsFiles(value = "helloworld.drl", location = "/drl/")
public class AppTest {

    @DroolsSession
    StatefulSession session;

    @Test
    public void should_set_discount() {
        Purchase purchase = new Purchase(new Customer(17));

        session.insert(purchase);
        session.fireAllRules();

        assertTrue(purchase.getTicket().hasDiscount());
    }

    @Test
    public void should_not_set_discount() {
        Purchase purchase = new Purchase(new Customer(22));

        session.insert(purchase);
        session.fireAllRules();

        assertFalse(purchase.getTicket().hasDiscount());
    }
}
  • @RunWith(DroolsJUnitRunner) – inits JUnit runner for testing drools rules
  • @DroolsFiles – set location of drl files (can be one or multiple) – drl files have to be on class path – @DroolsFiles#location is relative to src/test/resources or src/main/resources
  • @DroolsSession – autoinjects Drools session to your test before execution

Find full example with drl file in src/test directory of the project on Github.

In case you don’t want to use DroolsJUnitRunner, for example because you already want to use Mockito or Spring runner you can initialize Drools objects in @Before method:

@DroolsFiles(value = "helloworld.drl", location = "/drl/")
public class BeforeMethodBasedTest {
    @DroolsSession
    StatefulSession session;

    @Before
    public void initDrools() throws Exception {
        new DroolsInjector().initDrools(this);
    }

    @Test
    public void should_set_discount() {
        Purchase purchase = new Purchase(new Customer(17));

        session.insert(purchase);
        session.fireAllRules();

        assertTrue(purchase.getTicket().hasDiscount());
    }
}

I am not very experienced with Drools so the library actually does what was needed in project I was working with. You are welcome to add your improvements by sending pull requests into Github project.
 

Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

Leave a Reply


nine × = 45



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books