Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/google-java-format.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
<scope>test</scope>
</dependency>


<!-- https://mvnrepository.com/artifact/org.mockito/mockito-junit-jupiter -->
<dependency>
<groupId>org.mockito</groupId>
Expand Down
3 changes: 3 additions & 0 deletions settings.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"user" : "tester"
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
104 changes: 52 additions & 52 deletions src/test/java/ua/com/javarush/gnew/m2/cli/commands/AddContactTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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());
}
}

verify(phoneBookInterface, times(1)).add(any(ContactDto.class));
}
}
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);
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;
}
}
Loading
Loading