UDFs are registered in a static map and JUnit does not reset static variables. Use the @BeforeClass annotation to register your functions. This can be a problem if you have many functions, but if it is just one a or few it should be okay. If @BeforeClass is not an option then the only alternative is to reset static variables via reflection or class loaded in @After.
Yegor On Thu, Dec 21, 2017 at 5:36 PM, [email protected] < [email protected]> wrote: > Hello, I have a simple test class: > > import org.apache.poi.ss.formula.WorkbookEvaluator; > import org.apache.poi.ss.formula.eval.NumberEval; > import org.apache.poi.ss.formula.eval.ValueEval; > import org.apache.poi.ss.formula.functions.Function; > import org.junit.Test; > > public class FunctionRegistratorIT1 { > @Test > public void testRegisterFunction1() { > WorkbookEvaluator.registerFunction("TREND", new > DummyExcelFunction()); > } > > @Test > public void testRegisterFunction2() { > WorkbookEvaluator.registerFunction("TREND", new > DummyExcelFunction()); > } > > private class DummyExcelFunction implements Function { > @Override > public ValueEval evaluate(ValueEval[] args, int srcRowIndex, int > srcColumnIndex) { > return new NumberEval(0); > } > } > } > > Oddly enough, the test being executed second fails with > 'IllegalArgumentException: POI already implements TREND. You cannot POI's > implementations of Excel functions'. If I comment one of the tests out, the > other one passes. This behavior persists if the same function is registered > in different tests across a test suite. > > I expected that every @Test is run in isolation, but it seems that > WorkbookEvaluator stores registered functions between invocations of > different tests within a suite. > > Although it is possible that it is probably related to JUnit rather than > POI, perhaps someone here has already witnessed this behavior. > > To sum up, the questions are: > * why does this happen? > * how can I have get several tests registering the same function names > work? > > Regards, > Vladislav > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [email protected] > For additional commands, e-mail: [email protected] > >
