Skip to content
Snippets Groups Projects
Commit 8eddd755 authored by Patrik Pašek's avatar Patrik Pašek
Browse files

hw2

parent 723e71a4
Branches main
No related tags found
No related merge requests found
...@@ -5,16 +5,19 @@ public class Calculator { ...@@ -5,16 +5,19 @@ public class Calculator {
public int add(int a, int b) { public int add(int a, int b) {
return a + b; return a + b;
} }
public int subtract(int a, int b) { public int subtract(int a, int b) {
// TODO: implementujte tuto metodu
return a - b; return a - b;
} }
public int multiply(int a, int b) { public int multiply(int a, int b) {
// TODO: implementujte tuto metodu
return a * b; return a * b;
} }
public int divide(int a, int b) { public int divide(int a, int b) throws Exception {
// TODO: implementujte tuto metodu if (b == 0) {
return a / b; throw new Exception("Nulou nelze dělit!");
} else {
return a / b;
}
} }
} }
import hw2.Calculator; package hw2;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
...@@ -18,4 +19,29 @@ public class CalculatorTest { ...@@ -18,4 +19,29 @@ public class CalculatorTest {
int result = calc.add(num1, num2); int result = calc.add(num1, num2);
Assertions.assertEquals(expectResult, result); Assertions.assertEquals(expectResult, result);
} }
@ParameterizedTest(name = "{0} - {1} should be equal {2}")
@CsvSource({"1, 2, -1", "5, 3, 2"})
public void subtract_subtractAandB_returnC(int num1, int num2, int expectResult) {
int result = calc.subtract(num1, num2);
Assertions.assertEquals(expectResult, result);
}
@ParameterizedTest(name = "{0} * {1} should be equal {2}")
@CsvSource({"5, 3, 15", "6, 8, 48"})
public void multiply_multiplyAandB_returnC(int num1, int num2, int expectResult) {
int result = calc.multiply(num1, num2);
Assertions.assertEquals(expectResult, result);
}
@ParameterizedTest(name = "{0} / {1} should be equal {2}")
@CsvSource({"15, 3, 5", "64, 8, 8"})
public void divide_divideAnadB_returnC(int num1, int num2, int expectResult) throws Exception {
int result = calc.divide(num1, num2);
Assertions.assertEquals(expectResult, result);
}
@ParameterizedTest(name = "{0} / {1} cannot be divided by zero")
@CsvSource({"15, 0", "64, 0"})
public void divide_divideZero_returnException(int num1, int num2) {
Assertions.assertThrows(Exception.class, () -> calc.divide(num1, num2));
}
} }
No preview for this file type
File deleted
File added
File moved
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment