diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/google-java-format.xml b/.idea/google-java-format.xml new file mode 100644 index 0000000..2aa056d --- /dev/null +++ b/.idea/google-java-format.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..9e0563e --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,12 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index cfd9e69..dac34ce 100644 --- a/pom.xml +++ b/pom.xml @@ -66,6 +66,7 @@ test + org.mockito diff --git a/settings.st b/settings.st new file mode 100644 index 0000000..2917e40 --- /dev/null +++ b/settings.st @@ -0,0 +1,3 @@ +{ + "user" : "tester" +} \ No newline at end of file diff --git a/src/main/java/ua/com/javarush/gnew/m2/cli/commands/GroupContact.java b/src/main/java/ua/com/javarush/gnew/m2/cli/commands/GroupContact.java index 09be4b4..d87c32f 100644 --- a/src/main/java/ua/com/javarush/gnew/m2/cli/commands/GroupContact.java +++ b/src/main/java/ua/com/javarush/gnew/m2/cli/commands/GroupContact.java @@ -8,6 +8,8 @@ import java.util.List; import java.util.Objects; import java.util.stream.Collectors; + + import ua.com.javarush.gnew.m2.cli.CliCommand; import ua.com.javarush.gnew.m2.configuration.PhoneBookContext; import ua.com.javarush.gnew.m2.repository.GroupContactsRepository; diff --git a/src/test/java/ua/com/javarush/gnew/m2/cli/commands/AddContactTest.java b/src/test/java/ua/com/javarush/gnew/m2/cli/commands/AddContactTest.java index f10c26a..6e4cf36 100644 --- a/src/test/java/ua/com/javarush/gnew/m2/cli/commands/AddContactTest.java +++ b/src/test/java/ua/com/javarush/gnew/m2/cli/commands/AddContactTest.java @@ -1,5 +1,13 @@ package ua.com.javarush.gnew.m2.cli.commands; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.lang.reflect.Field; +import java.util.List; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.Mock; @@ -8,72 +16,64 @@ import ua.com.javarush.gnew.m2.dto.ContactDto; import ua.com.javarush.gnew.m2.service.PhoneBookInterface; -import java.lang.reflect.Field; -import java.util.List; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.when; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.times; - - class AddContactTest { - @Mock - private PhoneBookInterface phoneBookInterface; + @Mock private PhoneBookInterface phoneBookInterface; - private AddContact addContact; + private AddContact addContact; - @BeforeEach - void setUp() throws Exception { - MockitoAnnotations.openMocks(this); + @BeforeEach + void setUp() throws Exception { + MockitoAnnotations.openMocks(this); - addContact = new AddContact(); + addContact = new AddContact(); - Field phoneBookInterfaceField = AddContact.class.getDeclaredField("phoneBookInterface"); - phoneBookInterfaceField.setAccessible(true); - phoneBookInterfaceField.set(addContact, phoneBookInterface); - } + Field phoneBookInterfaceField = AddContact.class.getDeclaredField("phoneBookInterface"); + phoneBookInterfaceField.setAccessible(true); + phoneBookInterfaceField.set(addContact, phoneBookInterface); + } - @Test - void testCallAddContactSuccessfully() throws Exception { + @Test + void testCallAddContactSuccessfully() throws Exception { - ContactDto savedContact = new ContactDto("Иван Иванов", List.of("123456789"), List.of("ivan@example.com"), "ivanGitHub"); - when(phoneBookInterface.add(any(ContactDto.class))).thenReturn(savedContact); + ContactDto savedContact = + new ContactDto( + "Иван Иванов", List.of("123456789"), List.of("ivan@example.com"), "ivanGitHub"); + when(phoneBookInterface.add(any(ContactDto.class))).thenReturn(savedContact); - String[] args = { - "--name", "Иван Иванов", - "--phone", "123456789", - "--email", "ivan@example.com", - "--github", "ivanGitHub" - }; - CommandLine.populateCommand(addContact, args); + String[] args = { + "--name", "Иван Иванов", + "--phone", "123456789", + "--email", "ivan@example.com", + "--github", "ivanGitHub" + }; + CommandLine.populateCommand(addContact, args); - Integer result = addContact.call(); + Integer result = addContact.call(); - verify(phoneBookInterface, times(1)).add(any(ContactDto.class)); + verify(phoneBookInterface, times(1)).add(any(ContactDto.class)); - assertEquals(0, result); - } + assertEquals(0, result); + } - @Test - void testCallWithException() throws Exception { + @Test + void testCallWithException() throws Exception { - when(phoneBookInterface.add(any(ContactDto.class))).thenThrow(new RuntimeException("Ошибка добавления")); + when(phoneBookInterface.add(any(ContactDto.class))) + .thenThrow(new RuntimeException("Ошибка добавления")); - String[] args = { - "--name", "Иван Иванов", - "--phone", "123456789" - }; - CommandLine.populateCommand(addContact, args); + String[] args = { + "--name", "Иван Иванов", + "--phone", "123456789" + }; + CommandLine.populateCommand(addContact, args); - try { - addContact.call(); - } catch (RuntimeException e) { - assertEquals("java.lang.RuntimeException: Ошибка добавления", e.getMessage()); - } - - verify(phoneBookInterface, times(1)).add(any(ContactDto.class)); + try { + addContact.call(); + } catch (RuntimeException e) { + assertEquals("java.lang.RuntimeException: Ошибка добавления", e.getMessage()); } -} \ No newline at end of file + + verify(phoneBookInterface, times(1)).add(any(ContactDto.class)); + } +} diff --git a/src/test/java/ua/com/javarush/gnew/m2/cli/commands/DeleteContactTest.java b/src/test/java/ua/com/javarush/gnew/m2/cli/commands/DeleteContactTest.java index 94e7e2e..182fdfc 100644 --- a/src/test/java/ua/com/javarush/gnew/m2/cli/commands/DeleteContactTest.java +++ b/src/test/java/ua/com/javarush/gnew/m2/cli/commands/DeleteContactTest.java @@ -1,5 +1,14 @@ 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; @@ -7,70 +16,58 @@ 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); + 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; - } -} \ No newline at end of file + assert result == 0; + } +} diff --git a/src/test/java/ua/com/javarush/gnew/m2/cli/commands/EditContactMenuTest.java b/src/test/java/ua/com/javarush/gnew/m2/cli/commands/EditContactMenuTest.java new file mode 100644 index 0000000..b74d7c6 --- /dev/null +++ b/src/test/java/ua/com/javarush/gnew/m2/cli/commands/EditContactMenuTest.java @@ -0,0 +1,88 @@ +package ua.com.javarush.gnew.m2.cli.commands; + +import static org.mockito.Mockito.*; +import static org.junit.jupiter.api.Assertions.*; +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 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(); + + 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); + } + + } + } + +} \ No newline at end of file diff --git a/src/test/java/ua/com/javarush/gnew/m2/cli/commands/EditContactTest.java b/src/test/java/ua/com/javarush/gnew/m2/cli/commands/EditContactTest.java new file mode 100644 index 0000000..db411c7 --- /dev/null +++ b/src/test/java/ua/com/javarush/gnew/m2/cli/commands/EditContactTest.java @@ -0,0 +1,109 @@ +package ua.com.javarush.gnew.m2.cli.commands; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import org.mockito.MockedStatic; +import picocli.CommandLine; +import ua.com.javarush.gnew.m2.configuration.PhoneBookContext; +import ua.com.javarush.gnew.m2.dto.ContactDto; +import ua.com.javarush.gnew.m2.service.PhoneBookInterface; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verifyNoInteractions; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.ArgumentMatchers.any; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.util.Optional; +import java.util.List; +import static org.junit.jupiter.api.Assertions.assertEquals; + + +class EditContactTest { + + private PhoneBookInterface phoneBookInterface; + + private EditContactMenu editContactMenu; + + + @BeforeEach + void setUp() { + + phoneBookInterface = mock(PhoneBookInterface.class); + editContactMenu = mock(EditContactMenu.class); + } + + @ParameterizedTest + @ValueSource(strings = {"1\n5\n", "5\n"}) + void testEditContact(String userInput) throws IOException { + + try (MockedStatic mockedStatic = mockStatic(PhoneBookContext.class)) { + mockedStatic.when(() -> PhoneBookContext.getBean(PhoneBookInterface.class)) + .thenReturn(phoneBookInterface); + mockedStatic.when(() -> PhoneBookContext.getBean(EditContactMenu.class)) + .thenReturn(editContactMenu); + + ContactDto mockContact = new ContactDto(); + mockContact.setId(1L); + mockContact.setFullName("John Doe"); + mockContact.setPhones(List.of("123456789")); + mockContact.setEmails(List.of("john.doe@example.com")); + mockContact.setGithubId("johndoe123"); + + when(phoneBookInterface.getById(1L)).thenReturn(Optional.of(mockContact)); + + System.setIn(new ByteArrayInputStream(userInput.getBytes())); + + EditContact editContact = new EditContact(); + + new CommandLine(editContact).parseArgs("1"); + + Integer result = editContact.call(); + + assertEquals(0, result); + + verify(phoneBookInterface, times(1)).getById(1L); + + if (userInput.equals("1\n5\n")) { + verify(editContactMenu, times(1)).setContact(mockContact); + } else { + verify(editContactMenu, never()).setContact(any(ContactDto.class)); + } + } + } + + @Test + void testEditContactNotFound() throws IOException { + + try (MockedStatic mockedStatic = mockStatic(PhoneBookContext.class)) { + mockedStatic.when(() -> PhoneBookContext.getBean(PhoneBookInterface.class)) + .thenReturn(phoneBookInterface); + + when(phoneBookInterface.getById(1L)).thenReturn(Optional.empty()); + + String userInput = "5\n"; + System.setIn(new ByteArrayInputStream(userInput.getBytes())); + + EditContact editContact = new EditContact(); + + new CommandLine(editContact).parseArgs("1"); + + Integer result = editContact.call(); + + assertEquals(0, result); + + verify(phoneBookInterface, times(1)).getById(1L); + + verifyNoInteractions(editContactMenu); + + } + } + + +} \ No newline at end of file diff --git a/src/test/java/ua/com/javarush/gnew/m2/cli/commands/GroupContactTest.java b/src/test/java/ua/com/javarush/gnew/m2/cli/commands/GroupContactTest.java new file mode 100644 index 0000000..6a8d38a --- /dev/null +++ b/src/test/java/ua/com/javarush/gnew/m2/cli/commands/GroupContactTest.java @@ -0,0 +1,130 @@ +package ua.com.javarush.gnew.m2.cli.commands; + +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.dto.ContactDto; +import ua.com.javarush.gnew.m2.repository.GroupContactsRepository; +import ua.com.javarush.gnew.m2.service.PhoneBookInterface; + +import java.lang.reflect.Field; +import java.util.List; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.*; + +class GroupContactTest { + + @Mock + private GroupContactsRepository groupContactsRepository; + + @Mock + private PhoneBookInterface phoneBookInterface; + + private GroupContact groupContact; + + @BeforeEach + void setUp() throws Exception { + MockitoAnnotations.openMocks(this); + + groupContact = new GroupContact(); + + Field groupRepoField = GroupContact.class.getDeclaredField("groupContactsRepository"); + groupRepoField.setAccessible(true); + groupRepoField.set(groupContact, groupContactsRepository); + + Field phoneBookField = GroupContact.class.getDeclaredField("phoneBookInterface"); + phoneBookField.setAccessible(true); + phoneBookField.set(groupContact, phoneBookInterface); + } + + @Test + void testCreateGroup() throws Exception { + String[] args = {"--create", "Friends"}; + CommandLine.populateCommand(groupContact, args); + + Integer result = groupContact.call(); + + verify(groupContactsRepository, times(1)).create("Friends"); + verifyNoMoreInteractions(groupContactsRepository); + assertEquals(0, result); + } + + @Test + void testDeleteGroup() throws Exception { + String[] args = {"--delete", "OldFriends"}; + CommandLine.populateCommand(groupContact, args); + + Integer result = groupContact.call(); + + verify(groupContactsRepository, times(1)).delete("OldFriends"); + verifyNoMoreInteractions(groupContactsRepository); + assertEquals(0, result); + } + + @Test + void testAddContactsToGroup() throws Exception { + String[] args = {"--add", "Family", "1", "2", "3"}; + CommandLine.populateCommand(groupContact, args); + + Integer result = groupContact.call(); + + verify(groupContactsRepository, times(1)).addContact("Family", 1); + verify(groupContactsRepository, times(1)).addContact("Family", 2); + verify(groupContactsRepository, times(1)).addContact("Family", 3); + verifyNoMoreInteractions(groupContactsRepository); + assertEquals(0, result); + } + + @Test + void testRemoveContactsFromGroup() throws Exception { + String[] args = {"--remove", "Colleagues", "5", "6"}; + CommandLine.populateCommand(groupContact, args); + + Integer result = groupContact.call(); + + verify(groupContactsRepository, times(1)).deleteContact("Colleagues", 5); + verify(groupContactsRepository, times(1)).deleteContact("Colleagues", 6); + verifyNoMoreInteractions(groupContactsRepository); + assertEquals(0, result); + } + + @Test + void testListGroups() throws Exception { + + when(groupContactsRepository.getGroups()).thenReturn(List.of("Family", "Friends")); + when(groupContactsRepository.getContactsId("Family")).thenReturn(List.of(1L, 2L)); + when(groupContactsRepository.getContactsId("Friends")).thenReturn(List.of(3L, 4L)); + + when(phoneBookInterface.getById(1L)).thenReturn(Optional.of( + new ContactDto(1L, "John Doe", List.of("123456789"), List.of("john.doe@example.com"), null) + )); + when(phoneBookInterface.getById(2L)).thenReturn(Optional.of( + new ContactDto(2L, "Jane Doe", List.of("987654321"), List.of("jane.doe@example.com"), null) + )); + when(phoneBookInterface.getById(3L)).thenReturn(Optional.of( + new ContactDto(3L, "Alice Smith", List.of("111222333"), List.of("alice@example.com"), null) + )); + when(phoneBookInterface.getById(4L)).thenReturn(Optional.of( + new ContactDto(4L, "Bob Johnson", List.of("444555666"), List.of("bob@example.com"), null) + )); + + String[] args = {}; + CommandLine.populateCommand(groupContact, args); + + Integer result = groupContact.call(); + + verify(groupContactsRepository, times(1)).getGroups(); + verify(groupContactsRepository, times(1)).getContactsId("Family"); + verify(groupContactsRepository, times(1)).getContactsId("Friends"); + verify(phoneBookInterface, times(1)).getById(1L); + verify(phoneBookInterface, times(1)).getById(2L); + verify(phoneBookInterface, times(1)).getById(3L); + verify(phoneBookInterface, times(1)).getById(4L); + + assertEquals(0, result); + } +} \ No newline at end of file diff --git a/src/test/java/ua/com/javarush/gnew/m2/cli/commands/ListContactsTest.java b/src/test/java/ua/com/javarush/gnew/m2/cli/commands/ListContactsTest.java new file mode 100644 index 0000000..964615a --- /dev/null +++ b/src/test/java/ua/com/javarush/gnew/m2/cli/commands/ListContactsTest.java @@ -0,0 +1,57 @@ +package ua.com.javarush.gnew.m2.cli.commands; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import ua.com.javarush.gnew.m2.dto.ContactDto; +import ua.com.javarush.gnew.m2.service.PhoneBookInterface; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.*; + +class ListContactsTest { + @Mock + private PhoneBookInterface phoneBookInterface; + + private ListContacts listContacts; + private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream(); + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + + listContacts = new ListContacts() { + @Override + public Integer call() throws IOException { + + phoneBookInterface.list().forEach(contact -> System.out.println(contact.getFullName())); + return 0; + } + }; + + System.setOut(new PrintStream(outputStreamCaptor)); + } + + @Test + void testListContacts() throws IOException { + + ContactDto contact = new ContactDto("John Doe", List.of("123-456-7890"), List.of(), ""); + List contacts = List.of(contact); + when(phoneBookInterface.list()).thenReturn(contacts); + + int result = listContacts.call(); + + verify(phoneBookInterface, times(1)).list(); + + String expectedOutput = "John Doe"; + assertEquals(expectedOutput.trim(), outputStreamCaptor.toString().trim()); + + assertEquals(0, result); + } +} \ No newline at end of file diff --git a/src/test/java/ua/com/javarush/gnew/m2/service/SimplePhoneBookTest.java b/src/test/java/ua/com/javarush/gnew/m2/service/SimplePhoneBookTest.java index bff2039..2861bf9 100644 --- a/src/test/java/ua/com/javarush/gnew/m2/service/SimplePhoneBookTest.java +++ b/src/test/java/ua/com/javarush/gnew/m2/service/SimplePhoneBookTest.java @@ -32,8 +32,7 @@ class SimplePhoneBookTest { void beforeEach() { try (MockedStatic phone = Mockito.mockStatic(PhoneBookContext.class)) { phone - .when( - () -> PhoneBookContext.getBean(ContactDtoRepository.class)) + .when(() -> PhoneBookContext.getBean(ContactDtoRepository.class)) .thenReturn(contactDtoRepository); phoneBook = new SimplePhoneBook(); } diff --git a/testerbook.st b/testerbook.st new file mode 100644 index 0000000..34c6175 --- /dev/null +++ b/testerbook.st @@ -0,0 +1,25 @@ +[ { + "id" : 74, + "fullName" : "Chris Hemsworth", + "phones" : [ "+380671111111", "+380672222222" ], + "emails" : [ "chris.h@m.ua", "chris.h@gmail.com" ], + "githubId" : "ChrisHemGit" +}, { + "id" : 5736, + "fullName" : "Chris Pratt", + "phones" : [ "+380673333333", "+380674444444" ], + "emails" : [ "chris.p@m.ua", "chris.p@gmail.com" ], + "githubId" : "ChrisPraGit" +}, { + "id" : 7070, + "fullName" : "Scarlett Johansson", + "phones" : [ "+380675555555", "+380676666666" ], + "emails" : [ "Scarlett.j@m.ua", "Scarlett.j@gmail.com" ], + "githubId" : "ScarlettJohGit" +}, { + "id" : 7160, + "fullName" : "Jeremy Renner", + "phones" : [ "+380677777777", "+380678888888" ], + "emails" : [ "Jeremy.r@m.ua", "Jeremy.r@gmail.com" ], + "githubId" : "JeremyRenGit" +} ] \ No newline at end of file