### Question:
MVELAction implements Action { @Override public void execute(Facts facts) { try { MVEL.executeExpression(compiledExpression, facts.asMap()); } catch (Exception e) { LOGGER.error("Unable to evaluate expression: '" + expression + "' on facts: " + facts, e); throw e; } } MVELAction(String expression);  MVELAction(String expression, ParserContext parserContext); @Override void execute(Facts facts);  }### Answer:
@Test public void testMVELActionExecution() throws Exception { Action markAsAdult = new MVELAction("person.setAdult(true);"); Facts facts = new Facts(); Person foo = new Person("foo", 20); facts.put("person", foo); markAsAdult.execute(facts); assertThat(foo.isAdult()).isTrue(); }
@Test public void testMVELFunctionExecution() throws Exception { Action printAction = new MVELAction("def hello() { System.out.println(\"Hello from MVEL!\"); }; hello();"); Facts facts = new Facts(); printAction.execute(facts); assertThat(systemOutRule.getLog()).contains("Hello from MVEL!"); }
@Test public void testMVELActionExecutionWithFailure() { Action action = new MVELAction("person.setBlah(true);"); Facts facts = new Facts(); Person foo = new Person("foo", 20); facts.put("person", foo); assertThatThrownBy(() -> action.execute(facts)) .isInstanceOf(Exception.class) .hasMessageContaining("Error: unable to resolve method: org.jeasy.rules.mvel.Person.setBlah(java.lang.Boolean)"); }
@Test public void testMVELActionWithExpressionAndParserContext() throws Exception { ParserContext context = new ParserContext(); context.addPackageImport("java.util"); Action printAction = new MVELAction("def random() { System.out.println(\"Random from MVEL = \" + new java.util.Random(123).nextInt(10)); }; random();", context); Facts facts = new Facts(); printAction.execute(facts); assertThat(systemOutRule.getLog()).contains("Random from MVEL = 2"); }