Need assistance on mocking AEM adobe cq Workflow objects

599 views
Skip to first unread message

is.aka...@gmail.com

unread,
Oct 29, 2015, 11:46:17 AM10/29/15
to mockito
Hi, I was trying to get the workflow information from the below code. New to Mockito framework. Please guide me where i went wrong with in the below code in blue.

errors:

org.mockito.exceptions.misusing.UnfinishedStubbingException: 
Unfinished stubbing detected here:
-> at com.adobe.acs.commons.dam.RenditionModifyingProcessTest.test_with_rendition_arg_getting_real_rendition(RenditionModifyingProcessTest.java:143)

E.g. thenReturn() may be missing.
Examples of correct stubbing:
    when(mock.isOk()).thenReturn(true);
    when(mock.isOk()).thenThrow(exception);
    doThrow(exception).when(mock).someVoidMethod();
Hints:
 1. missing thenReturn()
 2. you are trying to stub a final method, you naughty developer!

at com.adobe.acs.commons.dam.RenditionModifyingProcessTest.test_with_rendition_arg_getting_real_rendition(RenditionModifyingProcessTest.java:143)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
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.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
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.runners.ParentRunner.run(ParentRunner.java:236)
at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
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:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

code below: 


 @Test
    public void test_with_rendition_arg_getting_real_rendition() throws Exception {
        String path = "/content/dam/some/path.ext";

        WorkItem workItem = mock(WorkItem.class);
        WorkflowData data = mock(WorkflowData.class);
        
        Workflow workflow = mock(Workflow.class);        
        when(workflow.getWorkflowData()).thenReturn(data);
        when(workflow.getInitiator()).thenReturn(workflowSession.getUser().getName());
            
        
        when(workItem.getWorkflowData()).thenReturn(data);
        when(data.getPayloadType()).thenReturn(AbstractAssetWorkflowProcess.TYPE_JCR_PATH);
        when(data.getPayload()).thenReturn(path);
        System.out.println("payload: "+data.getPayload());
         //+workItem.getWorkflow().getInitiator()
        
        System.out.println(" initiator: "+workflow.getInitiator());
        
        Resource resource = mock(Resource.class);
        Asset asset = mock(Asset.class);
        Rendition rendition = mock(Rendition.class);
        when(resource.adaptTo(Asset.class)).thenReturn(asset);
        when(resource.getResourceType()).thenReturn(DamConstants.NT_DAM_ASSET);
        when(resourceResolver.getResource(path)).thenReturn(resource);
        when(asset.getRendition(isA(RenditionPicker.class))).thenReturn(rendition);

        when(rendition.getStream()).then(new Answer<InputStream>() {

            @Override
            public InputStream answer(InvocationOnMock invocation) throws Throwable {
                return getClass().getResourceAsStream("/img/test.png");
            }
        });

        when(harness.processLayer(any(Layer.class), eq(rendition), eq(workflowSession), any(String[].class)))
                .thenAnswer(new Answer<Layer>() {

                    @Override
                    public Layer answer(InvocationOnMock invocation) throws Throwable {
                        return (Layer) invocation.getArguments()[0];
                    }
                });

        MetaDataMap metaData = new SimpleMetaDataMap();
        metaData.put("PROCESS_ARGS", "renditionName:test");

        process.execute(workItem, workflowSession, metaData);

        verify(harness, times(1)).processLayer(any(Layer.class), eq(rendition), eq(workflowSession), any(String[].class));
        verify(harness, times(1)).saveImage(eq(asset), eq(rendition), any(Layer.class), eq("image/png"), eq(0.6));
    }

}

Eric Lefevre-Ardant

unread,
Oct 30, 2015, 3:17:41 AM10/30/15
to moc...@googlegroups.com
You do not make clear how workflowSession is instantiated. Also, it is not clear what exact line is causing the exception.

My guess is that workflowSession is itself a mock. In that case, the line
        when(workflow.getInitiator()).thenReturn(workflowSession.getUser().getName());
is problematic I think. I believe that Mockito would mix up the configuration of workflow and believe that you are starting to configure workflowSession as well, since you are making a reference to it. Try to replace workflowSession.getUser().getName() with an hard-coded value instead.

Also, please provide more details on workflowSession and the exact failing line.

--
You received this message because you are subscribed to the Google Groups "mockito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mockito+u...@googlegroups.com.
To post to this group, send email to moc...@googlegroups.com.
Visit this group at http://groups.google.com/group/mockito.
To view this discussion on the web visit https://groups.google.com/d/msgid/mockito/17457aaf-9ce9-484b-9a6b-b28b48beb5b7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages