Tests and implementation
/** * Dealing with prime numbers. */ public class Prime { /** * Check whether a given value is prime or not ❶ * @param value A positive value * @return true if and only if value is a prime number. */ public static boolean isPrime(int value) { return true ❷; //TODO: Dummy value to be implemented correctly } }
An informal specification of the method's expected behaviour. This comprises the descriptions of all method parameters among with the expected outcome. Note that |
|
Since our current implementation is just a skeleton we simply return a constant value. Other choices:
In fact every syntactically correct expression will do since we defer our implementation to a later step. Thus the only requirement with respect to our code is its ability to get compiled. Returning a single value obviously is the most simple way to comply. |
for (int i = 1; i < 20;i++) {
System.out.println(i + " is " + (Prime.isPrime(i) ? " a " : " not a ")
+ " prime number");
}
1 is a prime number 2 is a prime number 3 is a prime number 4 is a prime number 5 is a prime number ...
Input | Expected output | Input | Expected output |
---|---|---|---|
1 |
false |
7 |
true |
2 |
true |
8 |
false |
3 |
true |
9 |
false |
4 |
false |
10 |
false |
5 |
true |
11 |
true |
6 |
false |
12 |
false |
goik@goiki Prime_v01> mvn test ... Running de.hdm_stuttgart.mi.sd1.PrimeTest Tests run: 2, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.065 sec <<< FAILURE! ... test_1_isNotPrime(de.hdm_stuttgart.mi.sd1.PrimeTest) Time elapsed: 0.001 sec <<< FAILURE! java.lang.AssertionError at org.junit.Assert.fail(Assert.java:86) at org.junit.Assert.assertTrue(Assert.java:41) at org.junit.Assert.assertFalse(Assert.java:64) at org.junit.Assert.assertFalse(Assert.java:74) ...
Similar to a main(...)
method execution
we may execute Junit Tests using our IDE:
❶ |
Test |
❷ |
Test |
Before replacing the skeleton implementation Figure 306, “Step 1 + 2: Specify method, write skeleton ” we supply additional tests:
@Test public void test_Primes() {
Assert.assertTrue(Prime.isPrime(3));
Assert.assertTrue(Prime.isPrime(5));
Assert.assertTrue(Prime.isPrime(7));
Assert.assertTrue(Prime.isPrime(11));
... }
@Test public void testOddNonPrimes() {
Assert.assertFalse(Prime.isPrime(9));
Assert.assertFalse(Prime.isPrime(15));
Assert.assertFalse(Prime.isPrime(21));...}
Since all even numbers greater than two are non-prime we add:
@Test public void testEvenNonPrimes() {
for (int i = 2; i < 100; i++) {
Assert.assertFalse(Prime.isPrime(2 * i));
}
}
Now its time actually implementation
isPrime()
:
public static boolean isPrime(int value) {
for (int i = 2; i < value; i++) {
if (0 == value % i) { // i divides value
return false;
}
}
return value != 1;
}
goik@goiki Prime_v01> mvn test
...
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running de.hdm_stuttgart.mi.sd1.PrimeTest
Tests run: 5, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.055 sec
Results :
Tests run: 5, Failures: 0, Errors: 0, Skipped: 0