### Question:
SandboxedGroovyScriptEngine extends AbstractScriptEngine implements Compilable { @Override public Object eval(final String script, final ScriptContext scriptContext) throws ScriptException { sandbox.register(); try { return realEngine.eval(script, scriptContext); } finally { sandbox.unregister(); } } SandboxedGroovyScriptEngine(final GroovyEngineFactory factory, final ScriptEngine realEngine,
                                final GroovyValueFilter sandbox); @Override Object eval(final String script, final ScriptContext scriptContext); @Override Object eval(final Reader reader, final ScriptContext scriptContext); @Override Bindings createBindings(); @Override ScriptEngineFactory getFactory(); @Override CompiledScript compile(final String script); @Override CompiledScript compile(final Reader reader);  }### Answer:
@Test public void shouldRegisterSandboxWhenEvaluatingScriptAsString() throws Exception { String script = "1 + 1"; ScriptContext context = new SimpleScriptContext(); testEngine.eval(script, context); verify(mockValueFilter).register(); verify(mockScriptEngine).eval(script, context); verify(mockValueFilter).unregister(); }
@Test public void shouldRegisterSandboxWhenEvaluatingScriptAsReader() throws Exception { String script = "1 + 1"; Reader reader = new StringReader(script); ScriptContext context = new SimpleScriptContext(); testEngine.eval(reader, context); verify(mockValueFilter).register(); verify(mockScriptEngine).eval(reader, context); verify(mockValueFilter).unregister(); }
@Test(expectedExceptions = ScriptException.class) public void shouldUnregisterEngineAfterException() throws Exception { String script = "1 + 1"; ScriptContext context = new SimpleScriptContext(); given(mockScriptEngine.eval(script, context)).willThrow(new ScriptException("test")); try { testEngine.eval(script, context); } finally { verify(mockValueFilter).unregister(); } }