Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
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);

Check warning on line 32 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#L32

You should not modify visibility of constructors, methods or fields using setAccessible()
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);

Check warning on line 31 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#L31

You should not modify visibility of constructors, methods or fields using setAccessible()
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

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/test/java/ua/com/javarush/gnew/m2/cli/commands/EditContactMenuTest.java#L3

Using the '.*' form of import should be avoided - org.mockito.Mockito.*.
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

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/test/java/ua/com/javarush/gnew/m2/cli/commands/EditContactMenuTest.java#L4

Using the '.*' form of import should be avoided - 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 {


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

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider declaring local parameters or even const variables:

private final String ONE = "1";
...
String expectedName = "New Name";

if (userChoice.equals(ONE) && userInput.equals(expectedName)) { assertEquals(expectedName, updatedContact.getFullName()); verify(phoneBookInterface, times(1)).edit(updatedContact); ...

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

}
}

}
Loading
Loading