Created
March 9, 2015 06:19
-
-
Save sergiilagutin/6f5edfba8aab41b4c0b3 to your computer and use it in GitHub Desktop.
Java factorial using TDD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Factorial { | |
public int fact(int number) { | |
int i = 1; | |
int result = 1; | |
while (i <= number) { | |
result = result * i; | |
i++; | |
} | |
return result; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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