RecentEmojiManager implements RecentEmoji { @Override public void persist() { if (emojiList.size() > 0) { final StringBuilder stringBuilder = new StringBuilder(emojiList.size() * EMOJI_GUESS_SIZE); for (int i = 0; i < emojiList.size(); i++) { final Data data = emojiList.get(i); stringBuilder.append(data.emoji.getUnicode()) .append(TIME_DELIMITER) .append(data.timestamp) .append(EMOJI_DELIMITER); } stringBuilder.setLength(stringBuilder.length() - EMOJI_DELIMITER.length()); sharedPreferences.edit().putString(RECENT_EMOJIS, stringBuilder.toString()).apply(); } } RecentEmojiManager(@NonNull final Context context); @Override @SuppressWarnings({ "PMD.AvoidDeeplyNestedIfStmts", "checkstyle:nestedifdepth" }) @NonNull Collection<Emoji> getRecentEmojis(); @Override void addEmoji(@NonNull final Emoji emoji); @Override void persist();  }
@Test public void persist() { final Emoji firstEmoji = new Emoji(0x1f437, new String[]{"test"}, R.drawable.emoji_recent, false); recentEmojiManager.addEmoji(firstEmoji); final Emoji secondEmoji = new Emoji(0x1f43d, new String[]{"test"}, R.drawable.emoji_recent, false); recentEmojiManager.addEmoji(secondEmoji); recentEmojiManager.persist(); final Collection<Emoji> recentEmojis = recentEmojiManager.getRecentEmojis(); assertThat(recentEmojis).hasSize(2).containsExactly(secondEmoji, firstEmoji); }