Skip to content

Instantly share code, notes, and snippets.

@jirkapenzes
Created October 13, 2016 21:41
Show Gist options
  • Save jirkapenzes/ea09ffeb835a960c8b6dfc7cde9fe800 to your computer and use it in GitHub Desktop.
Save jirkapenzes/ea09ffeb835a960c8b6dfc7cde9fe800 to your computer and use it in GitHub Desktop.
package demo;
public class Calculator {
enum Operation {
Addition, Subtraction
}
public int calculate(Operation operation, int a, int b) {
switch (operation) {
case Addition:
return addition(a, b);
case Subtraction:
return subtraction(a, b);
}
throw new RuntimeException("Unsupported exception");
}
private int addition(int a, int b) {
return a + b;
}
private int subtraction(int a, int b) {
return a - b;
}
}
package demo;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class CalculatorTest {
private Calculator calculator;
@Before
public void setUp() throws Exception {
calculator = new Calculator();
}
@Test
public void shouldPerformAddition() throws Exception {
int actual = calculator.calculate(Calculator.Operation.Addition, 1, 3);
int expected = 4;
Assert.assertEquals(expected, actual);
}
@Test
public void shouldPerformSubtraction() throws Exception {
int actual = calculator.calculate(Calculator.Operation.Subtraction, 3, 1);
int expected = 2;
Assert.assertEquals(expected, actual);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment