Server side mock of Work Items with Mockito
I want to write JUnit Tests for my Follow-Up Action. Therefor I want to mock all necessary Artifacts with Mockito.
But if I try to Mock the class IWorkItem.class
IWorkItem workItem = mock(IWorkItem.class);
I receive a NullPointerException. Did somebody know how to mock in a server plugin the work item with Mockito?
Thanks and regards
Martin
java.lang.ExceptionInInitializerError
at java.lang.J9VMInternals.initialize(J9VMInternals.java:259)
at com.ibm.team.workitem.common.internal.util.AuditablesHelper.<clinit>(AuditablesHelper.java:40)
at java.lang.J9VMInternals.initializeImpl(Native Method)
at java.lang.J9VMInternals.initialize(J9VMInternals.java:237)
at com.ibm.team.workitem.common.model.IWorkItem.<clinit>(IWorkItem.java:130)
at java.lang.J9VMInternals.initializeImpl(Native Method)
at java.lang.J9VMInternals.initialize(J9VMInternals.java:237)
at java.lang.Class.forNameImpl(Native Method)
at java.lang.Class.forName(Class.java:186)
at com.ibm.team.workitem.common.model.IWorkItem$$EnhancerByMockitoWithCGLIB$$75f09a5d.CGLIB$STATICHOOK5(<generated>)
at com.ibm.team.workitem.common.model.IWorkItem$$EnhancerByMockitoWithCGLIB$$75f09a5d.<clinit>(<generated>)
at java.lang.J9VMInternals.initializeImpl(Native Method)
at java.lang.J9VMInternals.initialize(J9VMInternals.java:237)
at java.lang.reflect.Constructor.newInstance(Constructor.java:541)
at org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator.newInstance(SunReflectionFactoryInstantiator.java:40)
at org.objenesis.ObjenesisBase.newInstance(ObjenesisBase.java:59)
at org.mockito.internal.creation.jmock.ClassImposterizer.createProxy(ClassImposterizer.java:128)
at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:63)
at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:56)
at org.mockito.internal.creation.CglibMockMaker.createMock(CglibMockMaker.java:23)
at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:26)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:51)
at org.mockito.Mockito.mock(Mockito.java:1243)
at org.mockito.Mockito.mock(Mockito.java:1120)
at com.rus.jazz.extension.service.mailnotification.MyFollowUpActionTest.setup(MyFollowUpActionTest.java:62)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.lang.reflect.Method.invoke(Method.java:619)
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)
Caused by: java.lang.NullPointerException
at com.ibm.team.workitem.common.internal.util.Utils.<clinit>(Utils.java:90)
at java.lang.J9VMInternals.initializeImpl(Native Method)
at java.lang.J9VMInternals.initialize(J9VMInternals.java:237)
... 32 more
4 answers
Hi Martin,
I do not know the exact reason to this but mockito is not completely aware of RTC Nature.
I also faced this problem but after digging a bit I figured out that for some of the RTC objects mocking doesn't work without starting the team platform. So add
I do not know the exact reason to this but mockito is not completely aware of RTC Nature.
I also faced this problem but after digging a bit I figured out that for some of the RTC objects mocking doesn't work without starting the team platform. So add
beforeTeamPlatform.startup();
IWorkItem workItem = mock(IWorkItem.class)
and then it starts working as expected.
What is line 62 of your test case? It looks like the Utils class is missing. Is the com.ibm.team.workitem.common in the features for the launcher for your test? Is it imported in the manifest.mf for the test package?
Here's a sample mock of a work item:
private IWorkItem aWorkItem(Identifier<IState> state, Identifier<IResolution> resolution, IApprovalDescriptor ... approvalDescriptors) throws Exception {
IWorkItem workItem= mock(IWorkItem.class);
when(workItem.getState2()).thenReturn(state);
when(workItem.getResolution2()).thenReturn(resolution);
IApprovals approvals= mock(IApprovals.class);
when(approvals.getDescriptors()).thenReturn(approvalDescriptors != null ? Arrays.asList(approvalDescriptors) : null);
when(workItem.getApprovals()).thenReturn(approvals);
return workItem;
}
Here is the manifest for a sample service test package... you may not need all of this...
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: %Bundle-Name.0
Bundle-SymbolicName: com.ibm.team.workitem.sample.tests;singleton:=true
Bundle-Version: 3.1.0.qualifier
Bundle-Vendor: %Bundle-Vendor.0
Bundle-Localization: plugin
Require-Bundle: org.eclipse.core.runtime,
org.eclipse.text,
com.ibm.team.repository.common,
com.ibm.team.repository.common.serialize,
com.ibm.team.repository.service,
com.ibm.team.repository.service.tests,
com.ibm.team.workitem.api.common,
com.ibm.team.workitem.common,
com.ibm.team.workitem.service,
org.junit,
com.ibm.team.process.common,
com.ibm.team.process.service,
com.ibm.team.workitem.common.tests,
org.apache.lucene2,
com.ibm.team.fulltext.service,
com.ibm.team.fulltext.common,
com.ibm.team.foundation.service,
com.ibm.team.calm.foundation.common,
com.ibm.team.calm.foundation.server,
com.ibm.team.process.service.migration,
com.ibm.team.repository.common.json,
com.ibm.team.repository.common.transport,
com.ibm.team.repository.tests.service,
com.hp.hpl.jena.rdf,
com.ibm.team.rtc.common.service,
com.ibm.team.dev.jdojo.runtime.common,
com.ibm.icu,
org.mockito.java,
com.ibm.team.jfs.app.distributed,
org.eclipse.test.performance,
javax.servlet,
com.ibm.team.rtc.common,
com.ibm.team.rtc.common.service.tests,
com.ibm.team.workitem.shared.common,
com.ibm.team.workitem.service.process,
com.ibm.team.rtc.foundation.tests.core
Export-Package: com.ibm.team.workitem.sample.tests
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Bundle-Activator: com.ibm.team.workitem.service.tests.WorkItemSampleTestsPlugin
Lawrence Smith [IBM]