1

I am trying out unit testing in Java with JUnit for an assignment. Previously, we were tasked with creating a generic puzzle-solving algorithm using graph searching algorithms.

The important part is that I have an interface called Graph Vertex.

public abstract class GraphVertex { 
abstract public List<GraphVertex> getAdjacentVertices();
    abstract boolean equals(Object o);
    abstract int hashCode();
}

This allows the user to swap between different node types if he so wishes.

Now, I have a concrete class called SliderPuzzleVertex<T> that extends GraphVertex, and holds a 2-dimensional array of T and a set of positions (x, y) that are considered empty. The getAdjacentVertices generates new SliderPuzzleVertex-es of every possible move that can be made.

I need to test getAdjacentVertices for this class.

Using Integer as my parameterized T, I initialized an instance and called the method, and now I am left with a List<GraphVertex>, but I want to test:

  1. That they are all SliderPuzzleVertex.

  2. Their fields (for example, that the size if their position set remained the same),

  3. Specific cases where i give the input and expected output, and check that they are same using equals().

You can easily see that in order to test 2 I would have to cast them to SliderPuzzleVertex, therefore relying on test 1. This test case dependency is what I am trying to avoid

How can I approach this problem?

2
  • 1
    Please edit your question to include a detailed description what the problem is when you try to test these things in your unit test method. Commented May 18 at 12:30
  • Why do cases 1 and 2 need to be separate? Why can't they be part of the same test case? Commented May 24 at 17:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.