dhamini-poornachandra / mockito

Automatically exported from code.google.com/p/mockito
0 stars 0 forks source link

Wanted but not invoked: Actually, there were zero interactions with this mock #413

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Error:

Wanted but not invoked:
provider.startAudit(
    "cn=dlakshman,cn=organizational users,o=system,cn=cordys,cn=Audit,o=vanenburg.com",
    "XMLS tore",
    "/Cordys/Test/test.log",
    "UpdateXMLObject",
    "o=system,cn=cordys,cn=Audit,o=vanenburg.com",
    "Add",
    "input",
    Cordys.XMLStore.Messages.xmlstoreUpdateEntryStart, 
 ""
);->

at 
com.cordys.XMLStoreAudit.MockXMLStoreAuditTest.verifyUpdate(MockXMLStoreAuditTes
t.java:146)
Actually, there were zero interactions with this mock.

    at com.cordys.XMLStoreAudit.MockXMLStoreAuditTest.verifyUpdate(MockXMLStoreAuditTest.java:146)
    at com.cordys.XMLStoreAudit.MockXMLStoreAuditTest.testAuditXMLStoreCreate(MockXMLStoreAuditTest.java:102)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

test class:
{@RunWith(MockitoJUnitRunner.class)
public class MockXMLStoreAuditTest
{
    private static final String     XMLSTORE_ARTIFACT_TYPE  = "XMLS tore";

    private static final String     USER_DN                 = "cn=dlakshman,cn=organizational users,o=system,cn=cordys,cn=Audit,o=vanenburg.com";

    private static final String     DESCRIPTION             = "UpdateXMLObject";

    private static final String     ORG_DN                  = "o=system,cn=cordys,cn=Audit,o=vanenburg.com";

    public @Rule
    SystemPropertySetter            propertiesSetter        = new SystemPropertySetter();

    @Mock
    AuditProvider                   provider;
    @Mock
    AuditRecording                  recording;

    private Connector connector;
    @Before
    public void setAuditInspector() throws ExceptionGroup, DirectoryException
    {
        propertiesSetter.setValue(Audit.AUDITPROVIDER_PROPERTY_NAME, TestAuditProvider.class.getCanonicalName());
        TestAuditProvider.auditProvider = provider;

        when(provider.isAuditEnabled(anyString(), anyString())).thenReturn(true);
        when(provider.startAudit(anyString(),
                                isA(String.class),
                                isA(String.class),
                                isA(String.class),
                                isA(String.class),
                                isA(String.class),
                                anyString(),
                                isA(IStringResource.class),
                                anyVararg())).thenReturn(recording);

        connector = Connector.getInstance("XMLStore");
        if(!connector.isOpen())
        {
            connector.open();
        }
    }
    @Test
    public void testAuditXMLStoreCreate () throws Exception
    {
        updateXMLObject("Add");
        verifyUpdate("Add");
    }

    private void updateXMLObject ( String operation ) throws DirectoryException, XMLException, TimeoutException,
            ExceptionGroup, UnsupportedEncodingException
    {
        int request = 0;
        int response = 0;
        try
        {
            request = connector.createSOAPMethod("http://schemas.cordys.com/1.0/xmlstore", "UpdateXMLObject");
            Node.appendToChildren(XPath.getFirstMatch(".//" + operation + "/tuple", null,NomUtil.readResourceAsNom(getClass(), "UpdateXMLRequests.xml")), request);
            response = connector.sendAndWait(request);
        }
        finally
        {
            Node.delete(request);
            Node.delete(response);
        }
    }

        private void verifyUpdate (String actionType)
        {
            verify(provider).startAudit(USER_DN,
                                        XMLSTORE_ARTIFACT_TYPE,
                                        "/Cordys/Test/test.log",
                                        DESCRIPTION,
                                        ORG_DN,
                                        actionType,
                                        "input",
                                        Messages.XMLSTORE_UPDATE_ENTRY_START,
                                        "");
            verify(recording).completed(AuditStatus.COMPLETE, Messages.XMLSTORE_AUDIT_ENTRY_COMPLETE, anyString());
            verify(provider).isAuditEnabled(ORG_DN, XMLSTORE_ARTIFACT_TYPE);

        }
}

The following statement will call startAudit() method internally.

response = connector.sendAndWait(request);

I think it is not calling the mocked startAudit()(from TestAuditProvider) 
instead it is calling actual startAudit()(from AuditProvider).

please help:

thanks

Original issue reported on code.google.com by dlaksh...@cordys.com on 25 Jan 2013 at 12:48

GoogleCodeExporter commented 8 years ago
SystemPropertySetter  is to set system properties with key and value.

Mocking is not happening I guess.

Original comment by dlaksh...@cordys.com on 25 Jan 2013 at 12:50

GoogleCodeExporter commented 8 years ago
Hi,

Your test code looks complex, I'm not sure what's wrong because it's not 
straightforward. Plus it seems the code is using statics which is most of the 
time a bad idea.

In my opinion you should check that the code is actually calling the real mock 
instance, or that the method being mocked is not final.

I'll invalidate the issue, but if you feel there is a real bug, please say so :)
Also instead of creating an issue please ask on the mailing-list.
Hope that helps.

Original comment by brice.du...@gmail.com on 28 Jan 2013 at 3:30