简体   繁体   English

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

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

I have a class defined as such:我有一个 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
    }

Simply put, there is an algorithm that requires input from other class, the mapObject, which is also another self-written package by me.简单来说,有一个算法需要其他class输入,mapObject,也是我自己写的另一个package。

My question is, apart from我的问题是,除了

import MapObject.*;

in junit that allows me to initialize a fixture in在 junit 中,它允许我在

@Before

Are there any better ways?有没有更好的方法?

It's probably better to create the input object (ie the mapObject[][] ) in the test case itself, rather than in a @Before method.在测试用例本身而不是在@Before方法中创建输入 object (即mapObject[][] )可能更好。 This allows you to create several test cases with different input objects.这允许您使用不同的输入对象创建多个测试用例。

Ie 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
}

As a note, it doesn't feel right that the input is a mapObject , but the actual algorithm works on an aNode[][] ;请注意,输入是mapObject感觉不对,但实际算法适用于aNode[][] but it's hard to tell without knowing the context of the code.但是如果不知道代码的上下文就很难说。

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

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