Reputation:
I'm brand new to JUnit so I'm having a bit of trouble. I'm currently running JUint 4, and I'm trying to write a test that is specifically supposed to fail, yet it keeps passing as successful. I need your help, as I feel I'm writing these correctly, yet they're not working as intended. Here's my main class:
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class PrimeNumbers implements Iterable<Integer> {
private List<Integer> primes = new ArrayList<Integer>();
/*
* creates a list of n prime numbers
*
* @param n - the number of primes to compute silently treats negative
* arguments as zero
*/
public void computePrimes(int n) {
int count = 1; // count of primes
int number = 2; // number tested for primeness
boolean isPrime; // is this number a prime
while (count <= n) {
isPrime = true;
for (int divisor = 2; divisor <= number / 2; divisor++) {
if (number % divisor == 0) {
isPrime = false;
break; // for loop
}
}
if (isPrime && (number % 10 != 9)) { // THIS IS THE FAULT!!!
primes.add(number);
count++;
}
number++;
}
}
@Override
public Iterator<Integer> iterator() {
return primes.iterator();
}
@Override
public String toString() {
return primes.toString();
}
}
and here's my test class:
import org.junit.Test;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import static org.junit.Assert.*;
public class PrimeNumbersTest {
@Test
public void test(){
//instantiate prime object, create primes list, and iterator
PrimeNumbers prime = new PrimeNumbers();
List<Integer> primeList = new ArrayList<Integer>();
//array holding 19 so I can compare the objects
int[] primeArray = new int[]{2,3,5,7,11,13,17,19};
int i = 0;
int n = 8;
//call the method
prime.computePrimes(n);
Iterator<Integer> primeIterator = primeList.iterator();
//loop to iterate through the list and then check last item if it is equal to 19
while(primeIterator.hasNext()){
if(!(primeIterator.equals(primeArray[i]))) {
assertTrue("incorrect", primeIterator.equals(primeArray[i]));
}
i++;
primeIterator.next();
}
}
}
Thanks guys for the help!
Upvotes: 1
Views: 351
Reputation: 48015
Have a look at these two lines:
List<Integer> primeList = new ArrayList<Integer>();
...
Iterator<Integer> primeIterator = primeList.iterator();
You create an empty list and then test the contents of that list. Since there's nothing in that list your code never enters the while
loop and hence the test assertion is never invoked.
If you replace this ...
Iterator<Integer> primeIterator = primeList.iterator();
... with this ...
Iterator<Integer> primeIterator = prime.iterator();
... then you'll be testing the iterator created by your PrimeNumbers
class. This must be what you want to do though it should be noted that this invocation will always return false: primeIterator.equals(primeArray[i])
(since it is comparing the iterator, rather than a value inside the iterator, against the 'expected' array) so your test will fail on its first iteration over the while loop. This would be a false negative.
It looks like you are trying to prove that PrimeNumbers
(which has been coded to not return 19) does not return 19 when it is asked to return the first 8 prime numbers. If so, then the following might be a more compact - and specific - way to do that:
@Test
public void test() {
//instantiate prime object, create primes list, and iterator
PrimeNumbers prime = new PrimeNumbers();
int n = 8;
int unexpectedPrimeNumber = 19;
// call the method
prime.computePrimes(n);
// gather the generated prime numbers into a list for easier assertion
List<Integer> primeList = new ArrayList<>();
prime.iterator().forEachRemaining(integer -> primeList.add(integer));
assertEquals("Should contain " + n + " prime numbers!", n, primeList.size());
assertFalse("Should not contain the unexpected prime number: " + unexpectedPrimeNumber, primeList.contains(unexpectedPrimeNumber));
}
Upvotes: 2