Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
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.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;

private AddContact addContact;

@BeforeEach
void setUp() throws Exception {
MockitoAnnotations.openMocks(this);

addContact = new AddContact();

Field phoneBookInterfaceField = AddContact.class.getDeclaredField("phoneBookInterface");
phoneBookInterfaceField.setAccessible(true);

Check warning on line 35 in src/test/java/ua/com/javarush/gnew/m2/cli/commands/AddContactTest.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/test/java/ua/com/javarush/gnew/m2/cli/commands/AddContactTest.java#L35

You should not modify visibility of constructors, methods or fields using setAccessible()
phoneBookInterfaceField.set(addContact, phoneBookInterface);
}

@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);

String[] args = {
"--name", "Иван Иванов",
"--phone", "123456789",
"--email", "ivan@example.com",
"--github", "ivanGitHub"
};
CommandLine.populateCommand(addContact, args);

Integer result = addContact.call();

verify(phoneBookInterface, times(1)).add(any(ContactDto.class));

assertEquals(0, result);
}

@Test
void testCallWithException() throws Exception {

when(phoneBookInterface.add(any(ContactDto.class))).thenThrow(new RuntimeException("Ошибка добавления"));

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));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
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.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;

private DeleteContact deleteContact;

@BeforeEach
void setUp() throws Exception {
MockitoAnnotations.openMocks(this);

deleteContact = new DeleteContact();
var field = DeleteContact.class.getDeclaredField("phoneBookInterface");
field.setAccessible(true);

Check warning on line 34 in src/test/java/ua/com/javarush/gnew/m2/cli/commands/DeleteContactTest.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/test/java/ua/com/javarush/gnew/m2/cli/commands/DeleteContactTest.java#L34

You should not modify visibility of constructors, methods or fields using setAccessible()
field.set(deleteContact, phoneBookInterface);
}

@Test
void testCallSuccessfulDeletion() throws Exception {

doNothing().when(phoneBookInterface).delete(anyLong());
when(phoneBookInterface.list()).thenReturn(List.of());

String[] args = {"123", "456", "789"};
CommandLine.populateCommand(deleteContact, args);

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)).list();

assert result == 0;
}

@Test
void testCallDeletionWithException() throws Exception {

doNothing().when(phoneBookInterface).delete(123L);
doThrow(new IOException("Ошибка удаления")).when(phoneBookInterface).delete(456L);

String[] args = {"123", "456"};
CommandLine.populateCommand(deleteContact, args);

Integer result = deleteContact.call();

verify(phoneBookInterface, times(1)).delete(123L);
verify(phoneBookInterface, times(1)).delete(456L);

verify(phoneBookInterface, times(1)).list();

assert result == 0;
}
}
Loading