|
1 | 1 | package com.baeldung.lombok.builder; |
2 | 2 |
|
3 | | -import org.junit.jupiter.api.Test; |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
4 | 4 |
|
5 | | -import static org.assertj.core.api.Assertions.*; |
| 5 | +import org.junit.jupiter.api.Test; |
6 | 6 |
|
7 | | -public class BuilderUnitTest |
8 | | -{ |
| 7 | +public class BuilderUnitTest { |
9 | 8 |
|
10 | 9 | @Test |
11 | 10 | public void givenBuilder_WidgetIsBuilt() { |
12 | | - Widget testWidget = Widget.builder().name("foo").id(1).build(); |
13 | | - assertThat(testWidget.getName()) |
14 | | - .isEqualTo("foo"); |
15 | | - assertThat(testWidget.getId()) |
16 | | - .isEqualTo(1); |
| 11 | + Widget testWidget = Widget.builder() |
| 12 | + .name("foo") |
| 13 | + .id(1) |
| 14 | + .build(); |
| 15 | + assertThat(testWidget.getName()).isEqualTo("foo"); |
| 16 | + assertThat(testWidget.getId()).isEqualTo(1); |
17 | 17 | } |
18 | 18 |
|
19 | 19 | @Test |
20 | 20 | public void givenToBuilder_whenToBuilder_BuilderIsCreated() { |
21 | 21 |
|
22 | | - Widget testWidget = Widget.builder().name("foo").id(1).build(); |
| 22 | + Widget testWidget = Widget.builder() |
| 23 | + .name("foo") |
| 24 | + .id(1) |
| 25 | + .build(); |
23 | 26 | Widget.WidgetBuilder widgetBuilder = testWidget.toBuilder(); |
24 | 27 |
|
25 | | - Widget newWidget = widgetBuilder.id(2).build(); |
26 | | - assertThat(newWidget.getName()) |
27 | | - .isEqualTo("foo"); |
28 | | - assertThat(newWidget.getId()) |
29 | | - .isEqualTo(2); |
| 28 | + Widget newWidget = widgetBuilder.id(2) |
| 29 | + .build(); |
| 30 | + assertThat(newWidget.getName()).isEqualTo("foo"); |
| 31 | + assertThat(newWidget.getId()).isEqualTo(2); |
30 | 32 | } |
31 | 33 |
|
32 | | - |
33 | | - |
34 | 34 | @Test |
35 | 35 | public void givenBuilderMethod_ClientIsBuilt() { |
36 | | - ImmutableClient testImmutableClient = ClientBuilder.builder().name("foo").id(1).build(); |
37 | | - assertThat(testImmutableClient.getName()) |
38 | | - .isEqualTo("foo"); |
39 | | - assertThat(testImmutableClient.getId()) |
40 | | - .isEqualTo(1); |
| 36 | + ImmutableClient testImmutableClient = ClientBuilder.builder() |
| 37 | + .name("foo") |
| 38 | + .id(1) |
| 39 | + .build(); |
| 40 | + assertThat(testImmutableClient.getName()).isEqualTo("foo"); |
| 41 | + assertThat(testImmutableClient.getId()).isEqualTo(1); |
41 | 42 | } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void givenBuilderAtMethodLevel_ChildInheritingParentIsBuilt() { |
| 46 | + Child child = Child.childBuilder() |
| 47 | + .parentName("Andrea") |
| 48 | + .parentAge(38) |
| 49 | + .childName("Emma") |
| 50 | + .childAge(6) |
| 51 | + .build(); |
| 52 | + |
| 53 | + assertThat(child.getChildName()).isEqualTo("Emma"); |
| 54 | + assertThat(child.getChildAge()).isEqualTo(6); |
| 55 | + assertThat(child.getParentName()).isEqualTo("Andrea"); |
| 56 | + assertThat(child.getParentAge()).isEqualTo(38); |
| 57 | + } |
| 58 | + |
42 | 59 | } |
0 commit comments