diff --git a/immutables/src/main/java/com/baeldung/immutable/Address.java b/immutables/src/main/java/com/baeldung/immutable/Address.java index 5e7cd4f3f9..93474dc043 100644 --- a/immutables/src/main/java/com/baeldung/immutable/Address.java +++ b/immutables/src/main/java/com/baeldung/immutable/Address.java @@ -1,5 +1,9 @@ package com.baeldung.immutable; -public class Address { +import org.immutables.value.Value; +@Value.Immutable +public interface Address { + String getStreetName(); + Integer getNumber(); } diff --git a/immutables/src/main/java/com/baeldung/immutable/Person.java b/immutables/src/main/java/com/baeldung/immutable/Person.java index 9ffee3059e..466daf42c2 100644 --- a/immutables/src/main/java/com/baeldung/immutable/Person.java +++ b/immutables/src/main/java/com/baeldung/immutable/Person.java @@ -3,6 +3,7 @@ package com.baeldung.immutable; import org.immutables.value.Value; @Value.Immutable -public class Person { - private String name; +public abstract class Person { + abstract String getName(); + abstract Integer getAge(); } diff --git a/immutables/src/test/java/com/baeldung/immutable/ImmutablePersonTest.java b/immutables/src/test/java/com/baeldung/immutable/ImmutablePersonTest.java new file mode 100644 index 0000000000..8c53569836 --- /dev/null +++ b/immutables/src/test/java/com/baeldung/immutable/ImmutablePersonTest.java @@ -0,0 +1,23 @@ +package com.baeldung.immutable; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class ImmutablePersonTest { + + @Test + public void whenModifying_shouldCreateNewInstance() throws Exception { + final com.baeldung.immutable.ImmutablePerson john = com.baeldung.immutable.ImmutablePerson.builder() + .age(42) + .name("John") + .build(); + + final com.baeldung.immutable.ImmutablePerson john43 = john.withAge(43); + + assertThat(john) + .isNotSameAs(john43); + assertThat(john.getAge()) + .isEqualTo(42); + } +} \ No newline at end of file