-
Notifications
You must be signed in to change notification settings - Fork 8
Mocking lab #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
obna
wants to merge
8
commits into
MillsMCS:mocking-base
Choose a base branch
from
obna:mocking-lab
base: mocking-base
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Mocking lab #12
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
08aefd2
Write tests for getWordOfTheDay()
72d40da
Fix test method name for getWordOfTheDay test
c4e0339
Write test for addWordOfTheDay
d6a11d9
Add javadoc for getFrequencyByYear()
83ca368
Complete missing javadoc comments
d2237f3
Remove doc files
6b78804
Apply code review changes for tests
1be4ae5
Use methods of WordRecord for addWordOfTheDay test
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| .metadata | ||
| doc/ | ||
| bin/ | ||
| tmp/ | ||
| *.tmp | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,38 +5,95 @@ | |
| import static org.mockito.ArgumentMatchers.anyString; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.CsvSource; | ||
| import edu.mills.cs180a.wordnik.client.api.WordApi; | ||
| import edu.mills.cs180a.wordnik.client.api.WordsApi; | ||
| import edu.mills.cs180a.wordnik.client.model.FrequencySummary; | ||
| import edu.mills.cs180a.wordnik.client.model.WordOfTheDay; | ||
| import javafx.collections.FXCollections; | ||
| import javafx.collections.ObservableList; | ||
|
|
||
|
|
||
| class SampleDataTest { | ||
| private final FrequencySummary mockFS = mock(FrequencySummary.class); | ||
| private final WordApi mockWordApi = mock(WordApi.class); | ||
| private final WordsApi mockWordsApi = mock(WordsApi.class); | ||
| private static final WordOfTheDay TODAYS_WORD = mock(WordOfTheDay.class); | ||
| private static final String A_WORD = "jingle"; | ||
| private static final Map<String, FrequencySummary> FREQS_MAP = Map.of( | ||
| "apple", | ||
| makeFreqSummary(List.of( | ||
| makeMap(2000, 339), | ||
| makeMap(2001, 464))), | ||
| "orange", | ||
| makeFreqSummary(List.of( | ||
| makeMap(2000, 774), | ||
| makeMap(2001, 941))), | ||
| A_WORD, | ||
| makeFreqSummary(List.of( | ||
| makeMap(2020, 187)))); | ||
| private static final String TODAYS_DEF = "def for jingle"; | ||
| private static final List<Object> WORD_DEFS = List.of(TODAYS_DEF); | ||
| private static final List<Object> DEF_LIST = List.of(Map.of("text", TODAYS_DEF)); | ||
| private static final List<Object> DEF_LIST_MAP = List.of(Map.of( | ||
| "text", WORD_DEFS.get(0))); | ||
|
|
||
|
|
||
|
|
||
| @BeforeEach | ||
| void setup() { | ||
| List<Object> freqObjects = List.of( | ||
| // frequencies for "apple" | ||
| makeMap(2000, 339), | ||
| makeMap(2001, 464)); | ||
| when(mockFS.getFrequency()) | ||
| .thenReturn(freqObjects); | ||
| when(mockWordApi.getWordFrequency(anyString(), anyString(), anyInt(), anyInt())) | ||
| .thenReturn(mockFS); | ||
| .thenAnswer(invocation -> FREQS_MAP.get(invocation.getArgument(0))); | ||
| when(mockWordsApi.getWordOfTheDay()) | ||
| .thenReturn(TODAYS_WORD); | ||
| when(TODAYS_WORD.getWord()) | ||
| .thenReturn(A_WORD); | ||
| when(TODAYS_WORD.getDefinitions()) | ||
| .thenReturn(DEF_LIST_MAP); | ||
| } | ||
|
|
||
| @Test | ||
| void addWordOfTheDay_EqualsWordRecord_MockWordsObject() { | ||
| List<WordRecord> testList = new ArrayList<>(); | ||
| ObservableList<WordRecord> testListRecord = FXCollections.observableList(testList); | ||
| SampleData.addWordOfTheDay(testListRecord, mockWordsApi, mockWordApi); | ||
|
|
||
| assertEquals("jingle", TODAYS_WORD.getWord()); | ||
| assertEquals(DEF_LIST, TODAYS_WORD.getDefinitions()); | ||
| assertEquals(1, testListRecord.size()); | ||
| assertEquals(187, SampleData.getFrequencyByYear(mockWordApi, TODAYS_WORD.getWord(), 2020)); | ||
| } | ||
|
|
||
| private static FrequencySummary makeFreqSummary(List<Object> freqs) { | ||
| FrequencySummary fs = mock(FrequencySummary.class); | ||
| when(fs.getFrequency()) | ||
| .thenReturn(freqs); | ||
| return fs; | ||
| } | ||
|
|
||
| private static Map<Object, Object> makeMap(int year, int count) { | ||
| return Map.of(SampleData.FREQ_YEAR_KEY, String.valueOf(year), | ||
| SampleData.FREQ_COUNT_KEY, count); | ||
| } | ||
|
|
||
| @Test | ||
| void getWordOfTheDay_EqualsWordRecord_MockWordsObject() { | ||
| assertEquals("jingle", TODAYS_WORD.getWord()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you misunderstood my comment. Your code was right before this commit when it called |
||
| assertEquals(DEF_LIST, TODAYS_WORD.getDefinitions()); | ||
| assertEquals(187, SampleData.getFrequencyByYear(mockWordApi, TODAYS_WORD.getWord(), 2020)); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @CsvSource({"apple,2000,339", "apple,2001,464", "apple,2020,0"}) | ||
| @CsvSource({"apple,2000,339", "apple,2001,464", "apple,2020,0", | ||
| "orange,2000,774", "orange,2001,941", "orange,2050,0"}) | ||
| void testGetFrequencyFromSummary(String word, int year, int count) { | ||
| assertEquals(count, SampleData.getFrequencyByYear(mockWordApi, word, year)); | ||
| } | ||
|
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't include type information (that the list contains word records) since that is shown automatically in generated javadoc as part of the signature.