dimanche 29 octobre 2017

Test with Mockito

I have two classes : Service and Main. And i am trying to test it with mockito however was faced with a incorrect construct(i have read about must have interfaces) my programm or smth else. Can somebody help me with my beginning?

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));
}

}

And 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