[prev in list] [next in list] [prev in thread] [next in thread] 

List:       poi-user
Subject:    Re: WorkbookEvaluator vs JUnit: WE seems to save state between test calls
From:       "gallon.fizik () gmail ! com"<gallon ! fizik () gmail ! com>
Date:       2017-12-22 11:26:12
Message-ID: pony-a58085dfb77c6b8e7bd4fe5435288f6072ecabc2-e9fe072b1b15ac805b0551f77cb4c3f5759985c9 () user ! poi ! apache ! org
[Download RAW message or body]

Thank you for the detailed explanation.

On 2017-12-21 16:35, Yegor Kozlov <yegor.kozlov@dinom.ru> wrote: 
> 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, gallon.fizik@gmail.com <
> gallon.fizik@gmail.com> 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: user-unsubscribe@poi.apache.org
> > For additional commands, e-mail: user-help@poi.apache.org
> >
> >
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic