简体   繁体   中英

How to test an array of objects with JUnit

Trying to check a method which adds a module object to an array works correctly. How do I write a JUnit test which tests to see if an object has indeed been added to the array?

Here is what I have so far:

@Test
public void testAddModule() {
    Student chris = new Student("Chris", "1");
    Module csc8001 = new Module("CSC8001", "Programming and data structures", 5, 5, 0, 7);
    Module csc8002 = new Module("CSC8002", "Programming II", 5, 5, 0, 7);   
    chris.addModule(csc8001);
    chris.addModule(csc8002);


    Module [] expectedResult = {csc8001,csc8002};
    ModuleRecord[] resultArray = Student.moduleRecords;

    Assert.assertArrayEquals( expectedResult, resultArray );

}

The problem I have is the array is just storing references to the objects, how do I test to see if the array is storing the correct information?

You're on the right track: assertArrayEquals should work. The concept you're looking for is the difference between shallow equals (" == ") and deep equals (" .equals ") , which is related to the difference between reference equality (" == ") and object equality (" .equals ") .

If two arrays have the same length, and contain the exact same primitive values and references, it's easy to say that they're equal: [0, 1] equals [0, 1] even if the arrays themselves are different objects, and the same happens for [csc8001, csc8002] and [csc8001, csc8002] . This is known as "shallow" equality, and it's very very fast to compute.

If you're actually looking to compare the objects using their equals methods, you need "deep" equality. This checks the proper number of entries, and checks that x[n].equals(y[n]) for all n . This may take longer, because Java can't just compare the references; it actually calls a method on the objects. This is only of value to you if you override equals , though; if your Module doesn't override equals it uses Object's built-in equals method, which acts the same way == does.

In any case, it's not very clear in the documentation, but Assert.assertArrayEquals calls Assert.internalArrayEquals , which basically does a deep comparison using .equals .

That leaves you with three options:

  • Keep using assertArrayEquals . That's what it's there for, and because equal references imply equal objects, it's the right thing to do.
  • Keep using assertArrayEquals but also write a Module.equals method to prove the data itself is identical. Remember, this is only an issue if you want to prove that two different instances behave as if they were equal based on their data, and you probably don't have to worry about that right now.
  • Break out what you're testing for, as Jason mentioned, which confirms that the data is correct without having you write an equals method:

     assertEquals(2, resultArray.length); Module addedModule = resultArray[1]; assertEquals("CSC8002", addedModule.code); /* ... */ 

If you ever do try overriding equals , be careful: You'll need to uphold some rules and you'll also need to override hashCode . Read more here.

蛮力方法是检查数组是否具有预期的条目数,然后存储在数组中的每个对象均具有预期的属性值。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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