diff --git a/easy-rules-core/src/test/java/org/jeasy/rules/core/AbstractTest.java b/easy-rules-core/src/test/java/org/jeasy/rules/core/AbstractTest.java index dc96d7c..0bd2549 100644 --- a/easy-rules-core/src/test/java/org/jeasy/rules/core/AbstractTest.java +++ b/easy-rules-core/src/test/java/org/jeasy/rules/core/AbstractTest.java @@ -50,7 +50,7 @@ public abstract class AbstractTest { facts = new Facts(); facts.add("fact1", fact1); facts.add("fact2", fact2); - rules = new Rules(rule1, rule2); + rules = new Rules(); rulesEngine = RulesEngineBuilder.aNewRulesEngine().build(); } diff --git a/easy-rules-core/src/test/java/org/jeasy/rules/core/AnnotatedRulesTest.java b/easy-rules-core/src/test/java/org/jeasy/rules/core/AnnotatedRulesTest.java index 5316131..1701388 100644 --- a/easy-rules-core/src/test/java/org/jeasy/rules/core/AnnotatedRulesTest.java +++ b/easy-rules-core/src/test/java/org/jeasy/rules/core/AnnotatedRulesTest.java @@ -37,7 +37,8 @@ import static org.assertj.core.api.Assertions.assertThat; public class AnnotatedRulesTest { @Test - public void test() throws Exception { + public void testFactInjection() throws Exception { + // Given Facts facts = new Facts(); facts.add("rain", true); facts.add("age", 18); @@ -46,23 +47,28 @@ public class AnnotatedRulesTest { AgeRule ageRule = new AgeRule(); Rules rules = new Rules(weatherRule, ageRule); + // When RulesEngine rulesEngine = new DefaultRulesEngine(); rulesEngine.fire(rules, facts); + // Then assertThat(ageRule.isExecuted()).isTrue(); assertThat(weatherRule.isExecuted()).isTrue(); } @Test(expected = RuntimeException.class) public void whenFactTypeDoesNotMatchParameterType_thenShouldThrowRuntimeException() throws Exception { + // Given Facts facts = new Facts(); facts.add("age", "foo"); - Rules rules = new Rules(new AgeRule()); - RulesEngine rulesEngine = new DefaultRulesEngine(); + // When rulesEngine.fire(rules, facts); + + // Then + // expected exception } @Rule diff --git a/easy-rules-core/src/test/java/org/jeasy/rules/core/AnnotationInheritanceTest.java b/easy-rules-core/src/test/java/org/jeasy/rules/core/AnnotationInheritanceTest.java index 0be71ea..528aaa8 100644 --- a/easy-rules-core/src/test/java/org/jeasy/rules/core/AnnotationInheritanceTest.java +++ b/easy-rules-core/src/test/java/org/jeasy/rules/core/AnnotationInheritanceTest.java @@ -33,16 +33,19 @@ import org.junit.Test; import static org.assertj.core.api.Assertions.assertThat; -public class AnnotationInheritanceTest { +public class AnnotationInheritanceTest extends AbstractTest { @Test public void annotationsShouldBeInherited() throws Exception { + // Given MyChildRule myChildRule = new MyChildRule(); - RulesEngine rulesEngine = RulesEngineBuilder.aNewRulesEngine().build(); - Rules rules = new Rules(); rules.register(myChildRule); - rulesEngine.fire(rules, new Facts()); + // When + RulesEngine rulesEngine = RulesEngineBuilder.aNewRulesEngine().build(); + rulesEngine.fire(rules, facts); + + // Then assertThat(myChildRule.isExecuted()).isTrue(); } diff --git a/easy-rules-core/src/test/java/org/jeasy/rules/core/CompositeRuleTest.java b/easy-rules-core/src/test/java/org/jeasy/rules/core/CompositeRuleTest.java index a0b94f3..2d421e2 100644 --- a/easy-rules-core/src/test/java/org/jeasy/rules/core/CompositeRuleTest.java +++ b/easy-rules-core/src/test/java/org/jeasy/rules/core/CompositeRuleTest.java @@ -52,24 +52,31 @@ public class CompositeRuleTest extends AbstractTest { @Test public void compositeRuleAndComposingRulesMustBeExecuted() throws Exception { + // Given compositeRule.addRule(rule1); compositeRule.addRule(rule2); - rules.clear(); // FIXME rules.register(compositeRule); + + // When rulesEngine.fire(rules, facts); + + // Then verify(rule1).execute(facts); verify(rule2).execute(facts); } @Test public void compositeRuleMustNotBeExecutedIfAComposingRuleEvaluatesToFalse() throws Exception { + // Given when(rule2.evaluate(facts)).thenReturn(false); compositeRule.addRule(rule1); compositeRule.addRule(rule2); - rules.clear(); // FIXME rules.register(compositeRule); + + // When rulesEngine.fire(rules, facts); + // Then /* * The composing rules should not be executed * since not all rules conditions evaluate to TRUE @@ -77,24 +84,22 @@ public class CompositeRuleTest extends AbstractTest { //Rule 1 should not be executed verify(rule1, never()).execute(facts); - //Rule 2 should not be executed verify(rule2, never()).execute(facts); - } @Test public void whenARuleIsRemoved_thenItShouldNotBeEvaluated() throws Exception { - + // Given compositeRule.addRule(rule1); compositeRule.addRule(rule2); compositeRule.removeRule(rule2); - - rules.clear(); // FIXME rules.register(compositeRule); + // When rulesEngine.fire(rules, facts); + // Then //Rule 1 should be executed verify(rule1).execute(facts); @@ -111,28 +116,34 @@ public class CompositeRuleTest extends AbstractTest { @Test public void testCompositeRuleWithAnnotatedComposingRules() throws Exception { + // Given MyRule rule = new MyRule(); compositeRule = new CompositeRule("myCompositeRule"); compositeRule.addRule(rule); + rules.register(compositeRule); - rules.register(compositeRule);; + // When rulesEngine.fire(rules, facts); + // Then assertThat(rule.isExecuted()).isTrue(); } @Test public void whenAnnotatedRuleIsRemoved_thenItsProxyShouldBeRetrieved() throws Exception { + // Given MyRule rule = new MyRule(); MyAnnotatedRule annotatedRule = new MyAnnotatedRule(); compositeRule = new CompositeRule("myCompositeRule", "composite rule with mixed types of rules"); compositeRule.addRule(rule); compositeRule.addRule(annotatedRule); compositeRule.removeRule(annotatedRule); - rules.register(compositeRule); + + // When rulesEngine.fire(rules, facts); + // Then assertThat(rule.isExecuted()).isTrue(); assertThat(annotatedRule.isExecuted()).isFalse(); } diff --git a/easy-rules-core/src/test/java/org/jeasy/rules/core/CustomRuleOrderingTest.java b/easy-rules-core/src/test/java/org/jeasy/rules/core/CustomRuleOrderingTest.java index 79ec23a..0586a17 100644 --- a/easy-rules-core/src/test/java/org/jeasy/rules/core/CustomRuleOrderingTest.java +++ b/easy-rules-core/src/test/java/org/jeasy/rules/core/CustomRuleOrderingTest.java @@ -46,7 +46,7 @@ public class CustomRuleOrderingTest extends AbstractTest { @Test public void whenCompareToIsOverridden_thenShouldExecuteRulesInTheCustomOrder() throws Exception { - + // Given when(rule1.getName()).thenReturn("a"); when(rule1.getPriority()).thenReturn(1); when(rule1.evaluate(facts)).thenReturn(true); @@ -57,12 +57,13 @@ public class CustomRuleOrderingTest extends AbstractTest { when(rule2.compareTo(rule1)).thenCallRealMethod(); - rules.clear(); rules.register(rule1); rules.register(rule2); + // When rulesEngine.fire(rules, facts); + // Then /* * By default, if compareTo is not overridden, then rule2 should be executed first (priority 0 < 1). * But in this case, the compareTo method order rules by their name, so rule1 should be executed first ("a" < "b") diff --git a/easy-rules-core/src/test/java/org/jeasy/rules/core/DefaultRulesEngineTest.java b/easy-rules-core/src/test/java/org/jeasy/rules/core/DefaultRulesEngineTest.java index 9ef1df3..0c86464 100644 --- a/easy-rules-core/src/test/java/org/jeasy/rules/core/DefaultRulesEngineTest.java +++ b/easy-rules-core/src/test/java/org/jeasy/rules/core/DefaultRulesEngineTest.java @@ -29,7 +29,6 @@ import org.jeasy.rules.annotation.Condition; import org.jeasy.rules.annotation.Priority; import org.jeasy.rules.api.Facts; import org.jeasy.rules.api.RuleListener; -import org.jeasy.rules.api.Rules; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -48,9 +47,6 @@ import static org.mockito.Mockito.*; */ public class DefaultRulesEngineTest extends AbstractTest { - @Mock - private BasicRule rule, anotherRule; - @Mock private RuleListener ruleListener; @@ -59,85 +55,98 @@ public class DefaultRulesEngineTest extends AbstractTest { @Before public void setup() throws Exception { super.setup(); - when(rule.getName()).thenReturn("r"); - when(rule.getDescription()).thenReturn("d"); - when(rule.getPriority()).thenReturn(1); + when(rule1.getName()).thenReturn("r"); + when(rule1.getDescription()).thenReturn("d"); + when(rule1.getPriority()).thenReturn(1); annotatedRule = new AnnotatedRule(); } @Test public void whenConditionIsTrue_thenActionShouldBeExecuted() throws Exception { - when(rule.evaluate(facts)).thenReturn(true); - - rules.clear();// FIXME - rules.register(rule); + // Given + when(rule1.evaluate(facts)).thenReturn(true); + rules.register(rule1); + // When rulesEngine.fire(rules, facts); - verify(rule).execute(facts); + // Then + verify(rule1).execute(facts); } @Test public void whenConditionIsFalse_thenActionShouldNotBeExecuted() throws Exception { - when(rule.evaluate(facts)).thenReturn(false); - rules.clear();// FIXME - rules.register(rule); + // Given + when(rule1.evaluate(facts)).thenReturn(false); + rules.register(rule1); + // When rulesEngine.fire(rules, facts); - verify(rule, never()).execute(facts); + // Then + verify(rule1, never()).execute(facts); } @Test public void rulesMustBeTriggeredInTheirNaturalOrder() throws Exception { - when(rule.evaluate(facts)).thenReturn(true); - when(anotherRule.evaluate(facts)).thenReturn(true); - when(anotherRule.compareTo(rule)).thenReturn(1); - rules.clear();// FIXME - rules.register(rule); - rules.register(anotherRule); + // Given + when(rule1.evaluate(facts)).thenReturn(true); + when(rule2.evaluate(facts)).thenReturn(true); + when(rule2.compareTo(rule1)).thenReturn(1); + rules.register(rule1); + rules.register(rule2); + // When rulesEngine.fire(rules, facts); - InOrder inOrder = inOrder(rule, anotherRule); - inOrder.verify(rule).execute(facts); - inOrder.verify(anotherRule).execute(facts); + // Then + InOrder inOrder = inOrder(rule1, rule2); + inOrder.verify(rule1).execute(facts); + inOrder.verify(rule2).execute(facts); } @Test public void rulesMustBeCheckedInTheirNaturalOrder() throws Exception { - when(rule.evaluate(facts)).thenReturn(true); - when(anotherRule.evaluate(facts)).thenReturn(true); - when(anotherRule.compareTo(rule)).thenReturn(1); - rules.clear();// FIXME - rules.register(rule); - rules.register(anotherRule); + // Given + when(rule1.evaluate(facts)).thenReturn(true); + when(rule2.evaluate(facts)).thenReturn(true); + when(rule2.compareTo(rule1)).thenReturn(1); + rules.register(rule1); + rules.register(rule2); + // When rulesEngine.check(rules, facts); - InOrder inOrder = inOrder(rule, anotherRule); - inOrder.verify(rule).evaluate(facts); - inOrder.verify(anotherRule).evaluate(facts); + // Then + InOrder inOrder = inOrder(rule1, rule2); + inOrder.verify(rule1).evaluate(facts); + inOrder.verify(rule2).evaluate(facts); } @Test public void actionsMustBeExecutedInTheDefinedOrder() { - rules.clear(); // FIXME + // Given rules.register(annotatedRule); + + // When rulesEngine.fire(rules, facts); + + // Then assertEquals("012", annotatedRule.getActionSequence()); } @Test public void annotatedRulesAndNonAnnotatedRulesShouldBeUsableTogether() throws Exception { - when(rule.evaluate(facts)).thenReturn(true); - rules.clear(); // FIXME - rules.register(rule); + // Given + when(rule1.evaluate(facts)).thenReturn(true); + rules.register(rule1); rules.register(annotatedRule); + // When rulesEngine.fire(rules, facts); - verify(rule).execute(facts); + // Then + verify(rule1).execute(facts); assertThat(annotatedRule.isExecuted()).isTrue(); } @@ -156,9 +165,8 @@ public class DefaultRulesEngineTest extends AbstractTest { @Test public void testCheckRules() throws Exception { // Given - when(rule.evaluate(facts)).thenReturn(true); - rules.clear(); // FIXME - rules.register(rule); + when(rule1.evaluate(facts)).thenReturn(true); + rules.register(rule1); rules.register(annotatedRule); // When @@ -174,25 +182,23 @@ public class DefaultRulesEngineTest extends AbstractTest { @Test public void listenerShouldBeInvokedBeforeCheckingRules() throws Exception { // Given - when(rule.evaluate(facts)).thenReturn(true); - when(ruleListener.beforeEvaluate(rule, facts)).thenReturn(true); + when(rule1.evaluate(facts)).thenReturn(true); + when(ruleListener.beforeEvaluate(rule1, facts)).thenReturn(true); rulesEngine = RulesEngineBuilder.aNewRulesEngine() .withRuleListener(ruleListener) .build(); - rules.clear(); // FIXME - rules.register(rule); + rules.register(rule1); // When rulesEngine.check(rules, facts); // Then - verify(ruleListener).beforeEvaluate(rule, facts); + verify(ruleListener).beforeEvaluate(rule1, facts); } @Test public void nullFactsShouldNotCrashTheEngine() { // Given - Facts facts = new Facts(); facts.add("foo", null); // When @@ -202,6 +208,8 @@ public class DefaultRulesEngineTest extends AbstractTest { Assertions.fail("Unable to fire rules on known facts", e); } + // Then + // Should not throw exception } @Test @@ -210,8 +218,7 @@ public class DefaultRulesEngineTest extends AbstractTest { .withRuleListener(ruleListener) .build(); - assertThat(rulesEngine.getRuleListeners()) - .contains(ruleListener); + assertThat(rulesEngine.getRuleListeners()).contains(ruleListener); } @After diff --git a/easy-rules-core/src/test/java/org/jeasy/rules/core/RuleListenerTest.java b/easy-rules-core/src/test/java/org/jeasy/rules/core/RuleListenerTest.java index b50e4f0..8c043d8 100644 --- a/easy-rules-core/src/test/java/org/jeasy/rules/core/RuleListenerTest.java +++ b/easy-rules-core/src/test/java/org/jeasy/rules/core/RuleListenerTest.java @@ -54,50 +54,50 @@ public class RuleListenerTest extends AbstractTest { @Test public void whenTheRuleExecutesSuccessfully_thenOnSuccessShouldBeExecuted() throws Exception { + // Given when(rule1.evaluate(facts)).thenReturn(true); - rules.clear(); // FIXME rules.register(rule1); + + // When rulesEngine.fire(rules, facts); + // Then InOrder inOrder = inOrder(rule1, fact1, fact2, ruleListener1, ruleListener2); inOrder.verify(ruleListener1).beforeExecute(rule1, facts); inOrder.verify(ruleListener2).beforeExecute(rule1, facts); inOrder.verify(ruleListener1).onSuccess(rule1, facts); inOrder.verify(ruleListener2).onSuccess(rule1, facts); - } @Test public void whenTheRuleFails_thenOnFailureShouldBeExecuted() throws Exception { - + // Given + when(rule1.evaluate(facts)).thenReturn(true); final Exception exception = new Exception("fatal error!"); doThrow(exception).when(rule1).execute(facts); - when(rule1.evaluate(facts)).thenReturn(true); - - rules.clear(); // FIXME rules.register(rule1); + // When rulesEngine.fire(rules, facts); + // Then InOrder inOrder = inOrder(rule1, fact1, fact2, ruleListener1, ruleListener2); inOrder.verify(ruleListener1).beforeExecute(rule1, facts); inOrder.verify(ruleListener2).beforeExecute(rule1, facts); inOrder.verify(ruleListener1).onFailure(rule1, facts, exception); inOrder.verify(ruleListener2).onFailure(rule1, facts, exception); - } @Test - public void whenListenerReturnsFalse_thenTheRuleShouldBeSkippedBeforeBeingEvaluated() throws Exception { - + public void whenListenerBeforeEvaluateReturnsFalse_thenTheRuleShouldBeSkippedBeforeBeingEvaluated() throws Exception { // Given when(ruleListener1.beforeEvaluate(rule1, facts)).thenReturn(false); rulesEngine = RulesEngineBuilder.aNewRulesEngine() .withRuleListener(ruleListener1) .build(); + rules.register(rule1); // When - rules.register(rule1); rulesEngine.fire(rules, facts); // Then @@ -105,16 +105,15 @@ public class RuleListenerTest extends AbstractTest { } @Test - public void whenListenerReturnsTrue_thenTheRuleShouldBeEvaluated() throws Exception { - + public void whenListenerBeforeEvaluateReturnsTrue_thenTheRuleShouldBeEvaluated() throws Exception { // Given when(ruleListener1.beforeEvaluate(rule1, facts)).thenReturn(true); rulesEngine = RulesEngineBuilder.aNewRulesEngine() .withRuleListener(ruleListener1) .build(); + rules.register(rule1); // When - rules.register(rule1); rulesEngine.fire(rules, facts); // Then @@ -128,10 +127,9 @@ public class RuleListenerTest extends AbstractTest { rulesEngine = RulesEngineBuilder.aNewRulesEngine() .withRuleListener(ruleListener1) .build(); + rules.register(rule1); // When - rules.clear(); - rules.register(rule1); rulesEngine.fire(rules, facts); // Then @@ -145,10 +143,9 @@ public class RuleListenerTest extends AbstractTest { rulesEngine = RulesEngineBuilder.aNewRulesEngine() .withRuleListener(ruleListener1) .build(); + rules.register(rule1); // When - rules.clear(); - rules.register(rule1); rulesEngine.fire(rules, facts); // Then diff --git a/easy-rules-core/src/test/java/org/jeasy/rules/core/RulePriorityThresholdTest.java b/easy-rules-core/src/test/java/org/jeasy/rules/core/RulePriorityThresholdTest.java index 7f8ecbc..ab4d6cd 100644 --- a/easy-rules-core/src/test/java/org/jeasy/rules/core/RulePriorityThresholdTest.java +++ b/easy-rules-core/src/test/java/org/jeasy/rules/core/RulePriorityThresholdTest.java @@ -51,18 +51,18 @@ public class RulePriorityThresholdTest extends AbstractTest { @Test public void rulesThatExceedPriorityThresholdMustNotBeExecuted() throws Exception { - + // Given rules.register(rule1); rules.register(rule2); + // When rulesEngine.fire(rules, facts); + // Then //Rule 1 should be executed verify(rule1).execute(facts); - //Rule 2 should be skipped since its priority (2) exceeds priority threshold (1) verify(rule2, never()).execute(facts); - } } diff --git a/easy-rules-core/src/test/java/org/jeasy/rules/core/RuleProxyTest.java b/easy-rules-core/src/test/java/org/jeasy/rules/core/RuleProxyTest.java index 7df5e6a..9840461 100644 --- a/easy-rules-core/src/test/java/org/jeasy/rules/core/RuleProxyTest.java +++ b/easy-rules-core/src/test/java/org/jeasy/rules/core/RuleProxyTest.java @@ -33,11 +33,14 @@ public class RuleProxyTest { @Test public void proxyingHappensEvenWhenRuleIsAnnotatedWithMetaRuleAnnotation() { - AnnotatedRuleWithMetaRuleAnnotation rule1 = new AnnotatedRuleWithMetaRuleAnnotation(); + // Given + AnnotatedRuleWithMetaRuleAnnotation rule = new AnnotatedRuleWithMetaRuleAnnotation(); - Rule rule = RuleProxy.asRule(rule1); + // When + Rule proxy = RuleProxy.asRule(rule); - assertNotNull(rule.getDescription()); - assertNotNull(rule.getName()); + // Then + assertNotNull(proxy.getDescription()); + assertNotNull(proxy.getName()); } } diff --git a/easy-rules-core/src/test/java/org/jeasy/rules/core/RulesEngineBuilderTest.java b/easy-rules-core/src/test/java/org/jeasy/rules/core/RulesEngineBuilderTest.java index f0bc26b..cc5e0e9 100644 --- a/easy-rules-core/src/test/java/org/jeasy/rules/core/RulesEngineBuilderTest.java +++ b/easy-rules-core/src/test/java/org/jeasy/rules/core/RulesEngineBuilderTest.java @@ -40,8 +40,10 @@ public class RulesEngineBuilderTest { @Test public void testCreationWithDefaultParameters() { + // When RulesEngine rulesEngine = RulesEngineBuilder.aNewRulesEngine().build(); + // Then assertThat(rulesEngine).isNotNull(); RulesEngineParameters parameters = rulesEngine.getParameters(); @@ -55,9 +57,11 @@ public class RulesEngineBuilderTest { @Test public void testCreationWithCustomParameters() { + // Given String name = "myRulesEngine"; int expectedThreshold = 10; + // When RulesEngine rulesEngine = RulesEngineBuilder.aNewRulesEngine() .named(name) .withRuleListener(ruleListener) @@ -68,9 +72,9 @@ public class RulesEngineBuilderTest { .withSkipOnFirstFailedRule(true) .build(); + // Then assertThat(rulesEngine).isNotNull(); RulesEngineParameters parameters = rulesEngine.getParameters(); - assertThat(parameters.getName()).isEqualTo(name); assertThat(parameters.getPriorityThreshold()).isEqualTo(expectedThreshold); assertThat(parameters.isSilentMode()).isTrue(); diff --git a/easy-rules-core/src/test/java/org/jeasy/rules/core/SkipOnFirstAppliedRuleTest.java b/easy-rules-core/src/test/java/org/jeasy/rules/core/SkipOnFirstAppliedRuleTest.java index f9a508f..0fc461b 100644 --- a/easy-rules-core/src/test/java/org/jeasy/rules/core/SkipOnFirstAppliedRuleTest.java +++ b/easy-rules-core/src/test/java/org/jeasy/rules/core/SkipOnFirstAppliedRuleTest.java @@ -39,9 +39,6 @@ public class SkipOnFirstAppliedRuleTest extends AbstractTest { @Before public void setup() throws Exception { super.setup(); - - setUpRule1(); - rulesEngine = aNewRulesEngine() .withSkipOnFirstAppliedRule(true) .build(); @@ -49,32 +46,40 @@ public class SkipOnFirstAppliedRuleTest extends AbstractTest { @Test public void testSkipOnFirstAppliedRule() throws Exception { + // Given + when(rule2.compareTo(rule1)).thenReturn(1); + when(rule1.evaluate(facts)).thenReturn(true); + rules.register(rule1); + rules.register(rule2); + + // When rulesEngine.fire(rules, facts); + // Then //Rule 1 should be executed verify(rule1).execute(facts); //Rule 2 should be skipped since Rule 1 has been executed verify(rule2, never()).execute(facts); - } @Test public void testSkipOnFirstAppliedRuleWithException() throws Exception { - rulesEngine.fire(rules, facts); - - //If an exception occurs when executing Rule 0, Rule 1 should still be applied - verify(rule1).execute(facts); - - //Rule 2 should be skipped since Rule 1 has been executed - verify(rule2, never()).execute(facts); - - } - - private void setUpRule1() throws Exception { + // Given when(rule1.evaluate(facts)).thenReturn(true); + when(rule2.evaluate(facts)).thenReturn(true); + when(rule2.compareTo(rule1)).thenReturn(1); final Exception exception = new Exception("fatal error!"); doThrow(exception).when(rule1).execute(facts); + + rules.register(rule1); + rules.register(rule2); + + // When + rulesEngine.fire(rules, facts); + + //If an exception occurs when executing Rule 1, Rule 2 should still be applied + verify(rule2).execute(facts); } } diff --git a/easy-rules-core/src/test/java/org/jeasy/rules/core/SkipOnFirstFailedRuleTest.java b/easy-rules-core/src/test/java/org/jeasy/rules/core/SkipOnFirstFailedRuleTest.java index a43fde5..bda89ca 100644 --- a/easy-rules-core/src/test/java/org/jeasy/rules/core/SkipOnFirstFailedRuleTest.java +++ b/easy-rules-core/src/test/java/org/jeasy/rules/core/SkipOnFirstFailedRuleTest.java @@ -38,7 +38,6 @@ public class SkipOnFirstFailedRuleTest extends AbstractTest { @Before public void setup() throws Exception { super.setup(); - setUpRule1(); rulesEngine = RulesEngineBuilder.aNewRulesEngine() .withSkipOnFirstFailedRule(true) .build(); @@ -46,23 +45,22 @@ public class SkipOnFirstFailedRuleTest extends AbstractTest { @Test public void testSkipOnFirstFailedRule() throws Exception { + // Given + when(rule1.evaluate(facts)).thenReturn(true); + when(rule2.compareTo(rule1)).thenReturn(1); + final Exception exception = new Exception("fatal error!"); + doThrow(exception).when(rule1).execute(facts); + rules.register(rule1); + rules.register(rule2); + // When rulesEngine.fire(rules, facts); + // Then //Rule 1 should be executed verify(rule1).execute(facts); - //Rule 2 should be skipped since Rule 1 has failed verify(rule2, never()).execute(facts); - - } - - private void setUpRule1() throws Exception { - when(rule1.getName()).thenReturn("r1"); - when(rule1.getPriority()).thenReturn(1); - when(rule1.evaluate(facts)).thenReturn(true); - final Exception exception = new Exception("fatal error!"); - doThrow(exception).when(rule1).execute(facts); } } diff --git a/easy-rules-core/src/test/java/org/jeasy/rules/core/SkipOnFirstNonTriggeredRuleTest.java b/easy-rules-core/src/test/java/org/jeasy/rules/core/SkipOnFirstNonTriggeredRuleTest.java index cae2c09..6ae2df6 100644 --- a/easy-rules-core/src/test/java/org/jeasy/rules/core/SkipOnFirstNonTriggeredRuleTest.java +++ b/easy-rules-core/src/test/java/org/jeasy/rules/core/SkipOnFirstNonTriggeredRuleTest.java @@ -38,7 +38,6 @@ public class SkipOnFirstNonTriggeredRuleTest extends AbstractTest { @Before public void setup() throws Exception { super.setup(); - setUpRule1(); rulesEngine = RulesEngineBuilder.aNewRulesEngine() .withSkipOnFirstNonTriggeredRule(true) .build(); @@ -46,21 +45,21 @@ public class SkipOnFirstNonTriggeredRuleTest extends AbstractTest { @Test public void testSkipOnFirstNonTriggeredRule() throws Exception { + // Given + when(rule1.evaluate(facts)).thenReturn(false); + when(rule2.compareTo(rule1)).thenReturn(1); + + rules.register(rule1); + rules.register(rule2); + // When rulesEngine.fire(rules, facts); - //Rule1 is non triggered + // Then + //Rule1 is not triggered verify(rule1, never()).execute(facts); - //Rule 2 should be skipped since Rule 1 has not been executed verify(rule2, never()).execute(facts); - - } - - private void setUpRule1() throws Exception { - when(rule1.getName()).thenReturn("r1"); - when(rule1.getPriority()).thenReturn(1); - when(rule1.evaluate(facts)).thenReturn(false); } }