By kswaughs | Tuesday, June 7, 2016

How to mock object that implements multiple interfaces

When a java class implements multiple interfaces then it is little difficult to mock and set expectations with type of its interfaces. Mockito provides some useful method Mockito.withSettings().extraInterfaces to overcome this difficulty. Let us see the below example.

Interface User
package com.kswaughs;

public interface User {
    
    public String getUserName(String Id);

}

Interface Customer
package com.kswaughs;

public interface Customer {
    
    public String getCustomerName(String Id);

}

Sample Java class that implements multiple interfaces
package com.kswaughs;

public class UserImpl implements User, Customer {

    @Override
    public String getCustomerName(String Id) {
        
        return "MyCustomer";
    }

    @Override
    public String getUserName(String Id) {
        
        return "MyUser";
    }

}

Junit Test class to mock and test UserImpl class
package com.kswaughs;

import junit.framework.Assert;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Matchers;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
public class UserTest {
    
    private User mockWithTestData() {
        
        User user = PowerMockito.mock(UserImpl.class , 
             Mockito.withSettings().extraInterfaces(Customer.class));
                
        PowerMockito.when(
             user.getUserName(Matchers.eq("test"))).thenReturn("TestUser");
        
        Customer customer = (Customer) user;
        
        PowerMockito.when(
             customer.getCustomerName(Matchers.eq("test"))).thenReturn("TestCustomer");
        
        return user;
    }
    
        
    @Test
    public void testNames() throws Exception {
        
        // Test 1 : with test data
        
        UserImpl userImplMock = (UserImpl) mockWithTestData();
        
        String userName = userImplMock.getUserName("test");
        String customerName = userImplMock.getCustomerName("test");
        
        Assert.assertEquals("TestUser", userName);
        Assert.assertEquals("TestCustomer", customerName);
        
        logOutput("1 : With Mock Data", userName, customerName);
        
        // Test 2 : with implementation data
        UserImpl user = new UserImpl();
        
        userName = user.getUserName("12345");
        customerName = user.getCustomerName("12345");
        
        Assert.assertEquals("MyUser", userName);
        Assert.assertEquals("MyCustomer", customerName);
        
        logOutput("2 : With Real Data", userName, customerName);
        
    }
        

    private void logOutput(String testCase, String userName, String customerName) {
        
        System.out.println("***** Test "+testCase +" ***** :");
        System.out.println("userName :" +userName);
        System.out.println("customerName :" +customerName);
        System.out.println("\n");
    }


Console Logs
***** Test 1 : With Mock Data ***** :
userName :TestUser
customerName :TestCustomer


***** Test 2 : With Real Data ***** :
userName :MyUser
customerName :MyCustomer

Recommend this on


1 comment: