How work with if-else in mockito

12,793 views
Skip to first unread message

Vivek

unread,
Jul 14, 2014, 12:46:58 PM7/14/14
to moc...@googlegroups.com
Hi All

I just stepped in learning the mocking frameworks. I have below method that want to test.
public  Map getConfigProps(Configuration config)
    {
        Map props=new HashMap();
        String  customerSecret,refreshToken;
        props.put("instanceUrl",(String)config.get("instanceurl", ""));
        props.put("accesstoken",(String)config.get("accesstoken", ""));
        props.put("clientId",(String)config.get("customerkey", ""));

        String customerSecretTemp = (String)config.get("customersecret", "");
        String refreshTokenTemp = (String)config.get("refreshtoken", "");
        customerSecret = customerSecretTemp;
        refreshToken = refreshTokenTemp;
        try {
            if (this.cryptoSupport.isProtected(customerSecretTemp)) {
                customerSecret = this.cryptoSupport.unprotect(customerSecretTemp);
            }
            if (this.cryptoSupport.isProtected(refreshTokenTemp)) {
                refreshToken = this.cryptoSupport.unprotect(refreshTokenTemp);
            }
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        props.put("customerSecret",customerSecret);
        props.put("refreshToken",refreshToken);
        return props;
    }

Test Case that i written so far :

@Test
    public void testGetConfigProps() throws Exception {
            Configuration config=mock(Configuration.class);
            Map props=mock(Map.class);
            CryptoSupport   cryptoSupport=mock(CryptoSupport.class);
            when(config.get("instanceurl","")).thenReturn("instanceurlVal").thenReturn(null);
            props.put("instanceurl","instanceurlVal");
            when(config.get("accesstoken","")).thenReturn("accesstokenVal").thenReturn(null);
            props.put("accesstoken","accesstokenVal");
            when(config.get("clientId","")).thenReturn("clientIdVal").thenReturn(null);
            props.put("clientId","clientIdVal");
            when(config.get("customersecret","")).thenReturn("customersecretVal").thenReturn(null);
            when(config.get("refreshToken","")).thenReturn("refreshTokenVal").thenReturn(null);

    }

Not able to understand how to proceed from here. Any suggestions.

Thanks


KARR, DAVID

unread,
Jul 14, 2014, 1:41:30 PM7/14/14
to moc...@googlegroups.com

You have several problems here.

 

One of the important points of a unit test of some CUT (code under test) is that you actually have to execute the CUT in order to test it.  The flow of a test typically starts with a “setup” phase, where you assign some property values, create some mocks, and inject those mocks.  You might then have a “execute CUT” phase, where you now execute the method of the CUT that you want to exercise and test.  This is followed by a “assert and verify” phase, where you inspect the side effects of the CUT method and verify their correctness, and you also verify that particular business-logic-critical methods of your mocked instances were executed properly.

 

In your test, you seem to sort of have a “setup” phase, but you never execute the CUT, and you certainly don’t verify the side-effects of the CUT.

 

You should probably remove the creation of the “props” Map in your test, as you are only attempting to put things into it, but you never use it.  In addition, you should almost never create a mock instance of a Map, and because you’re doing that, your “put” calls are ineffective.  If you need to supply a Map with specific contents, just create an ordinary Map and populate it.

 

--
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.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted

Vivek

unread,
Jul 15, 2014, 5:40:47 AM7/15/14
to moc...@googlegroups.com
Solved that issue, But now error is something like : 
java.lang.NullPointerException
at com.ig.igwebcms.services.impl.SalesforceLeadSearch1.getConfigProps(SalesforceLeadSearch1.java:131)
at com.ig.igwebcms.services.impl.SalesforceLeadSearch1TestN.testGetConfigProps(SalesforceLeadSearch1TestN.java:70)
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:606)
junit.framework.AssertionFailedError: 
Expected :{accesstoken=accesstokenVal, instanceurl=instanceurlVal, refreshtoken=refreshTokenValNew, customerkey=clientIdVal, customersecret=customersecretValNew}
Actual   :{accesstoken=accesstokenVal, customerkey=clientIdVal, refreshToken=refreshTokenValNew, customerSecret=customersecretValNew, instanceUrl=instanceurlVal}


@Test
    public void testGetConfigProps() throws Exception {
        SalesforceLeadSearch1 clt=new SalesforceLeadSearch1();

        Configuration config=mock(Configuration.class);
            Map props=new HashMap();
            CryptoSupport  cryptoSupport=mock(CryptoSupport.class);
            when(config.get("instanceurl","")).thenReturn("instanceurlVal");
            props.put("instanceurl", "instanceurlVal");
            when(config.get("accesstoken","")).thenReturn("accesstokenVal");
            props.put("accesstoken", "accesstokenVal");
            when(config.get("customerkey","")).thenReturn("clientIdVal");
            props.put("customerkey", "clientIdVal");

            when(cryptoSupport.isProtected("customersecretVal")).thenReturn(true);
            when(cryptoSupport.unprotect("customersecretVal")).thenReturn("customersecretValNew");
            when(config.get("customersecret","")).thenReturn("customersecretValNew");
            props.put("customersecret","customersecretValNew");

            when(cryptoSupport.isProtected("refreshTokenVal")).thenReturn(true);
            when(cryptoSupport.unprotect("refreshTokenVal")).thenReturn("refreshTokenValNew");
            when(config.get("refreshtoken", "")).thenReturn("refreshTokenValNew");
            props.put("refreshtoken","refreshTokenValNew");

            assertEquals(props,clt.getConfigProps(config));
    }


On Tuesday, July 15, 2014 1:04:28 PM UTC+5:30, Vivek wrote:
Hi Thanks, I just tried and modified but getting some excption :
 public void testGetConfigProps() throws Exception {
        SalesforceLeadSearch1 clt=new SalesforceLeadSearch1();

        Configuration config=mock(Configuration.class);
            Map props=new HashMap();
            CryptoSupport  cryptoSupport=mock(CryptoSupport.class);
            when(config.get("instanceurl","")).thenReturn("instanceurlVal").thenReturn(null);
            props.put("instanceurl", "instanceurlVal");
            when(config.get("accesstoken","")).thenReturn("accesstokenVal").thenReturn(null);
            props.put("accesstoken", "accesstokenVal");
            when(config.get("clientId","")).thenReturn("clientIdVal").thenReturn(null);
            props.put("clientId", "clientIdVal");
            when(config.get("customersecret","")).thenReturn("customersecretVal").thenReturn("");
            when(cryptoSupport.isProtected("customersecretVal")).thenReturn(true);
            when(cryptoSupport.unprotect("customersecretVal")).thenReturn("customersecretValNew");
            when(config.get("customersecret","")).thenReturn("customersecretValNew").thenReturn(null);
            props.put("customersecret","customersecretValNew");
            when(config.get("refreshToken", "")).thenReturn("refreshTokenVal").thenReturn(null);
            when(cryptoSupport.isProtected("refreshTokenVal")).thenReturn(true);
            when(cryptoSupport.unprotect("refreshTokenVal")).thenReturn("refreshTokenValNew");
            when(config.get("refreshToken", "")).thenReturn("refreshTokenValNew").thenReturn(null);
            props.put("refreshToken","refreshTokenValNew");
            assertEquals(props,clt.getConfigProps(config));
}

Exception is : 

Expected :{accesstoken=accesstokenVal, instanceurl=instanceurlVal, refreshToken=refreshTokenValNew, clientId=clientIdVal, customersecret=customersecretValNew}
Actual   :{accesstoken=accesstokenVal, refreshToken=null, customerSecret=customersecretValNew, clientId=null, instanceUrl=instanceurlVal}

KARR, DAVID

unread,
Jul 15, 2014, 10:28:29 AM7/15/14
to moc...@googlegroups.com

Learn how to execute your tests in the debugger.  Set a breakpoint at that line and determine what is null, which should lead you to your next steps. 

 

From: moc...@googlegroups.com [mailto:moc...@googlegroups.com] On Behalf Of Vivek


Sent: Tuesday, July 15, 2014 2:41 AM
To: moc...@googlegroups.com

--

Reply all
Reply to author
Forward
0 new messages