Skip to content

Instantly share code, notes, and snippets.

@sergiilagutin
Created March 9, 2015 06:19
Show Gist options
  • Save sergiilagutin/6f5edfba8aab41b4c0b3 to your computer and use it in GitHub Desktop.
Save sergiilagutin/6f5edfba8aab41b4c0b3 to your computer and use it in GitHub Desktop.
Java factorial using TDD
public class Factorial {
public int fact(int number) {
int i = 1;
int result = 1;
while (i <= number) {
result = result * i;
i++;
}
return result;
}
}
import org.junit.Test;
import static org.junit.Assert.*;
public class FactorialTest {
@Test
public void factorialZero() {
Factorial f = new Factorial();
int expected = 1;
int actual = f.fact(0);
assertEquals(expected, actual);
}
@Test
public void factorialOne() {
Factorial f = new Factorial();
int expected = 1;
int actual = f.fact(1);
assertEquals(expected, actual);
}
@Test
public void factorialTwo() {
Factorial f = new Factorial();
int expected = 2;
int actual = f.fact(2);
assertEquals(expected, actual);
}
@Test
public void factorialFive() {
Factorial f = new Factorial();
int expected = 120;
int actual = f.fact(5);
assertEquals(expected, actual);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment