Sunday, 15 May 2016

MutabilityDetector: Check Immutability property of classes

MutabilityDetector  is a lightweight analysis tool for detecting whether given class is immutable (or) not. I am using following maven dependency for this tutorial.

<dependency>
    <groupId>org.mutabilitydetector</groupId>
    <artifactId>MutabilityDetector</artifactId>
    <version>0.9.5</version>
</dependency>

Mutability detector takes a class (or) classes and analyze whether this class is immutable (or) not.

assertImmutable(Student.class);


Above statement pass the test case, if Student class is immutable, else fail the test case.

public final class Student {
 private final int id;
 private final String name;

 Student(int id, String name) {
  this.id = id;
  this.name = name;
 }

 public int getId() {
  return id;
 }

 public String getName() {
  return name;
 }

}


import static org.mutabilitydetector.unittesting.MutabilityAssert.assertImmutable;

import org.junit.Test;

public class TestMutability {

 @Test
 public void checkMyClassIsImmutable() {
  assertImmutable(Student.class);
 }
}


Home

No comments:

Post a Comment