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


2 comments:

  1. For instance, the recyclability of superior nanomaterials continues to be questioned; weapons manufacturing might turn into simpler; to not mention the implications for counterfeiting and on mental property. It might be maintained that in distinction to the economic paradigm whose aggressive dynamics were about economies of scale, commons-based peer production 3D printing might develop economies of scope. While MOTORCYCLE GEAR GLOVES the advantages of|some nice benefits of|the benefits of} scale rest on low-cost world transportation, the economies of scope share infrastructure prices , benefit of|benefiting from|profiting from} the capabilities of the fabrication instruments. In the United States, the FAA has anticipated a desire to make use of additive manufacturing techniques and has been contemplating how finest to manage this process. The FAA has jurisdiction over such fabrication because of|as a outcome of} all plane elements should be made underneath FAA production approval or underneath different FAA regulatory classes. In December 2016, the FAA approved the production of a 3D printed fuel nozzle for the GE LEAP engine.

    ReplyDelete