samedi 16 décembre 2017

How to reset() Class?> references in PowerMockito

class UniqueCharacterCounterService {

    public Map<Character, Integer> countCharacters(final String inputData) {
         *some logic....*

         count(inputData);
    }

    private Map<Character, Integer> count(final String inputData) {
        *some logic...*

         return Map
    }

}

And have tests. When the second method was public it`s work.

     @Test
    public void testCache() throws Exception {
     UniqueCharacterCounterService characterCounter = spy(new 
        UniqueCharacterCounterService());

        characterCounter.countCharacters("Hello");
        verify(characterCounter).count("Hello");
        characterCounter.countCharacters("World");
        verify(characterCounter).count("World");
        reset(characterCounter);
        characterCounter.countCharacters("Hello");
        verify(characterCounter, never()).count("Hello");

    }

Actually i tasted cache . Then i made a method - private and selected PowerMockito to do it. However, i cant do it.

    characterCounter.countCharacters("Hello");
    verifyPrivate(characterCounter).invoke("count","Hello");
    reset(characterCounter);
    characterCounter.countCharacters("Hello");
    verifyPrivate(characterCounter, times(0)).invoke("count","Hello");

How can i reset it? Or ways to implementation .

Aucun commentaire:

Enregistrer un commentaire