2

I'm just starting out with testing.

The method I'm trying to test has no return value (void), but it creates a static 2D array (char[][]) in its own class, so from what I understand, that is its side effect.

Here is some mock code:

public class MyClass{

    public static char[][] table;

    public void setTable(int rows, int columns, int number){
        board = new char[n][m];
        // more code that alters the table in a specific way, 
        // depending on the 3rd parameter
    }

Now for the testing, I was thinking of doing something like:

public class SetTableTest{

    @Test
    public void test(){
        MyClass test = new MyClass();
        assertArrayEquals(**********, test.table);
    }
}

I have 2 questions:

  1. Am I allowed comparing to a static variable like I did (test.table) ie. will that actually return an "instance" of the completed table?

  2. I'm fairly certain that there's no assertArrayEquals equivalent for 2D arrays, so how do I go about this?

3 Answers 3

7

The answer is already given, but for the sake of other users I am going to add another approach.

To compare two arrays, double[][] arr1, arr2, one may use Arrays.deepequals(arr1, arr2) which returns true or false. The signature of this fucntion is:

java.util.Arrays.deepEquals(Object[] a1, Object[] a2)

For a unit test a single line would then suffice:

assertTrue(Arrays.deepEquals(arr1, arr2));
Sign up to request clarification or add additional context in comments.

1 Comment

This works great. However one shortcoming is the fact that when the test fails you get expected: <true> but was: <false>, whereas the approach using arrayEquals provides more detail on why the assertion failed: array contents differ at index [...], expected: <expected> but was: <actual>.
2

There is a assertArrayEquals() here http://junit.sourceforge.net/javadoc/org/junit/Assert.html

so you can use static import for it:

import static org.junit.Assert.assertArrayEquals;

It is, however, only "shallow" array equals, so you need to implement the logic itself. Also make sure, you call the setTable() before asserting. Here is something:

import static org.junit.Assert.assertArrayEquals;

public class SetTableTest{

    @Test
    public void test() {
        MyClass test = new MyClass();
        int foo = 42;
        int bar = 42;
        int baz = 42;
        test.setTable(foo, bar, baz)
        for (char[] innerArray : test.table) {
             assertArrayEquals(*****, innerArray);
        }
    }
}

Comments

1

Answers:

  1. Yes, the static variable will return the instance of the completed table, assuming that at the end of setTable() you set the completed table to the table variable. If you don't then it will not have the correct instance.

    However, from a design standpoint, it would be better to have an accessor method, such as getTable() for the completed table if you are setting it to a variable in MyClass, but that is a different issue.

  2. To test that the 2D array is created, I would suggest creating an array that represents each row of the 2D array, example

    char[] row0 == test.table[0]
    char[] row1 == test.table[1].
    

    You would need to create these Arrays yourself filled with the values you would expect to be in the table created from setTable(). Then you can have use assertArrayEquals() for each row. Example:

    public class SetTableTest{
    
        @Test
        public void test(){
            MyClass test = new MyClass();
            test.setTable(2, 2, 5);
            char[] row0 = {x, x} // This is whatever you would expect to be in row 0
            char[] row1 = {x, x} // This is whatever you would expect to be in row 1
            assertArrayEquals(row0, test.table[0]);
            assertArrayEquals(row1, test.table[1]);
        }
    }
    

Comments

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.