'How to mock a return from a call to a private method

I have a class which has a method I want to test, but it calls a private method in the same class to get a Map value. I want to mock what the private method returns to the method I want to test.

import java.util.*;

public class ClassOne {

    public List<String> getList(String exampleString) {
        List<String> exampleList = null;
        Map<String, String> exampleMap = getMap();
        String exampleValue = exampleMap.get(exampleString);
        // Does some stuff
        
        exampleList = Arrays.asList(exampleValue.split(","));
        return exampleList;
    }
    
    private Map<String, String> getMap(){
        Map<String, String> exampleMap = new HashMap<>();
        exampleMap.put("pre-defined String", "pre-defined String");
        return exampleMap;
    }

}

From what I can find- it seems like I want to use PowerMock, but I can't seem to figure it out.

A way to mock the return of the private method getMap() when it is called by the method getList.



Solution 1:[1]

You should be able to mock it using powermock

"Mocking of Private Methods Using PowerMock | Baeldung" https://www.baeldung.com/powermock-private-method

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mh377