Junit test of a class requires objects of other classes

Tags: , ,

I have a class defined as such:

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?
            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.

My question is, apart from

import MapObject.*;

in junit that allows me to initialize a fixture in


Are there any better ways?


It’s probably better to create the input object (i.e. the mapObject[][]) in the test case itself, rather than in a @Before method. This allows you to create several test cases with different input objects.


@Test void testWithSpecificArrayConfiguration1() {
    mapObject[][] objectConfig1 = createConfig1();
    // verification steps for config 1
@Test void testWithSpecificArrayConfiguration2() {
    mapObject[][] objectConfig2 = createConfig2();
    // 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[][]; but it’s hard to tell without knowing the context of the code.

Source: stackoverflow