From eff343b59679b3525235b99ce6c7fefb6e354bc6 Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Sat, 30 Jul 2016 10:50:47 +0200 Subject: [PATCH] Add first test --- .../java/com/baeldung/immutable/Address.java | 6 ++++- .../java/com/baeldung/immutable/Person.java | 5 ++-- .../immutable/ImmutablePersonTest.java | 23 +++++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 immutables/src/test/java/com/baeldung/immutable/ImmutablePersonTest.java 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