需要协助进行JUnit测试

时间:2019-03-24 19:47:59

标签: java unit-testing junit

我是JUnit的新手,所以遇到了一些麻烦。我当前正在运行JUint 4,并且我正在尝试编写一个明确应该失败的测试,但它一直通过并保持成功。我需要您的帮助,因为我觉得我写的没错,但它们没有按预期工作。这是我的主要课程:

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();
    }

}

这是我的测试课:

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();
        }
    }
}

谢谢大家的帮助!

1 个答案:

答案 0 :(得分:2)

看看这两行:

List<Integer> primeList = new ArrayList<Integer>();
...
Iterator<Integer> primeIterator = primeList.iterator();

您创建一个空列表,然后测试那个列表的内容。由于该列表中没有任何内容,因此您的代码永远不会进入while循环,因此永远不会调用测试断言。

如果您替换此...

Iterator<Integer> primeIterator = primeList.iterator();

...与此...

Iterator<Integer> primeIterator = prime.iterator();

...那么您将测试PrimeNumbers类创建的迭代器。尽管必须注意该调用将始终返回false:primeIterator.equals(primeArray[i])(因为它正在将迭代器而不是迭代器内部的值与“ expected”数组进行比较),但这必须是您要执行的操作您的测试将在while循环的第一次迭代中失败。这将是一个假阴性。

您似乎正在试图证明PrimeNumbers(已被编码为 not 返回19)在被要求返回前8个质数时没有返回19。如果是这样,那么以下方法可能是一种更紧凑且更具体的方法:

@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));
}