<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>Answers to: How can Junit access to Ivy environment without EnvironmentNotAvailableException</title><link>https://answers.axonivy.com/questions/617/how-can-junit-access-to-ivy-environment-without-environmentnotavailableexception</link><description>&lt;p&gt;Hello everybody
I have two projects, one is A and one is A_Test. And i try to run JUnit Test case from project A_Test to test some functionalities of A. Here is a portion of code 
Project A&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;public CalendarEventRequest(){
    this.organizer = **Ivy.var().get("xabs_calendarEvent_organizer").toString();**
    Calendar c = new GregorianCalendar(Ivy.session().getContentLocale());
    this.timezone= c.getTimeZone();
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Test case&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;@Test
public void testCreateCalendarEvent002() throws Exception {
    CalendarEventRequest request = new CalendarEventRequest();
    CalendarEventResult result = CalendarUtil.createCalendarEvent(request);
    assertNotNull(result);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And JUnit threw this exception:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;    ch.ivyteam.ivy.environment.EnvironmentNotAvailableException: Access to ivy environment outside a process request thread is not possible.
Current thread: Thread[main,5,main]
    at ch.ivyteam.ivy.environment.Ivy.getEnvironmentData(Ivy.java:455)
    at ch.ivyteam.ivy.environment.Ivy.var(Ivy.java:272)
    at ch.soreco.customers.xabs.bean.CalendarEventRequest.&amp;lt;init&amp;gt;(CalendarEventRequest.java:26)
    at ch.soreco.customers.xabs.bean.CalendarUtilTest.createRequestForOneDay(CalendarUtilTest.java:75)
    at ch.soreco.customers.xabs.bean.CalendarUtilTest.testCreateCalendarEvent002(CalendarUtilTest.java:52)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;anyone can help me?&lt;/p&gt;</description><atom:link href="https://answers.axonivy.com/questions/617/how-can-junit-access-to-ivy-environment-without-environmentnotavailableexception" rel="self"></atom:link><language>en</language><lastBuildDate>Tue, 13 May 2014 04:04:40 -0400</lastBuildDate><item><title>Comment by trungdv on nvhuong's answer</title><link>https://answers.axonivy.com/questions/617/how-can-junit-access-to-ivy-environment-without-environmentnotavailableexception#688</link><description>&lt;p&gt;Thanks Mr Huong and Daniel Oechslin
I have tried and it worked perfectly.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">trungdv</dc:creator><pubDate>Tue, 13 May 2014 04:04:40 -0400</pubDate><guid>https://answers.axonivy.com/questions/617/how-can-junit-access-to-ivy-environment-without-environmentnotavailableexception#688</guid></item><item><title>Answer by nvhuong</title><link>https://answers.axonivy.com/questions/617/how-can-junit-access-to-ivy-environment-without-environmentnotavailableexception/684</link><description>&lt;p&gt;Base on the comment of Daniel Oechslin, we should have the function in your case like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;public class IvyMock extends MockUp&amp;lt;Ivy&amp;gt; {

    @Mock
    public static IGlobalVariableContext var() {
        return new IGlobalVariableContext() {
            @Override
            @PublicAPI(IvyScriptVisibility.EXPERT)
            public String get(String varName) {
                String value = "";
                if(varName.equalsIgnoreCase(yourVarName)){ //example: xabs_dateFormat_portal
                    value = yourValueVar; //example: yyyy/MM/dd
                }
                //else if(...
                // other cases here
                return value;
            }
            @Override
            public Set&amp;lt;String&amp;gt; getVariableNames() {
                return null;
            }
        };
  }
}
&lt;/code&gt;&lt;/pre&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">nvhuong</dc:creator><pubDate>Fri, 09 May 2014 13:03:05 -0400</pubDate><guid>https://answers.axonivy.com/questions/617/how-can-junit-access-to-ivy-environment-without-environmentnotavailableexception/684</guid></item><item><title>Comment by Daniel Oechslin on trungdv's question</title><link>https://answers.axonivy.com/questions/617/how-can-junit-access-to-ivy-environment-without-environmentnotavailableexception#623</link><description>&lt;p&gt;The unit test looks like:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;@Before
public void init() {
  new IvyMock();
  new MockUp&amp;lt;ProjectActivityDao&amp;gt;() {
     @Mock
     public ProjectActivity getLatestProjectActivity(long projectId) {
          return oldProjectActivity;
          }
     };
  }
}
&lt;/code&gt;&lt;/pre&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Daniel Oechslin</dc:creator><pubDate>Wed, 09 Apr 2014 12:50:10 -0400</pubDate><guid>https://answers.axonivy.com/questions/617/how-can-junit-access-to-ivy-environment-without-environmentnotavailableexception#623</guid></item><item><title>Comment by Daniel Oechslin on trungdv's question</title><link>https://answers.axonivy.com/questions/617/how-can-junit-access-to-ivy-environment-without-environmentnotavailableexception#622</link><description>&lt;p&gt;My Co-worker changed the current unittesting code to a more convenient one. He simply mocks the the ivy engine with Jmockit.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;public class IvyMock extends MockUp&amp;lt;Ivy&amp;gt; {

    @Mock
    public static IPersistenceContext persistence() {
        return new IPersistenceContext() {
            public IIvyEntityManager get(String persistenceUnitName) {
                return new IIvyEntityManager() {...}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;so we can use the mock in the unittests without starting any ivy relevant stuff.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Daniel Oechslin</dc:creator><pubDate>Wed, 09 Apr 2014 12:49:43 -0400</pubDate><guid>https://answers.axonivy.com/questions/617/how-can-junit-access-to-ivy-environment-without-environmentnotavailableexception#622</guid></item><item><title>Answer by Daniel Oechslin</title><link>https://answers.axonivy.com/questions/617/how-can-junit-access-to-ivy-environment-without-environmentnotavailableexception/618</link><description>&lt;p&gt;I once wrote an roundabout how you can use &lt;a href="http://answers.xpertivy.ch/questions/166/solution-how-to-unit-test"&gt;Unittests in XpertIvy&lt;/a&gt;. I hope this helps.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Daniel Oechslin</dc:creator><pubDate>Mon, 07 Apr 2014 10:14:57 -0400</pubDate><guid>https://answers.axonivy.com/questions/617/how-can-junit-access-to-ivy-environment-without-environmentnotavailableexception/618</guid></item></channel></rss>