So i have this class which count unique symbols .This method contain cache too.I need to test exactly cahce . I know for this target can use Mockito, however i have some problems with it.
public class UniqueCharacterCounterService {
private Map<String, Map<Character, Integer>> cache = new HashMap<>();
public Map<Character,Integer> countCharacters(final String inputData) throws IncorrectInputDataException {
if (inputData == null) {
throw new IncorrectInputDataException("The method parameter can't be null");
}
Map<Character, Integer> value = cache.get(inputData);
Map<Character, Integer> result = new LinkedHashMap<>();
if (value != null) {
return value;
}
char symbol;
Integer counter;
char inputAsCharArray[] = inputData.toCharArray();
for (int j = 0; j < inputAsCharArray.length; j++) {
symbol = inputAsCharArray[j];
counter = result.get(symbol);
if (counter != null) {
result.put(symbol, counter + 1);
} else {
result.put(symbol, 1);
}
}
cache.put(inputData, result);
return result;
}
public void printResult(Map<Character, Integer> input) {
input.forEach((key, value) -> System.out.println("\"" + key + "\" " + "- " + value));
}
}
Main
> public class Main {
final static Logger logger = Logger.getLogger(Main.class);
public static void main(String[] args) throws IncorrectInputDataException {
try {
UniqueCharacterCounterService characterCounter = new UniqueCharacterCounterService();
Map<Character, Integer> result = characterCounter.countCharacters("Hello");
characterCounter.printResult(result);
result = characterCounter.countCharacters("world");
characterCounter.printResult(result);
result = characterCounter.countCharacters("Hello");
characterCounter.printResult(result);
} catch (IncorrectInputDataException e) {
logger.error(e.getMessage());
}
}
}`
Aucun commentaire:
Enregistrer un commentaire