-
Notifications
You must be signed in to change notification settings - Fork 4
Write-tests-for-the-EditContact-and-EditContactMenu-classes-#108- #112
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
base: main
Are you sure you want to change the base?
Changes from all commits
f1efebd
7826cc8
e913c01
0970e66
3608437
e97cfe6
1894859
348a59d
d588dfb
76912a3
b37654e
2941913
311c1aa
7b253be
be4e4a8
3cd04a8
d045656
b07d5c3
55e2da9
e304691
83e00aa
72e6080
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,76 +1,73 @@ | ||
| package ua.com.javarush.gnew.m2.cli.commands; | ||
|
|
||
| import static org.mockito.ArgumentMatchers.anyLong; | ||
| import static org.mockito.Mockito.doNothing; | ||
| import static org.mockito.Mockito.doThrow; | ||
| import static org.mockito.Mockito.times; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.List; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.mockito.Mock; | ||
| import org.mockito.MockitoAnnotations; | ||
| import picocli.CommandLine; | ||
| import ua.com.javarush.gnew.m2.service.PhoneBookInterface; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.List; | ||
|
|
||
| import static org.mockito.ArgumentMatchers.anyLong; | ||
| import static org.mockito.Mockito.doNothing; | ||
| import static org.mockito.Mockito.doThrow; | ||
| import static org.mockito.Mockito.when; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.times; | ||
|
|
||
|
|
||
| class DeleteContactTest { | ||
|
|
||
| @Mock | ||
| private PhoneBookInterface phoneBookInterface; | ||
| @Mock private PhoneBookInterface phoneBookInterface; | ||
|
|
||
| private DeleteContact deleteContact; | ||
| private DeleteContact deleteContact; | ||
|
|
||
| @BeforeEach | ||
| void setUp() throws Exception { | ||
| MockitoAnnotations.openMocks(this); | ||
| @BeforeEach | ||
| void setUp() throws Exception { | ||
| MockitoAnnotations.openMocks(this); | ||
|
|
||
| deleteContact = new DeleteContact(); | ||
| var field = DeleteContact.class.getDeclaredField("phoneBookInterface"); | ||
| field.setAccessible(true); | ||
| field.set(deleteContact, phoneBookInterface); | ||
| } | ||
| deleteContact = new DeleteContact(); | ||
| var field = DeleteContact.class.getDeclaredField("phoneBookInterface"); | ||
| field.setAccessible(true); | ||
|
Check warning on line 31 in src/test/java/ua/com/javarush/gnew/m2/cli/commands/DeleteContactTest.java
|
||
| field.set(deleteContact, phoneBookInterface); | ||
| } | ||
|
|
||
| @Test | ||
| void testCallSuccessfulDeletion() throws Exception { | ||
| @Test | ||
| void testCallSuccessfulDeletion() throws Exception { | ||
|
|
||
| doNothing().when(phoneBookInterface).delete(anyLong()); | ||
| when(phoneBookInterface.list()).thenReturn(List.of()); | ||
| doNothing().when(phoneBookInterface).delete(anyLong()); | ||
| when(phoneBookInterface.list()).thenReturn(List.of()); | ||
|
|
||
| String[] args = {"123", "456", "789"}; | ||
| CommandLine.populateCommand(deleteContact, args); | ||
| String[] args = {"123", "456", "789"}; | ||
| CommandLine.populateCommand(deleteContact, args); | ||
|
|
||
| Integer result = deleteContact.call(); | ||
| Integer result = deleteContact.call(); | ||
|
|
||
| verify(phoneBookInterface, times(1)).delete(123L); | ||
| verify(phoneBookInterface, times(1)).delete(456L); | ||
| verify(phoneBookInterface, times(1)).delete(789L); | ||
| verify(phoneBookInterface, times(1)).delete(123L); | ||
| verify(phoneBookInterface, times(1)).delete(456L); | ||
| verify(phoneBookInterface, times(1)).delete(789L); | ||
|
|
||
| verify(phoneBookInterface, times(1)).list(); | ||
| verify(phoneBookInterface, times(1)).list(); | ||
|
|
||
| assert result == 0; | ||
| } | ||
| assert result == 0; | ||
| } | ||
|
|
||
| @Test | ||
| void testCallDeletionWithException() throws Exception { | ||
| @Test | ||
| void testCallDeletionWithException() throws Exception { | ||
|
|
||
| doNothing().when(phoneBookInterface).delete(123L); | ||
| doThrow(new IOException("Ошибка удаления")).when(phoneBookInterface).delete(456L); | ||
| doNothing().when(phoneBookInterface).delete(123L); | ||
| doThrow(new IOException("Ошибка удаления")).when(phoneBookInterface).delete(456L); | ||
|
|
||
| String[] args = {"123", "456"}; | ||
| CommandLine.populateCommand(deleteContact, args); | ||
| String[] args = {"123", "456"}; | ||
| CommandLine.populateCommand(deleteContact, args); | ||
|
|
||
| Integer result = deleteContact.call(); | ||
| Integer result = deleteContact.call(); | ||
|
|
||
| verify(phoneBookInterface, times(1)).delete(123L); | ||
| verify(phoneBookInterface, times(1)).delete(456L); | ||
| verify(phoneBookInterface, times(1)).delete(123L); | ||
| verify(phoneBookInterface, times(1)).delete(456L); | ||
|
|
||
| verify(phoneBookInterface, times(1)).list(); | ||
| verify(phoneBookInterface, times(1)).list(); | ||
|
|
||
| assert result == 0; | ||
| } | ||
| } | ||
| assert result == 0; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| package ua.com.javarush.gnew.m2.cli.commands; | ||
|
|
||
| import static org.mockito.Mockito.*; | ||
|
Check notice on line 3 in src/test/java/ua/com/javarush/gnew/m2/cli/commands/EditContactMenuTest.java
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
Check notice on line 4 in src/test/java/ua/com/javarush/gnew/m2/cli/commands/EditContactMenuTest.java
|
||
| import java.io.IOException; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.Scanner; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.CsvSource; | ||
| import org.mockito.MockedStatic; | ||
| import ua.com.javarush.gnew.m2.configuration.PhoneBookContext; | ||
| import ua.com.javarush.gnew.m2.dto.ContactDto; | ||
| import ua.com.javarush.gnew.m2.service.PhoneBookInterface; | ||
|
|
||
| class EditContactMenuTest { | ||
|
|
||
|
|
||
| @ParameterizedTest | ||
| @CsvSource({"1, New Name", "2, 987654321", "3, new.email@example.com", "4, newGithubID", "5, invalid"}) | ||
| void testEditContactMenuValidAndInvalidInput(String userChoice, String userInput) throws IOException { | ||
|
|
||
|
|
||
| PhoneBookInterface phoneBookInterface = mock(PhoneBookInterface.class); | ||
|
|
||
|
|
||
|
|
||
| try (MockedStatic<PhoneBookContext> mockedStatic = mockStatic(PhoneBookContext.class)) { | ||
| mockedStatic.when(() -> PhoneBookContext.getBean(PhoneBookInterface.class)) | ||
| .thenReturn(phoneBookInterface); | ||
|
|
||
| ContactDto contact = new ContactDto(); | ||
| contact.setId(1L); | ||
| contact.setFullName("John Doe"); | ||
| contact.setPhones(List.of("123456789")); | ||
| contact.setEmails(List.of("john.doe@example.com")); | ||
| contact.setGithubId("johndoe123"); | ||
|
|
||
| EditContactMenu editContactMenu = new EditContactMenu(); | ||
|
|
||
| editContactMenu.setContact(contact); | ||
|
|
||
| when(phoneBookInterface.getById(1L)).thenReturn(Optional.of(contact)); | ||
|
|
||
| editContactMenu.setChoice(userChoice); | ||
|
|
||
| editContactMenu.setScanner(new Scanner(userInput)); | ||
|
|
||
| Integer result = editContactMenu.call(); | ||
|
|
||
| assertEquals(0, result); | ||
|
|
||
| ContactDto updatedContact = editContactMenu.getContact(); | ||
|
|
||
|
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. Consider declaring local parameters or even const variables:
|
||
| String ONE = "1"; | ||
| String TWO = "2"; | ||
| String THREE = "3"; | ||
| String FOUR = "4"; | ||
| String FIVE = "5"; | ||
|
|
||
| String expectedName = "New Name"; | ||
| String expectedPhone = "987654321"; | ||
| String expectedEmail = "new.email@example.com"; | ||
| String expectedGitHubID = "newGitHubID"; | ||
| String expectedInput = "invalid"; | ||
|
|
||
|
|
||
| if (userChoice.equals(ONE) && userInput.equals(expectedName)) { | ||
| assertEquals(expectedName, updatedContact.getFullName()); | ||
| verify(phoneBookInterface, times(1)).edit(updatedContact); | ||
|
|
||
| } else if (userChoice.equals(TWO) && userInput.equals(expectedPhone)) { | ||
| assertEquals(List.of(expectedPhone), updatedContact.getPhones()); | ||
| verify(phoneBookInterface, times(1)).edit(updatedContact); | ||
|
|
||
| } else if (userChoice.equals(THREE) && userInput.equals(expectedEmail)) { | ||
| assertEquals(List.of(expectedEmail), updatedContact.getEmails()); | ||
| verify(phoneBookInterface, times(1)).edit(updatedContact); | ||
|
|
||
| } else if (userChoice.equals(FOUR) && userInput.equals(expectedGitHubID)) { | ||
| assertEquals(expectedGitHubID, updatedContact.getGithubId()); | ||
| verify(phoneBookInterface, times(1)).edit(updatedContact); | ||
|
|
||
| } else if (userChoice.equals(FIVE) && userInput.equals(expectedInput)) { | ||
| verifyNoInteractions(phoneBookInterface); | ||
| } | ||
|
|
||
| } | ||
| } | ||
|
|
||
| } | ||
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.
Seems like test logic clashes the Triple AAA pattern