반응형
테스트 코드 예시
package org.example;
import java.util.stream.Stream;
import org.junit.jupiter.api.DisplayName;
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.MethodSource;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.junit.jupiter.params.provider.Arguments.arguments;
public class CalculatorTest {
@DisplayName("덧셈 연산을 수행")
@ParameterizedTest
@MethodSource("formulaAndResult")
void additionTest(int operand1, String operator, int operand2, int result) {
Calculator calculator = new Calculator();
int calculateResult = calculator.calculate(operand1, operator, operand2);
assertThat(calculateResult).isEqualTo(result);
}
private static Stream<Arguments> formulaAndResult() {
return Stream.of(
arguments(1, "+", 2, 3),
arguments(1, "-", 2, -1),
arguments(4, "*", 2, 8),
arguments(4, "/", 2, 2)
);
}
@DisplayName("나눗셈에서 0을 나눈경우 익셉션 발생")
@Test
void wrongDivisionTest() {
Calculator calculator = new Calculator();
assertThatCode(() -> calculator.calculate(10, "/", 0)).isInstanceOf(IllegalArgumentException.class);
}
}
반응형