같은 것을 수행하여 예를 들어,이 방법을 고려한다.
이
는 JUnit 테스트가 작동하는 방법의 예입니다 : 당신이 주장 할 경우
public class Factorial {
/**
* Calculates the factorial of the specified number in linear time and constant space complexity.<br>
* Due to integer limitations, possible calculation of factorials are for n in interval [0,12].<br>
*
* @param n the specified number
* @return n!
*/
public static int calc(int n) {
if (n < 0 || n > 12) {
throw new IllegalArgumentException("Factorials are defined only on non-negative integers.");
}
int result = 1;
if (n < 2) {
return result;
}
for (int i = 2; i <= n; i++) {
result *= i;
}
return result;
}
}
import static org.junit.Assert.*;
import org.junit.Test;
public class FactorialTest {
@Test
public void factorialOfZeroShouldBeOne() {
assertEquals(1, Factorial.calc(0));
}
@Test
public void factorialOfOneShouldBeOne() {
assertEquals(1, Factorial.calc(1));
}
@Test
public void factorialOfNShouldBeNTimesFactorialOfNMinusOne() {
for (int i = 2; i <= 12; i++) {
assertEquals(i * Factorial.calc(i - 1), Factorial.calc(i));
System.out.println(i + "! = " + Factorial.calc(i));
}
}
@Test(expected = IllegalArgumentException.class)
public void factorialOfNegativeNumberShouldThrowException() {
Factorial.calc(-1);
Factorial.calc(Integer.MIN_VALUE);
}
@Test(expected = IllegalArgumentException.class)
public void factorialOfNumberGreaterThanTwelveShouldThrowException() {
Factorial.calc(13);
Factorial.calc(20);
Factorial.calc(50);
}
}
당신은 또한 당신의 '테스트'코드 – radimpe
를 추가 할 수있는 매개 변수 귀하의 방법은 null이 아닌데, 그것은 입력 검증과 같은데, 단위 테스트는 아닙니다. 그것은 Sender 클래스의 주장 일 것입니다. 단위 테스트의 경우 Sender 클래스를 호출하여 작동하는지 확인합니다. – Thilo