-
Notifications
You must be signed in to change notification settings - Fork 0
2/12 Тесты #1
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
iaroman
wants to merge
1
commit into
master
Choose a base branch
from
branch_for_check
base: master
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
2/12 Тесты #1
Changes from all commits
Commits
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
49 changes: 49 additions & 0 deletions
49
src/main/java/pro/sky/Calculator/CalculatorController.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,49 @@ | ||
| package pro.sky.Calculator; | ||
|
|
||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RequestParam; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/calculator") | ||
| public class CalculatorController { | ||
| private final CalculatorService calculatorService; | ||
|
|
||
| public CalculatorController(CalculatorService calculatorService) { | ||
| this.calculatorService = calculatorService; | ||
| } | ||
| @GetMapping | ||
| public String welcome() { | ||
| return "Добро пожаловать в калькулятор"; | ||
| } | ||
| @GetMapping("/plus") | ||
| public String plus(@RequestParam(required = false) Integer num1, @RequestParam(required = false) Integer num2){ | ||
| isNull(num1); | ||
| isNull(num2); | ||
| return calculatorService.result(num1, num2, "+", calculatorService.plus(num1,num2)); | ||
| } | ||
| @GetMapping("/minus") | ||
| public String minus(@RequestParam Integer num1, @RequestParam Integer num2){ | ||
| isNull(num1); | ||
| isNull(num2); | ||
| return calculatorService.result(num1, num2, "-", calculatorService.minus(num1,num2)); | ||
| } | ||
| @GetMapping("/multiply") | ||
| public String multiply(@RequestParam Integer num1, @RequestParam Integer num2){ | ||
| isNull(num1); | ||
| isNull(num2); | ||
| return calculatorService.result(num1, num2, "*", calculatorService.multiply(num1,num2)); | ||
| } | ||
| @GetMapping("/divide") | ||
| public String divide(@RequestParam Integer num1, @RequestParam Integer num2){ | ||
| isNull(num1); | ||
| isNull(num2); | ||
| return calculatorService.result(num1, num2, "/", calculatorService.divide(num1,num2)); | ||
| } | ||
| private void isNull(Integer num) { | ||
| if (num==null) | ||
| throw new NumIsNullException("Переменная не задана"); | ||
|
|
||
| } | ||
| } |
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
7 changes: 7 additions & 0 deletions
7
src/main/java/pro/sky/Calculator/IllegalArgumentException.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,7 @@ | ||
| package pro.sky.Calculator; | ||
|
|
||
| public class IllegalArgumentException extends RuntimeException{ | ||
| public IllegalArgumentException(String s) { | ||
| super(s); | ||
| } | ||
| } |
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,7 @@ | ||
| package pro.sky.Calculator; | ||
|
|
||
| public class NumIsNullException extends RuntimeException{ | ||
| public NumIsNullException (String s) { | ||
| super(s); | ||
| } | ||
| } |
76 changes: 76 additions & 0 deletions
76
src/test/java/pro/sky/Calculator/CalculatorServiceParameterizedTest.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,76 @@ | ||
| package pro.sky.Calculator; | ||
|
|
||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.Arguments; | ||
| import org.junit.jupiter.params.provider.CsvSource; | ||
|
|
||
| import java.util.stream.Stream; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| class CalculatorServiceParameterizedTest { | ||
| private CalculatorService calculatorService = new CalculatorService(); | ||
|
|
||
| @ParameterizedTest | ||
| @CsvSource({ | ||
| "1, 2", | ||
| "20, 10", | ||
| "-10, 10" | ||
| }) | ||
| void plus(Integer num1, Integer num2) { | ||
| Integer expected = calculatorService.plus(num1, num2); | ||
| Integer actual = num1 + num2; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @CsvSource({ | ||
| "1, 2", | ||
| "20, 10", | ||
| "-10, 10" | ||
| }) | ||
| void minus(Integer num1, Integer num2) { | ||
| Integer expected = calculatorService.minus(num1, num2); | ||
| Integer actual = num1 - num2; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @CsvSource({ | ||
| "1, 2", | ||
| "20, 10", | ||
| "-10, 10" | ||
| }) | ||
| void multiply(Integer num1, Integer num2) { | ||
| Integer expected = calculatorService.multiply(num1, num2); | ||
| Integer actual = num1 * num2; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @CsvSource({ | ||
| "1, 2", | ||
| "20, 10", | ||
| "-10, 10" | ||
| }) | ||
| void divide(Integer num1, Integer num2) { | ||
| Integer expected = calculatorService.divide(num1, num2); | ||
| Integer actual = num1 / num2; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @CsvSource({ | ||
| "1, 2", | ||
| "20, 10", | ||
| "-10, 10" | ||
| }) | ||
| void result(Integer num1, Integer num2) { | ||
| String operation = "+"; | ||
| String expected = calculatorService.result(num1, num2, operation, calculatorService.plus(num1, num2)); | ||
| String actual = num1 + " " + operation + " " + num2 + " = " + calculatorService.plus(num1,num2); | ||
| assertEquals(expected, actual); | ||
| } | ||
| } | ||
61 changes: 61 additions & 0 deletions
61
src/test/java/pro/sky/Calculator/CalculatorServiceTest.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,61 @@ | ||
| package pro.sky.Calculator; | ||
|
|
||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| class CalculatorServiceTest { | ||
| private Integer num1; | ||
| private Integer num2; | ||
| private CalculatorService calculatorService; | ||
| @BeforeEach | ||
| void setUp() { | ||
| num1 = 4; | ||
| num2 = 2; | ||
| calculatorService = new CalculatorService(); | ||
| } | ||
| @Test | ||
| void plus() { | ||
| Integer expected = calculatorService.plus(num1, num2); | ||
| Integer actual = num1 + num2; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void minus() { | ||
| Integer expected = calculatorService.minus(num1, num2); | ||
| Integer actual = num1 - num2; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void multiply() { | ||
| Integer expected = calculatorService.multiply(num1, num2); | ||
| Integer actual = num1 * num2; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void divide() { | ||
| Integer expected = calculatorService.divide(num1, num2); | ||
| Integer actual = num1 / num2; | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void result() { | ||
| String operation = "+"; | ||
| String expected = calculatorService.result(num1, num2, operation, calculatorService.plus(num1, num2)); | ||
| String actual = num1 + " " + operation + " " + num2 + " = " + calculatorService.plus(num1,num2); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldThrowIllegalArgumentExceptionIfDivideByZero() { | ||
| assertThrows(IllegalArgumentException.class, | ||
| () -> calculatorService.divide(1, 0)); | ||
| } | ||
|
|
||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.