cancel
Showing results for 
Search instead for 
Did you mean: 

Nested CallActivity Mockito Serialization Problem

gokceng1
Champ in-the-making
Champ in-the-making
Hi,
You can find the test project here: https://docs.google.com/open?id=0B53lOHnXR0yJVHlqS3lRajhqUXc

I have 3 bpmn process. Level1 calls Level2 and Level2 calls Level3. Level3 has only one service task which uses an expression.
I pass a spring managed bean from L1 to L2 and then L2 to L3. It works when I don't mock it. It sometimes works when I mock the bean.

In project there are 2 test classes:
ProcessTestNestedCallActivityProcess.java(uses activitiTestContext.xml):
Real spring bean.
ProcessTestNestedCallActivityProcessWithMock.java(uses activitiMockContext.xml):
There are 2 test methods.
First one someTimesFails() fails sometimes as name implies.
Second one alwaysFails() fails all the time with a serialization exception.

The difference is, in first test method, I call real method when service bean's method is called. On the contrary I call thenAnswer in the second test method.


@Test
@Deployment(resources = { "diagram/dummy/level1Process.bpmn", "diagram/dummy/level2Process.bpmn", "diagram/dummy/level3Process.bpmn" })
public void someTimesFails() throws Exception
{
   dummyService = Mockito.mock(DummyService.class, Mockito.withSettings().serializable());
   Mockito.when(dummyService.dummyMethod("Level 3 Service Task")).thenCallRealMethod();
   …
}


@Test
@Deployment(resources = { "diagram/dummy/level1Process.bpmn", "diagram/dummy/level2Process.bpmn", "diagram/dummy/level3Process.bpmn" })
public void alwaysFails() throws Exception
{
   dummyService = Mockito.mock(DummyService.class, Mockito.withSettings().serializable());
   Mockito.when(dummyService.dummyMethod("Level 3 Service Task")).thenAnswer(new Answer<Serializable>()
   {
      @Override
      public Serializable answer(InvocationOnMock invocation) throws Throwable
      {
         System.out.println("Called with mock");
         return "Called with mock";
      }
   });
   …
}


In second test method, I got this:

2344 [main] ERROR org.activiti.engine.impl.interceptor.CommandContext  - Error while closing command context
org.activiti.engine.ActivitiException: Couldn't serialize value 'Mock for Serializable, hashCode: 11835846' in variable 'service'
   at org.activiti.engine.impl.variable.SerializableType.serialize(SerializableType.java:98)
   at org.activiti.engine.impl.variable.SerializableType.setValue(SerializableType.java:73)
   at org.activiti.engine.impl.persistence.entity.VariableInstanceEntity.setValue(VariableInstanceEntity.java:194)
   at org.activiti.engine.impl.persistence.entity.VariableInstanceEntity.create(VariableInstanceEntity.java:72)
   at org.activiti.engine.impl.persistence.entity.VariableInstanceEntity.createAndInsert(VariableInstanceEntity.java:58)
   at org.activiti.engine.impl.persistence.entity.VariableScopeImpl.createVariableInstance(VariableScopeImpl.java:361)
   at org.activiti.engine.impl.persistence.entity.VariableScopeImpl.createVariableLocal(VariableScopeImpl.java:292)
   at org.activiti.engine.impl.persistence.entity.VariableScopeImpl.createVariableLocal(VariableScopeImpl.java:279)
   at org.activiti.engine.impl.persistence.entity.VariableScopeImpl.setVariable(VariableScopeImpl.java:254)
   at org.activiti.engine.impl.persistence.entity.VariableScopeImpl.setVariable(VariableScopeImpl.java:237)
   at org.activiti.engine.impl.bpmn.behavior.CallActivityBehavior.execute(CallActivityBehavior.java:82)
   at org.activiti.engine.impl.pvm.runtime.AtomicOperationActivityExecute.execute(AtomicOperationActivityExecute.java:44)
   at org.activiti.engine.impl.interceptor.CommandContext.performOperation(CommandContext.java:85)
   at org.activiti.engine.impl.persistence.entity.ExecutionEntity.performOperationSync(ExecutionEntity.java:535)
   at org.activiti.engine.impl.persistence.entity.ExecutionEntity.performOperation(ExecutionEntity.java:530)
   at org.activiti.engine.impl.pvm.runtime.AtomicOperationTransitionNotifyListenerStart.eventNotificationsCompleted(AtomicOperationTransitionNotifyListenerStart.java:52)
   at org.activiti.engine.impl.pvm.runtime.AbstractEventAtomicOperation.execute(AbstractEventAtomicOperation.java:56)
   at org.activiti.engine.impl.interceptor.CommandContext.performOperation(CommandContext.java:85)
   at org.activiti.engine.impl.persistence.entity.ExecutionEntity.performOperationSync(ExecutionEntity.java:535)
   at org.activiti.engine.impl.persistence.entity.ExecutionEntity.performOperation(ExecutionEntity.java:530)
   at org.activiti.engine.impl.pvm.runtime.AbstractEventAtomicOperation.execute(AbstractEventAtomicOperation.java:49)
   at org.activiti.engine.impl.interceptor.CommandContext.performOperation(CommandContext.java:85)
   at org.activiti.engine.impl.persistence.entity.ExecutionEntity.performOperationSync(ExecutionEntity.java:535)
   at org.activiti.engine.impl.persistence.entity.ExecutionEntity.performOperation(ExecutionEntity.java:530)
   at org.activiti.engine.impl.pvm.runtime.AtomicOperationTransitionCreateScope.execute(AtomicOperationTransitionCreateScope.java:49)
   at org.activiti.engine.impl.interceptor.CommandContext.performOperation(CommandContext.java:85)
   at org.activiti.engine.impl.persistence.entity.ExecutionEntity.performOperationSync(ExecutionEntity.java:535)
   at org.activiti.engine.impl.persistence.entity.ExecutionEntity.performOperation(ExecutionEntity.java:530)
   at org.activiti.engine.impl.pvm.runtime.AtomicOperationTransitionNotifyListenerTake.execute(AtomicOperationTransitionNotifyListenerTake.java:65)
   at org.activiti.engine.impl.interceptor.CommandContext.performOperation(CommandContext.java:85)
   at org.activiti.engine.impl.persistence.entity.ExecutionEntity.performOperationSync(ExecutionEntity.java:535)
   at org.activiti.engine.impl.persistence.entity.ExecutionEntity.performOperation(ExecutionEntity.java:530)
   at org.activiti.engine.impl.pvm.runtime.AtomicOperationTransitionDestroyScope.execute(AtomicOperationTransitionDestroyScope.java:115)
   at org.activiti.engine.impl.interceptor.CommandContext.performOperation(CommandContext.java:85)
   at org.activiti.engine.impl.persistence.entity.ExecutionEntity.performOperationSync(ExecutionEntity.java:535)
   at org.activiti.engine.impl.persistence.entity.ExecutionEntity.performOperation(ExecutionEntity.java:530)
   at org.activiti.engine.impl.pvm.runtime.AtomicOperationTransitionNotifyListenerEnd.eventNotificationsCompleted(AtomicOperationTransitionNotifyListenerEnd.java:36)
   at org.activiti.engine.impl.pvm.runtime.AbstractEventAtomicOperation.execute(AbstractEventAtomicOperation.java:56)
   at org.activiti.engine.impl.interceptor.CommandContext.performOperation(CommandContext.java:85)
   at org.activiti.engine.impl.persistence.entity.ExecutionEntity.performOperationSync(ExecutionEntity.java:535)
   at org.activiti.engine.impl.persistence.entity.ExecutionEntity.performOperation(ExecutionEntity.java:530)
   at org.activiti.engine.impl.pvm.runtime.AbstractEventAtomicOperation.execute(AbstractEventAtomicOperation.java:49)
   at org.activiti.engine.impl.interceptor.CommandContext.performOperation(CommandContext.java:85)
   at org.activiti.engine.impl.persistence.entity.ExecutionEntity.performOperationSync(ExecutionEntity.java:535)
   at org.activiti.engine.impl.persistence.entity.ExecutionEntity.performOperation(ExecutionEntity.java:530)
   at org.activiti.engine.impl.persistence.entity.ExecutionEntity.take(ExecutionEntity.java:370)
   at org.activiti.engine.impl.bpmn.behavior.BpmnActivityBehavior.performOutgoingBehavior(BpmnActivityBehavior.java:102)
   at org.activiti.engine.impl.bpmn.behavior.BpmnActivityBehavior.performDefaultOutgoingBehavior(BpmnActivityBehavior.java:51)
   at org.activiti.engine.impl.bpmn.behavior.FlowNodeActivityBehavior.leave(FlowNodeActivityBehavior.java:44)
   at org.activiti.engine.impl.bpmn.behavior.FlowNodeActivityBehavior.execute(FlowNodeActivityBehavior.java:36)
   at org.activiti.engine.impl.pvm.runtime.AtomicOperationActivityExecute.execute(AtomicOperationActivityExecute.java:44)
   at org.activiti.engine.impl.interceptor.CommandContext.performOperation(CommandContext.java:85)
   at org.activiti.engine.impl.persistence.entity.ExecutionEntity.performOperationSync(ExecutionEntity.java:535)
   at org.activiti.engine.impl.persistence.entity.ExecutionEntity.performOperation(ExecutionEntity.java:530)
   at org.activiti.engine.impl.pvm.runtime.AtomicOperationProcessStartInitial.eventNotificationsCompleted(AtomicOperationProcessStartInitial.java:46)
   at org.activiti.engine.impl.pvm.runtime.AbstractEventAtomicOperation.execute(AbstractEventAtomicOperation.java:56)
   at org.activiti.engine.impl.interceptor.CommandContext.performOperation(CommandContext.java:85)
   at org.activiti.engine.impl.persistence.entity.ExecutionEntity.performOperationSync(ExecutionEntity.java:535)
   at org.activiti.engine.impl.persistence.entity.ExecutionEntity.performOperation(ExecutionEntity.java:530)
   at org.activiti.engine.impl.pvm.runtime.AtomicOperationProcessStart.eventNotificationsCompleted(AtomicOperationProcessStart.java:45)
   at org.activiti.engine.impl.pvm.runtime.AbstractEventAtomicOperation.execute(AbstractEventAtomicOperation.java:56)
   at org.activiti.engine.impl.interceptor.CommandContext.performOperation(CommandContext.java:85)
   at org.activiti.engine.impl.persistence.entity.ExecutionEntity.performOperationSync(ExecutionEntity.java:535)
   at org.activiti.engine.impl.persistence.entity.ExecutionEntity.performOperation(ExecutionEntity.java:530)
   at org.activiti.engine.impl.persistence.entity.ExecutionEntity.start(ExecutionEntity.java:325)
   at org.activiti.engine.impl.cmd.StartProcessInstanceCmd.execute(StartProcessInstanceCmd.java:80)
   at org.activiti.engine.impl.cmd.StartProcessInstanceCmd.execute(StartProcessInstanceCmd.java:33)
   at org.activiti.engine.impl.interceptor.CommandExecutorImpl.execute(CommandExecutorImpl.java:24)
   at org.activiti.engine.impl.interceptor.CommandContextInterceptor.execute(CommandContextInterceptor.java:60)
   at org.activiti.spring.SpringTransactionInterceptor$1.doInTransaction(SpringTransactionInterceptor.java:42)
   at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:130)
   at org.activiti.spring.SpringTransactionInterceptor.execute(SpringTransactionInterceptor.java:40)
   at org.activiti.engine.impl.interceptor.LogInterceptor.execute(LogInterceptor.java:32)
   at org.activiti.engine.impl.RuntimeServiceImpl.startProcessInstanceByKey(RuntimeServiceImpl.java:51)
   at ProcessTestNestedCallActivityProcessWithMock.alwaysFails(ProcessTestNestedCallActivityProcessWithMock.java:103)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:601)
   at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
   at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
   at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
   at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
   at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
   at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
   at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
   at org.junit.rules.TestWatchman$1.evaluate(TestWatchman.java:48)
   at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
   at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
   at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
   at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
   at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
   at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
   at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
   at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
   at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
   at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
   at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
   at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
   at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
   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.io.NotSerializableException: ProcessTestNestedCallActivityProcessWithMock$1
   at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1180)
   at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
   at java.util.concurrent.ConcurrentLinkedQueue.writeObject(ConcurrentLinkedQueue.java:766)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:601)
   at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:975)
   at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1480)
   at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416)
   at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)
   at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1528)
   at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1493)
   at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416)
   at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)
   at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
   at java.util.LinkedList.writeObject(LinkedList.java:1118)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:601)
   at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:975)
   at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1480)
   at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416)
   at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)
   at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1528)
   at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1493)
   at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416)
   at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)
   at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1528)
   at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1493)
   at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416)
   at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)
   at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1528)
   at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1493)
   at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416)
   at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)
   at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1528)
   at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1493)
   at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416)
   at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)
   at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1528)
   at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1493)
   at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416)
   at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)
   at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1528)
   at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1493)
   at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416)
   at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)
   at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
   at org.activiti.engine.impl.variable.SerializableType.serialize(SerializableType.java:96)
   … 105 more

Is there a restriction on using thenAnswer method in unit tests?
4 REPLIES 4

jbarrez
Star Contributor
Star Contributor
I've got a hard time following you're explanation, but from the stacktrace it seems that the mocked object is not serializable, but the object is used as a process variable, thence the need for it to be serialized.

gokceng1
Champ in-the-making
Champ in-the-making
Sorry for complexity of situation, I try to make it simple here.
I have one object which is dummyService used in process as a process variable. DummyService class implements Serializable so using it as a Spring bean there is no problem. My problem is that:

Initializing dummyService as a Mock using Mockito like this:

dummyService = Mockito.mock(DummyService.class, Mockito.withSettings().serializable());
Mockito.when(dummyService.dummyMethod("Level 3 Service Task")).thenCallRealMethod();

OR

dummyService = Mockito.mock(DummyService.class, Mockito.withSettings().serializable());
Mockito.when(dummyService.dummyMethod("Level 3 Service Task")).thenReturn(null);

yields no problem.

But initializing  dummyService as a Mock using Mockito like that:
  this.dummyService = Mockito.mock(DummyService.class, Mockito.withSettings().serializable());
  Mockito.when(this.dummyService.dummyMethod("Level 3 Service Task")).thenAnswer(new Answer<String>()
  {
   @Override
   public String answer(final InvocationOnMock invocation) throws Throwable
   {
    System.out.println("Called with mock");
    return "Called with mock";
   }
  });

You can see both uses Mockito.withSettings().serializable() but the difference is in the Mockito.when part. So I've asked about whether there is a known limitation about that point.

frederikherema1
Star Contributor
Star Contributor
I think the mockito forum is a better place for this, as this is a mockito serialization-issue.

Perhaps your "answer" object should be serializable as weLl?

gokceng1
Champ in-the-making
Champ in-the-making
Thanks Frederik, I tried it without Activiti and Answer class seems to create problem as you said. Anybody concerned about the situation can follow Mockito mailing list topic here