vendredi 22 mars 2019

How to create unit test to method that returns collection?

I'm have a method:

    public List<Numbers> findNumbers(int[] array) {
    List<Numbers> list = new ArrayList<Numbers>();
    for (int a = 0; a < array.length; a++) {
        for (int b = a; b < array.length; b++) {
            for (int c = b; c < array.length; c++) {
                for (int d = c; d < array.length; d++) {
                    if (array[a] + array[b] + array[c] + array[d] == n) {
                        list.add(new Number(array[a], array[b], array[c], array[d]));
                    } else {
                        haveNumbers = false;
                    }
                }
            }
        }
    }
    return list;
}

It accepts an array of Integers, find Lagrange Numbers to some "n" variale, which declared in class, create objects Number which contains just fields, getters and setters:

public class Number implements Numbers {

private int a;
private int b;
private int c;
private int d;

public Number(int a, int b, int c, int d) {
    this.a = (int) Math.sqrt(a);
    this.b = (int) Math.sqrt(b);
    this.c = (int) Math.sqrt(c);
    this.d = (int) Math.sqrt(d);
}
...

And after add it to collection of Number objects and return it. I don't really know how to write tests, but I ask how can I write unit test on JUnit or TestNG for this method.

Aucun commentaire:

Enregistrer un commentaire