Immutability test failing
See original GitHub issueimport java.util.ArrayList;
import java.util.List;
public final class Student {
private final int id;
private final String name;
private final List<String> hobbies;
Student(int id, String name, List<String> hobbies) {
this.id = id;
this.name = name;
this.hobbies = hobbies;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public List<String> getHobbies() {
return new ArrayList<> (hobbies);
}
}
When I tried to test for immutability using “assertImmutable(Student.class);”, I am getting following error.
Attempt to wrap mutable collection using a non-whitelisted unmodifiable wrapper method. [Field: hobbies…]
Issue Analytics
- State:
- Created 7 years ago
- Comments:10 (1 by maintainers)
Top Results From Across the Web
Testing Object's Immutability: Is This a Good Idea?
The crazy thing here is that you might not realize an immutability failure, they are usually hard to catch, because, they appear in...
Read more >Unit test is failing while converting an entity to Immutable ...
I was able to convert an entity to Immutable model object but unit test is failing for this. It is throwing NPE at...
Read more >Your Unit Tests Should Be Immutable - DEV Community
If you go into those failing tests and update the tests with the new assumption it's highly likely that an error will get...
Read more >Class invariants, immutability, and testing
Class invariants, immutability, and testing. Josh Bloch ... Implementation testing with assertions ... Never write code without a failing test.
Read more >Unit testing Java data classes immutability with the Mutability ...
the test will fail if the class is not immutable. For example, if a field is not final and it has a setter...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hi,
While Stephan is correct that wrapping in unmodifiable in the getter makes the class immutable, unfortunately Mutability Detector does not realise that. It’s analysis is a bit too naïve.
I suggest copying and wrapping in the constructor:
this.hobbies = Collections.unmodifiableList(new ArrayList<>(hobbies));
That should make the test pass and have other benefits:
Let us know how that works for you.
Cheers!
P.S. really satisfying to see other people helping out on issues, thanks Stephan. On 15 May 2016 8:13 pm, “harikrishna553” notifications@github.com wrote:
@Grundlefleck, good suggestion on moving everything in the constructor; I almost feel dumb for not suggesting it myself. I guess it’s because in my own code I always use Guava’s immutable collections, so I haven’t recently used the “JDK-only” approach. Anyway, happy to help!