-
Notifications
You must be signed in to change notification settings - Fork 3
after review_Lesson 125-127 (JUnit/Mockito) #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Binary-Cat-01
wants to merge
16
commits into
KFalcon2022:mockito-for-pr
Choose a base branch
from
Binary-Cat-01:for-pr-after-review
base: mockito-for-pr
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
74f800c
Fixed task description
14ac90a
Make object decomposition
Binary-Cat-01 b607151
Add dependencies: junit
Binary-Cat-01 c4976ff
Fixed Gradle UTF-8 problem
Binary-Cat-01 cc53241
Refactored
Binary-Cat-01 bb7b651
Added unit-test
Binary-Cat-01 b732195
Fixed minor
Binary-Cat-01 f435117
Fixed test
Binary-Cat-01 b8ded7f
build: add mockito-core dependency
Binary-Cat-01 64b9d80
test: refactor fullNameParsingServiceTest with Mockito
Binary-Cat-01 b68f560
build: add mockito-junit dependency
Binary-Cat-01 724fc51
test: refactor fullNameParsingServiceTest with MockitoExtension
Binary-Cat-01 78e5bed
test: add fail-case to fullNameParsingServiceTest
Binary-Cat-01 a8ff859
Merge branch 'working' into mockito-for-pr
Binary-Cat-01 516bc6d
refactor: refactor after pr
Binary-Cat-01 dae78a9
Merge branch 'for-pr' into mockito-for-pr
Binary-Cat-01 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| #Tue Feb 11 23:22:06 MSK 2025 | ||
| distributionBase=GRADLE_USER_HOME | ||
| distributionPath=wrapper/dists | ||
| distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip | ||
| distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip | ||
| zipStoreBase=GRADLE_USER_HOME | ||
| zipStorePath=wrapper/dists |
65 changes: 0 additions & 65 deletions
65
src/main/java/com/walking/lesson125_unit_testing/Main.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,77 +1,12 @@ | ||
| package com.walking.lesson125_unit_testing; | ||
|
|
||
|
|
||
| import com.walking.lesson125_unit_testing.exception.RegexValidationException; | ||
| import com.walking.lesson125_unit_testing.model.FullName; | ||
|
|
||
| /** | ||
| * На базе вашего решения | ||
| * <a href="https://github.com/KFalcon2022/unit-testing-practical-task">задачи из урока 125</a> | ||
| * актуализируйте юнит-тесты с использованием Mockito. Сложность задачи напрямую зависит от качества декомпозиции в | ||
| * вашем предыдущем решении. | ||
| */ | ||
| public class Main { | ||
|
|
||
| public static final String FULL_NAME_REGEX = "^[А-Я][А-Яа-я-]* [А-Я][а-я]* [А-Я][а-я]+$"; | ||
| public static final String DOUBLE_SURNAME_REGEX = "[А-Я][а-я]*-[А-Я][а-я]*"; | ||
| public static final String NAME_REGEX = "[А-Я][а-я]*"; | ||
| public static final String PATRONYMIC_REGEX = "[А-Я][а-я]+"; | ||
|
|
||
| public static void main(String[] args) { | ||
| System.out.println(parseName("Иванов Иван Иванович")); | ||
| System.out.println(parseName("Иванов-Иванов Иван Иванович")); | ||
| System.out.println(parseName("Иванов-Иванов И Иванович")); | ||
| System.out.println(parseName("И-Иванов И Иванович")); | ||
| System.out.println(parseName("Иванов иван Иванович")); | ||
| // Все равно упадет на 30й строке. | ||
| // System.out.println(parseName("И-иванов И Иванович")); | ||
| // System.out.println(parseName("Иванов Иван иванович")); | ||
| // System.out.println(parseName("ИваНов Иван Иванович")); | ||
| // System.out.println(parseName("Ivanov Ivan")); | ||
| } | ||
|
|
||
| private static FullName parseName(String nameString) { | ||
| if (!nameString.matches(FULL_NAME_REGEX)) { | ||
| throw new RegexValidationException(nameString, FULL_NAME_REGEX); | ||
| } | ||
|
|
||
| FullName fullName = new FullName(); | ||
| String[] splitNameString = nameString.split(" "); | ||
|
|
||
| String surname = splitNameString[0]; | ||
| validateSurname(surname); | ||
| fullName.setSurname(surname); | ||
|
|
||
| String name = splitNameString[1]; | ||
| validateName(name); | ||
| fullName.setName(name); | ||
|
|
||
| String patronymic = splitNameString[2]; | ||
| validatePatronymic(patronymic); | ||
| fullName.setPatronymic(patronymic); | ||
|
|
||
| return fullName; | ||
| } | ||
|
|
||
| private static void validateSurname(String surname) { | ||
| if (surname.contains("-")) { | ||
| if (!surname.matches(DOUBLE_SURNAME_REGEX)) { | ||
| throw new RegexValidationException(surname, DOUBLE_SURNAME_REGEX); | ||
| } | ||
| } else { | ||
| validateName(surname); | ||
| } | ||
| } | ||
|
|
||
| private static void validateName(String name) { | ||
| if (!name.matches(NAME_REGEX)) { | ||
| throw new RegexValidationException(name, NAME_REGEX); | ||
| } | ||
| } | ||
|
|
||
| private static void validatePatronymic(String name) { | ||
| if (!name.matches(PATRONYMIC_REGEX)) { | ||
| throw new RegexValidationException(name, PATRONYMIC_REGEX); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/main/java/com/walking/lesson125_unit_testing/service/FullNameParsingService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package com.walking.lesson125_unit_testing.service; | ||
|
|
||
| import com.walking.lesson125_unit_testing.model.FullName; | ||
|
|
||
| public class FullNameParsingService { | ||
| private final FullNameValidationService fullNameValidationService; | ||
|
|
||
| public FullNameParsingService(FullNameValidationService fullNameValidationService) { | ||
| this.fullNameValidationService = fullNameValidationService; | ||
| } | ||
|
|
||
| public FullName parseFullName(String nameString) { | ||
| if (nameString == null) { | ||
| throw new IllegalArgumentException("Unable parse null"); | ||
| } | ||
|
|
||
| fullNameValidationService.validateFullName(nameString); | ||
|
|
||
| String[] splitNameString = nameString.split(" "); | ||
|
|
||
| String surname = splitNameString[0]; | ||
| fullNameValidationService.validateSurname(surname); | ||
|
|
||
| String name = splitNameString[1]; | ||
| fullNameValidationService.validateName(name); | ||
|
|
||
| String patronymic = splitNameString[2]; | ||
| fullNameValidationService.validatePatronymic(patronymic); | ||
|
|
||
| return new FullName(name, surname, patronymic); | ||
| } | ||
| } |
38 changes: 38 additions & 0 deletions
38
src/main/java/com/walking/lesson125_unit_testing/service/FullNameValidationService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package com.walking.lesson125_unit_testing.service; | ||
|
|
||
| import com.walking.lesson125_unit_testing.exception.RegexValidationException; | ||
|
|
||
| public class FullNameValidationService { | ||
| private static final String FULL_NAME_REGEX = "^[А-Я][А-Яа-я-]* [А-Я][а-я]* [А-Я][а-я]+$"; | ||
| private static final String DOUBLE_SURNAME_REGEX = "[А-Я][а-я]*-[А-Я][а-я]*"; | ||
| private static final String NAME_REGEX = "[А-Я][а-я]*"; | ||
| private static final String PATRONYMIC_REGEX = "[А-Я][а-я]+"; | ||
|
|
||
| public void validateFullName(String nameString) { | ||
| if (!nameString.matches(FULL_NAME_REGEX)) { | ||
| throw new RegexValidationException(nameString, FULL_NAME_REGEX); | ||
| } | ||
| } | ||
|
|
||
| public void validateSurname(String surname) { | ||
| if (surname.contains("-")) { | ||
| if (!surname.matches(DOUBLE_SURNAME_REGEX)) { | ||
| throw new RegexValidationException(surname, DOUBLE_SURNAME_REGEX); | ||
| } | ||
| } else { | ||
| validateName(surname); | ||
| } | ||
| } | ||
|
|
||
| public void validateName(String name) { | ||
| if (!name.matches(NAME_REGEX)) { | ||
| throw new RegexValidationException(name, NAME_REGEX); | ||
| } | ||
| } | ||
|
|
||
| public void validatePatronymic(String name) { | ||
| if (!name.matches(PATRONYMIC_REGEX)) { | ||
| throw new RegexValidationException(name, PATRONYMIC_REGEX); | ||
| } | ||
| } | ||
| } |
72 changes: 72 additions & 0 deletions
72
src/test/java/com/walking/lesson125_unit_testing/service/FullNameParsingServiceTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| package com.walking.lesson125_unit_testing.service; | ||
|
|
||
| import com.walking.lesson125_unit_testing.exception.RegexValidationException; | ||
| import com.walking.lesson125_unit_testing.model.FullName; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.junit.jupiter.api.function.Executable; | ||
| import org.mockito.InjectMocks; | ||
| import org.mockito.Mock; | ||
| import org.mockito.junit.jupiter.MockitoExtension; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
| import static org.mockito.Mockito.*; | ||
|
|
||
| @ExtendWith(MockitoExtension.class) | ||
| class FullNameParsingServiceTest { | ||
| @InjectMocks | ||
| private FullNameParsingService fullNameParsingService; | ||
|
|
||
| @Mock | ||
| private FullNameValidationService fullNameValidationService; | ||
|
|
||
| @Test | ||
| void parseFullName_success() { | ||
| // given | ||
| FullName expected = new FullName("Иван", "Иванов", "Иванович"); | ||
|
|
||
| // when | ||
| FullName result = fullNameParsingService.parseFullName("Иванов Иван Иванович"); | ||
|
|
||
| // then | ||
| assertEquals(expected, result); | ||
|
|
||
| verify(fullNameValidationService).validateFullName(anyString()); | ||
| verify(fullNameValidationService).validateSurname(anyString()); | ||
| verify(fullNameValidationService).validateName(anyString()); | ||
| verify(fullNameValidationService).validatePatronymic(anyString()); | ||
| } | ||
|
|
||
| @Test | ||
| void parseFullName_failed_with_invalidFullName() { | ||
| // given | ||
| doThrow(RegexValidationException.class).when(fullNameValidationService) | ||
| .validateFullName(anyString()); | ||
|
|
||
| // when | ||
| Executable actual = () -> fullNameParsingService.parseFullName(anyString()); | ||
|
|
||
| // then | ||
| assertThrows(RegexValidationException.class, actual); | ||
|
|
||
| verify(fullNameValidationService).validateFullName(anyString()); | ||
|
|
||
| verify(fullNameValidationService, never()).validateSurname(anyString()); | ||
| verify(fullNameValidationService, never()).validateName(anyString()); | ||
| verify(fullNameValidationService, never()).validatePatronymic(anyString()); | ||
| } | ||
|
|
||
| @Test | ||
| void parseFullName_failed_with_null() { | ||
| // when | ||
| Executable actual = () -> fullNameParsingService.parseFullName(null); | ||
|
|
||
| // then | ||
| assertThrows(IllegalArgumentException.class, actual); | ||
|
|
||
| verify(fullNameValidationService, never()).validateFullName(anyString()); | ||
| verify(fullNameValidationService, never()).validateSurname(anyString()); | ||
| verify(fullNameValidationService, never()).validateName(anyString()); | ||
| verify(fullNameValidationService, never()).validatePatronymic(anyString()); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
эта штука обычно располагается выше блока с зависимостями. Это не критично, но меньше шанс, что потеряется