Skip to content

Commit eff343b

Browse files
author
Grzegorz Piwowarek
committed
Add first test
1 parent ff84127 commit eff343b

3 files changed

Lines changed: 31 additions & 3 deletions

File tree

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package com.baeldung.immutable;
22

3-
public class Address {
3+
import org.immutables.value.Value;
44

5+
@Value.Immutable
6+
public interface Address {
7+
String getStreetName();
8+
Integer getNumber();
59
}

immutables/src/main/java/com/baeldung/immutable/Person.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.immutables.value.Value;
44

55
@Value.Immutable
6-
public class Person {
7-
private String name;
6+
public abstract class Person {
7+
abstract String getName();
8+
abstract Integer getAge();
89
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.immutable;
2+
3+
import org.junit.Test;
4+
5+
import static org.assertj.core.api.Assertions.assertThat;
6+
7+
public class ImmutablePersonTest {
8+
9+
@Test
10+
public void whenModifying_shouldCreateNewInstance() throws Exception {
11+
final com.baeldung.immutable.ImmutablePerson john = com.baeldung.immutable.ImmutablePerson.builder()
12+
.age(42)
13+
.name("John")
14+
.build();
15+
16+
final com.baeldung.immutable.ImmutablePerson john43 = john.withAge(43);
17+
18+
assertThat(john)
19+
.isNotSameAs(john43);
20+
assertThat(john.getAge())
21+
.isEqualTo(42);
22+
}
23+
}

0 commit comments

Comments
 (0)