-
-
Save okellodaniel/e8770b8a0a93ea4102d1897b93f5dadf 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