27 lines
679 B
Java
Raw Normal View History

2016-07-30 10:50:47 +02:00
package com.baeldung.immutable;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
2016-07-30 12:22:43 +02:00
import static org.mutabilitydetector.unittesting.MutabilityAssert.assertImmutable;
2016-07-30 10:50:47 +02:00
public class ImmutablePersonTest {
@Test
public void whenModifying_shouldCreateNewInstance() throws Exception {
2016-07-30 12:56:20 +02:00
final ImmutablePerson john = ImmutablePerson.builder()
2016-07-30 10:50:47 +02:00
.age(42)
.name("John")
.build();
2016-07-30 12:56:20 +02:00
final ImmutablePerson john43 = john.withAge(43);
2016-07-30 10:50:47 +02:00
assertThat(john)
.isNotSameAs(john43);
2016-07-30 12:08:27 +02:00
2016-07-30 10:50:47 +02:00
assertThat(john.getAge())
.isEqualTo(42);
2016-07-30 12:22:43 +02:00
2016-07-30 12:56:20 +02:00
assertImmutable(ImmutablePerson.class);
2016-07-30 10:50:47 +02:00
}
}