uma
uma

Reputation: 1575

How to convert this to Mockito 2.2?

I upgraded my mockito version but now , import static se.cambio.cosmic.silmigrator.sil.port.Whitebox.setInternalState; this import not available. I replaced it using powerMock but now the unit test is fails. I need some expert help to correct this issue?

import com.google.common.collect.ImmutableList;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import se.cambio.cosmic.silmigrator.external.sil.ws.Atc;
import se.cambio.cosmic.silmigrator.external.sil.ws.SilException_Exception;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*;

import static org.testng.Assert.assertEquals;
import static se.cambio.cosmic.silmigrator.sil.port.Whitebox.setInternalState;

public class AbstractCachedArrayTest
{
  private AbstractCachedArray<Atc, String> cachedArray;

  private Map<String, Atc> cacheMap;

  @BeforeMethod
  public void setUp() 
  {
    cacheMap = new HashMap<>();
    cachedArray = mock(AbstractCachedArray.class);

    setInternalState(cachedArray, "backingMap", cacheMap);
    doCallRealMethod().when(cachedArray).get(any(ArrayList.class));
    doCallRealMethod().when(cachedArray).update(any(ArrayList.class));
  }

  @Test
  public void testGetNewData() 
  {
    Atc atc1 = new Atc();
    atc1.setAtcCode("N01AB");
    atc1.setLevel(4);
    atc1.setTextSv("sv");
    Atc atc2 = new Atc();
    atc2.setAtcCode("N01AC");
    atc2.setLevel(4);
    atc2.setTextSv("sv");

    List<Atc> dataList = new ArrayList<>();
    dataList.add(atc1);
    dataList.add(atc2);

    List<String> keys = ImmutableList.of("N01AB", "N01AC");
    when(cachedArray.load(keys)).thenReturn(dataList);

    List<Atc> data = cachedArray.get(keys);
    assertEquals(data, dataList);
  }
}

Upvotes: 1

Views: 1371

Answers (1)

Michiel Leegwater
Michiel Leegwater

Reputation: 1180

The power of Mockito mocking is that you do not provide the internals of a class. Just the external responses.

That means white box is not needed. But something like

when (mock.doSomehing()).thenReturn (backingMap);

So what I am suggesting is to switch to black box testing. Then you just go with he Mockito flow.

Upvotes: 4

Related Questions