diff --git a/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/objectmutability/ImmutablePerson.java b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/objectmutability/ImmutablePerson.java new file mode 100644 index 0000000000..e67c802a2e --- /dev/null +++ b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/objectmutability/ImmutablePerson.java @@ -0,0 +1,21 @@ +package com.baeldung.objectmutability; + +public final class ImmutablePerson { + + private final String name; + private final int age; + + public ImmutablePerson(String name, int age) { + this.name = name; + this.age = age; + } + + public String getName() { + return name; + } + + public int getAge() { + return age; + } +} + diff --git a/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/objectmutability/ImmutableObjectExamplesUnitTest.java b/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/objectmutability/ImmutableObjectExamplesUnitTest.java new file mode 100644 index 0000000000..4c41050928 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/objectmutability/ImmutableObjectExamplesUnitTest.java @@ -0,0 +1,33 @@ +package com.baeldung.objectmutability; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotSame; + +public class ImmutableObjectExamplesUnitTest { + + @Test + public void givenImmutableString_whenConcatString_thenNotSameAndCorrectValues() { + String originalString = "Hello"; + String modifiedString = originalString.concat(" World"); + + assertNotSame(originalString, modifiedString); + + assertEquals("Hello", originalString); + assertEquals("Hello World", modifiedString); + } + + @Test + public void givenImmutableInteger_whenAddInteger_thenNotSameAndCorrectValue() { + Integer immutableInt = 42; + Integer modifiedInt = immutableInt + 8; + + assertNotSame(immutableInt, modifiedInt); + + assertEquals(42, (int) immutableInt); + assertEquals(50, (int) modifiedInt); + } +} + + diff --git a/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/objectmutability/ImmutablePersonUnitTest.java b/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/objectmutability/ImmutablePersonUnitTest.java new file mode 100644 index 0000000000..5bd6abbb4a --- /dev/null +++ b/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/objectmutability/ImmutablePersonUnitTest.java @@ -0,0 +1,17 @@ +package com.baeldung.objectmutability; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class ImmutablePersonUnitTest { + + @Test + public void givenImmutablePerson_whenAccessFields_thenCorrectValues() { + ImmutablePerson person = new ImmutablePerson("John", 30); + + assertEquals("John", person.getName()); + assertEquals(30, person.getAge()); + } +} + diff --git a/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/objectmutability/MutableObjectExamplesUnitTest.java b/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/objectmutability/MutableObjectExamplesUnitTest.java new file mode 100644 index 0000000000..6c770d68af --- /dev/null +++ b/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/objectmutability/MutableObjectExamplesUnitTest.java @@ -0,0 +1,28 @@ +package com.baeldung.objectmutability; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class MutableObjectExamplesUnitTest { + + @Test + public void givenMutableString_whenAppendElement_thenCorrectValue() { + StringBuilder mutableString = new StringBuilder("Hello"); + mutableString.append(" World"); + + assertEquals("Hello World", mutableString.toString()); + } + + @Test + public void givenMutableList_whenAddElement_thenCorrectSize() { + List mutableList = new ArrayList<>(); + mutableList.add("Java"); + + assertEquals(1, mutableList.size()); + } +} +