繁体   English   中英

Junit 测试 class 需要其他类的对象

[英]Junit test of a class requires objects of other classes

我有一个 class 定义如下:

public class Monster {
    public static final int ARENA_SIZE = 480;

    public int health; //can be negative since it will then be removed
    public int speed;
    public int counter;
    public int x; //current position
    public int y; //current position
    public mapObject next;
    public void nextAlgorithm(mapObject[][] map) {
        aNode[][] aNodeMap = newANodeMap(map); //1. create a new aNodeMap

        PriorityQueue<aNode> pq = newPriorityMap(aNodeMap[this.x][this.y]); //2. create a new priority queue with starting point added
        aNode current;

        while (pq.isEmpty() == false) {
            current = pq.poll();
            if (endPointReached(current.x, current.y)) //3. Is the end point reached?
                break;
            aNode[] neighbours = findNeighbour(current.x, current.y, aNodeMap); //4. The end point isn't reached, find me the neigbours
            for (aNode neighbour : neighbours) //5. process all my neighbours
                processNeighbour(current, neighbour, pq);
        }
        next = updateNext(aNodeMap[ARENA_SIZE - 1][ARENA_SIZE - 1], this.x, this.y); //6. Update my next after all these work
    }

简单来说,有一个算法需要其他class输入,mapObject,也是我自己写的另一个package。

我的问题是,除了

import MapObject.*;

在 junit 中,它允许我在

@Before

有没有更好的方法?

在测试用例本身而不是在@Before方法中创建输入 object (即mapObject[][] )可能更好。 这允许您使用不同的输入对象创建多个测试用例。

IE

@Test void testWithSpecificArrayConfiguration1() {
    mapObject[][] objectConfig1 = createConfig1();
    testMonster.nextAlgorith(objectConfig1);
    // verification steps for config 1
}
@Test void testWithSpecificArrayConfiguration2() {
    mapObject[][] objectConfig2 = createConfig2();
    testMonster.nextAlgorith(objectConfig2);
    // verification steps for config 2
}

请注意,输入是mapObject感觉不对,但实际算法适用于aNode[][] 但是如果不知道代码的上下文就很难说。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM