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.

8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@
<scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/org.mockito/mockito-junit-jupiter -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>5.14.2</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@
aliases = {"-a", "--add"},
description = "Додає новий контакт до телефонної книги",
mixinStandardHelpOptions = true)

public class AddContact implements CliCommand {


private final PhoneBookInterface phoneBookInterface =
PhoneBookContext.getBean(PhoneBookInterface.class);
PhoneBookContext.getBean(PhoneBookInterface.class);


@Option(
names = {"-n", "--name"},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package ua.com.javarush.gnew.m2.cli.commands;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
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.io.IOException;
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.*;

Check notice on line 18 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#L18

Using the '.*' form of import should be avoided - org.mockito.Mockito.*.

class AddContactTest {
@Mock
private PhoneBookInterface phoneBookInterface;

@InjectMocks
private AddContact addContact;

@BeforeEach
void setUp() throws NoSuchFieldException, IllegalAccessException {

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 testCall_addContactSuccessfully() throws Exception {

Check notice on line 40 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#L40

The JUnit 5 test method name 'testCall_addContactSuccessfully' doesn't match '[a-z][a-zA-Z0-9]*'

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

Check notice on line 59 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#L59

'METHOD_DEF' should be separated from previous line.
void testCall_withException() throws IOException {

Check notice on line 60 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#L60

The JUnit 5 test method name 'testCall_withException' doesn't match '[a-z][a-zA-Z0-9]*'

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

String[] args = {
"--name", "Иван Иванов",
"--phone", "123456789"
};
CommandLine.populateCommand(addContact, args);

RuntimeException thrownException = null;
try {
addContact.call();
} catch (RuntimeException e) {
thrownException = e;
}

assertEquals("java.lang.RuntimeException: Ошибка добавления", thrownException.getMessage());

verify(phoneBookInterface, times(1)).add(any(ContactDto.class));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
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 ua.com.javarush.gnew.m2.utils.Utils;

Check notice on line 10 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#L10

Unused import - ua.com.javarush.gnew.m2.utils.Utils.

import java.io.IOException;
import java.util.List;

import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.*;

Check notice on line 16 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#L16

Using the '.*' form of import should be avoided - org.mockito.Mockito.*.


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 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 testCall_successfulDeletion() throws Exception {

Check notice on line 36 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#L36

The JUnit 5 test method name 'testCall_successfulDeletion' doesn't match '[a-z][a-zA-Z0-9]*'

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 testCall_deletionWithException() throws Exception {

Check notice on line 56 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#L56

The JUnit 5 test method name 'testCall_deletionWithException' doesn't match '[a-z][a-zA-Z0-9]*'

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