It's all about the answers!

Ask a question

Server side mock of Work Items with Mockito


Martin Wassermann (13864144) | asked Jan 19 '15, 9:11 a.m.
edited Jan 19 '15, 9:47 a.m.
 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



permanent link
Arshad Adavani (231220) | answered Sep 15 '16, 10:50 a.m.
edited Sep 15 '16, 10:51 a.m.
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
TeamPlatform.startup(); 
	
	
before
IWorkItem workItem = mock(IWorkItem.class)

and then it starts working as expected.

permanent link
Lawrence Smith (3764) | answered Apr 02 '15, 8:10 a.m.
JAZZ DEVELOPER
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]

permanent link
Daniel Stanaszek (11) | answered Nov 05 '21, 10:23 a.m.

Hello


What about mocking RTC service methods, like: getService(ServiceName.class)? Did anyone try it and can share some examples?


Comments
1
David Honey commented Nov 08 '21, 4:47 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
Daniel, if you have a question to ask, please submit it as a new question rather than appending it to a dormant 6+ year old one.

Thanks,
David

-1
permanent link
Joannie Beier (9) | answered Nov 08 '21, 6:17 a.m.

 Mocking and stubbing are the cornerstones of having quick and simple unit tests. Mocks are useful if you have a dependency on an external system, file reading takes too long, the database connection is unreliable, or if you don’t want to send an email after every test.


Comments
Ralph Schoon commented Nov 08 '21, 7:08 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

This answer is pointless, does not belong here and is likely only the alibi answer that will soon be made to be spam linked to some obscure web page and be deleted as spam. 

Your answer


Register or to post your answer.


Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.