Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Currently I am in process of writing large batches of Unit-Tests in the Project

ID: 639451 • Letter: C

Question

Currently I am in process of writing large batches of Unit-Tests in the Project I am working on. Additionally we recently Introduced GUI-Testing. This project is a relatively simple webapp, on basis of Java EE 6.

Now my colleage did write quite some GuiTests, and I did write quite some Unit-Tests.

For both tests we need data to run them. Now we both took different approaches on how we "store" the data we use.

Solution 1:

public class UnitTestData {
     public static final String DATA_PIECE = "some data";
     public static final String MORE_DATA = "and more data";
     //... and so on
}
Solution 2:

public class GuiTestData {
    public String getDataPiece() {
        return "some data";
    }

    public String getMoreData() {
        return "and more data";
    }
    //... and so on.
}
My question now is: Which of these approaches is the objectively better one?

Explanation / Answer

There are two arguments for using accessor methods instead of public variables:

Access Control: A method allows us to publish only a getter method, or to add value-based (in contrast to type-based) constraints on a setter. With public final identifiers, this point is irrelevant, as that value can't be changed, and invariants therefore can't be broken by client code.

Uniform Access: The method of accessing data in an object should not leak information on whether this data is stored or computed on the fly. In languages without