vendredi 3 juin 2016

Mocking out EHCache with PowerMock

I'm trying to mock out ehcache for testing, somehow I can't mock out the call to Element.getObjectValue(), I get null whatever I do.

Part of code that is being tested:

final Cache paymentFrequencyCache = cm.getCache("paymentFrequencyCache");

Element paymentFrequencyElement = paymentFrequencyCache.get(PAYMENT_FREQUENCY_CACHEKEY);
if (null != paymentFrequencyElement) {
    return (ArrayList<PaymentFrequencyType>)paymentFrequencyElement.getObjectValue();
} else {

Test initialization:

@RunWith(PowerMockRunner.class)
@PrepareForTest({Element.class, Cache.class, CacheManager.class})
@SuppressStaticInitializationFor({"net.sf.ehcache.Element", "net.sf.ehcache.Cache"})
public class ServiceRepPlan201411ImplTest {
    ArrayList<PaymentFrequencyType> paymentFrequencyTypes = new ArrayList<>();

    @Mock
    CacheManager cacheManagerMock;

    @Mock
    Cache paymentFrequencyCacheMock;

    @Mock
    Element paymentFrequencyElementMock;

    @Before
    public void setUp() throws Exception {
        PowerMockito.mockStatic(CacheManager.class);
        PowerMockito.when(CacheManager.newInstance()).thenReturn(cacheManagerMock);

        PowerMockito.when(cacheManagerMock.getCache(Mockito.eq("paymentFrequencyCache"))).thenReturn(paymentFrequencyCacheMock);
        PowerMockito.when(paymentFrequencyCacheMock.get(Mockito.eq("paymentfrequencies"))).thenReturn(paymentFrequencyElementMock);
        PowerMockito.when(paymentFrequencyElementMock.getObjectValue()).thenReturn(paymentFrequencyTypes);

When I get to the paymentFrequencyElement.getObjectValue(); function, it returns null instead of the ArrayList I have set up with

Aucun commentaire:

Enregistrer un commentaire