javaintellij中的junit测试|如何编写专门测试set/get方法的测试?

9rygscc1  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(288)

所以我有这个类和这两个方法:

public class RiddleNode {
    public final RiddleType type;
    public final int value; // Zahlenwert, falls Knotentyp == VAL
    public final boolean result; // Endknoten?
    private final RiddleNode[] neighbours = new RiddleNode[4]; // direkte Nachbarknoten
    boolean visited = false;

    public RiddleNode(RiddleType type, int value, boolean result) {
        this.type = type;
        this.value = value;
        this.result = result;
    }

设置方法:

public void setNeighbours(RiddleNode... neighbours) {
            if (neighbours == null) {
                throw new IllegalArgumentException("neighbours must not be null!");
            }
            if (neighbours.length > 4) {
                throw new IllegalArgumentException("Each node must have 2, 3 or 4 non-null neighbours - not more!");
            }
            int count = 0;
            for (int i = 0; i < neighbours.length; i++) {
                if (neighbours[i] != null) {
                    count++;
                }
                this.neighbours[i] = neighbours[i];
            }
            if (count < 2) {
                throw new IllegalArgumentException("Each node must have 2, 3 or 4 non-null neighbours - not less!");
            }
        }

获取方法:

public RiddleNode[] getNeighbours() {
        return java.util.Arrays.copyOf(neighbours, neighbours.length);
    }

我现在想知道如何在另一个类(在我的例子中称为“riddletest”)中编写一个测试来测试这两个方法。因为我甚至不能得到setneights方法在我的另一个类中的参数,或者在我的另一个类中可能使用相同的参数吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题