0
1

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

public CalendarEventRequest(){
    this.organizer = **Ivy.var().get("xabs_calendarEvent_organizer").toString();**
    Calendar c = new GregorianCalendar(Ivy.session().getContentLocale());
    this.timezone= c.getTimeZone();
}

Test case

@Test
public void testCreateCalendarEvent002() throws Exception {
    CalendarEventRequest request = new CalendarEventRequest();
    CalendarEventResult result = CalendarUtil.createCalendarEvent(request);
    assertNotNull(result);
}

And JUnit threw this exception:

    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.<init>(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)

anyone can help me?

This question is marked "community wiki".

asked 07.04.2014 at 09:34

trungdv's gravatar image

trungdv
450628398
accept rate: 52%

edited 07.04.2014 at 09:44

2

My Co-worker changed the current unittesting code to a more convenient one. He simply mocks the the ivy engine with Jmockit.

public class IvyMock extends MockUp<Ivy> {

    @Mock
    public static IPersistenceContext persistence() {
        return new IPersistenceContext() {
            public IIvyEntityManager get(String persistenceUnitName) {
                return new IIvyEntityManager() {...}

so we can use the mock in the unittests without starting any ivy relevant stuff.

(09.04.2014 at 12:49) Daniel Oechslin Daniel%20Oechslin's gravatar image
1

The unit test looks like:

@Before
public void init() {
  new IvyMock();
  new MockUp<ProjectActivityDao>() {
     @Mock
     public ProjectActivity getLatestProjectActivity(long projectId) {
          return oldProjectActivity;
          }
     };
  }
}
(09.04.2014 at 12:50) Daniel Oechslin Daniel%20Oechslin's gravatar image

Base on the comment of Daniel Oechslin, we should have the function in your case like this:

public class IvyMock extends MockUp<Ivy> {

    @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<String> getVariableNames() {
                return null;
            }
        };
  }
}
link

answered 09.05.2014 at 13:03

nvhuong's gravatar image

nvhuong
105267
accept rate: 66%

edited 13.05.2014 at 16:47

Thanks Mr Huong and Daniel Oechslin I have tried and it worked perfectly.

(13.05.2014 at 04:04) trungdv trungdv's gravatar image

I once wrote an roundabout how you can use Unittests in XpertIvy. I hope this helps.

link

answered 07.04.2014 at 10:14

Daniel%20Oechslin's gravatar image

Daniel Oechslin
45771121
accept rate: 36%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×93
×63
×7
×4

Asked: 07.04.2014 at 09:34

Seen: 2,755 times

Last updated: 13.05.2014 at 16:47