2

I am trying to create a Mock class for droplet. I am able to mock the repository calls and req.getParameter but need help on how to mock the repository item list from the repository. Below is the sample code.

for (final RepositoryItem item : skuList) {
    final String skuId = (String) item.getPropertyValue("id");
    final String skuType = (String) item.getPropertyValue("skuType");
    if (this.isLoggingDebug()) {
        this.logDebug("skuType [ " + skuType + " ]");
    }
    final String skuActive = (String) item.getPropertyValue("isActive");
    if EJSD.equalsIgnoreCase(skuType) && (skuActive.equals("1"))) {
        eSkuList.add(item);
        skuCode = (String) item.getPropertyValue(ESTConstants.SKU_MISC1);
        } else (PJPROMIS.equalsIgnoreCase(skuType) && skuId.contains("PP") && (skuActive.equals("1"))) {
        personalSkuList.add(item);
        String tmp = "";
        if (skuId.lastIndexOf("-") > -1) {
            tmp = skuId.substring(skuId.lastIndexOf("-") + 1);
            tmp = tmp.toUpperCase();
            if (this.getDefaultDisplayNameMap() != null) {
                String val = this.getDefaultDisplayNameMap().get(tmp);
                if (StringUtils.isNotEmpty(val)) {
                    displayNameMap.put(skuId, val);
                    } else {
                    val = (String) item.getPropertyValue("displayName");
                    displayNameMap.put(skuId, val);
                }
                } else {
                final String val = (String) item.getPropertyValue("displayName");
                displayNameMap.put(skuId, val);
            }
        }

    }
}
Charles
  • 50,943
  • 13
  • 104
  • 142
Abhishek
  • 25
  • 7
  • Where does 'skuList' come from? A simple way to start this would be to create a couple of `RepositoryItem` and then add them to a list and then mock the individual responses of the `getPropertyValue(String)` for each one of these `RepositoryItem` in the list. Also the [SSCCE](http://meta.stackexchange.com/questions/22754/sscce-how-to-provide-examples-for-programming-questions) doesn't show what you want to test from the output. Is this the `service` method in your droplet or is this a utility method? – radimpe Aug 12 '13 at 05:52
  • Thanks radimpe . This method is in my droplet and skulist is the list of repositoryItems – Abhishek Aug 12 '13 at 06:39
  • You need to create mock objects and then stub each call. e.g. when(repositoryItemMock.getRepositoryId("id")).thenReturn(yourSkuId); – Saurabh Aug 12 '13 at 08:32

2 Answers2

4

There are a multitude of ways to 'mock' the list. I've been doing it this was as I feel it is more readable.

    @Mock private RepositoryItem   skuMockA;
    @Mock private RepositoryItem   skuMockB;

    List<RepositoryItem> skuList = new ArrayList<RepositoryItem>();

    @BeforeMethod(groups = { "unit" })
    public void setup() throws Exception {
        testObj = new YourDropletName();
        MockitoAnnotations.initMocks(this);

        skuList = new ArrayList<RepositoryItem>();
        skuList.add(skuMockA);
        skuList.add(skuMockB);


        Mockito.when(skuMockA.getPropertyValue("id")).thenReturn("skuA");
        Mockito.when(skuMockA.getPropertyValue("skuType")).thenReturn(ActiveSkuDroplet.EJSD);
        Mockito.when(skuMockA.getPropertyValue(ESTConstants.SKU_MISC1)).thenReturn("skuCodeA");
        Mockito.when(skuMockA.getPropertyValue("displayName")).thenReturn("skuADisplayName");

        Mockito.when(skuMockB.getPropertyValue("id")).thenReturn("skuB-PP");
        Mockito.when(skuMockB.getPropertyValue("skuType")).thenReturn(ActiveSkuDroplet.PJPROMIS);
        Mockito.when(skuMockB.getPropertyValue(ESTConstants.SKU_MISC1)).thenReturn("skuCodeB");
        Mockito.when(skuMockB.getPropertyValue("displayName")).thenReturn("skuBDisplayName");
    }

So when you then call this within a test it will be something like this:

Mockito.when(someMethodThatReturnsAList).thenReturn(skuList);

So the key really is that you are not mocking the List but instead the contents of the List.

radimpe
  • 3,197
  • 2
  • 27
  • 46
  • Have one confusion in the code Abhishek shared. There is a for loop which is in Java5 way. The declaration/assignation of RepositoryItem is within the loop. How we can mock this RepositoryItem ?? – Saurabh Aug 12 '13 at 10:19
  • 1
    @Saurabh The loop iterates over the items that exist in the `skuList` that was setup in the `@BeforeMethod` so as long as the `skuList` contains `RepositoryItem` it will already exist as your 'mocked' `RepositoryItem`. You don't need to mock it separately. – radimpe Aug 12 '13 at 10:22
0

Creating a mock using mockito is a good option. But I am here explaining a different way of mocking the repository item.

Create a common implementation for RepositoryItem, say MockRepositoryItemImpl like this in your test package.

Public MockRepositoryItemImpl implements RepositoryItem {

    private Map<String, Object> properties;

    MockRepositoryItemImpl(){
        properties = new HashMap<>();
    }

    @override
    public Object getPropertyValue(String propertyName){
        return properties.get(propertyName);
    }

    @override
    public void setPropertyValue(String propertyName, Object propertyValue){
        properties.put(propertyName, propertyValue);
    }
}

Use this implementation to create the mock object in your test case.

RepositoryItem mockSKU = new MockRepositoryItemImpl();
mockSKU.setPropertyValue("id", "sku0001");
mockSKU.setPropertyValue("displayName", "Mock SKU");
mockSKU.setPropertyValue("skuType", "Type1");
mockSKU.setPropertyValue("isActive", "1");
Riju Thomas
  • 331
  • 3
  • 4