From eceb217e7a30336723c6fbd8cd2688ee3eab9d41 Mon Sep 17 00:00:00 2001 From: Eric Goebelbecker Date: Mon, 21 May 2018 22:06:04 -0400 Subject: [PATCH] BAEL-1787 - Use Lombok @Builder Annotation (#4302) * BAEL-1787 - using Lombok @Builder on methods * BAEL-1787 - rename class. Add AssertJ to Lombok project. * BAEL-1787 - rename class again. Change AssertJ tests. * BAEL-1787 - add Builder directory. Move method cocde to that package. Add code for using builder with a class. --- .../lombok/builder/ClientBuilder.java | 11 +++++ .../lombok/builder/ImmutableClient.java | 11 +++++ .../com/baeldung/lombok/builder/Widget.java | 13 ++++++ .../lombok/builder/BuilderUnitTest.java | 42 +++++++++++++++++++ 4 files changed, 77 insertions(+) create mode 100644 lombok/src/main/java/com/baeldung/lombok/builder/ClientBuilder.java create mode 100644 lombok/src/main/java/com/baeldung/lombok/builder/ImmutableClient.java create mode 100644 lombok/src/main/java/com/baeldung/lombok/builder/Widget.java create mode 100644 lombok/src/test/java/com/baeldung/lombok/builder/BuilderUnitTest.java diff --git a/lombok/src/main/java/com/baeldung/lombok/builder/ClientBuilder.java b/lombok/src/main/java/com/baeldung/lombok/builder/ClientBuilder.java new file mode 100644 index 0000000000..32c3d4b8ce --- /dev/null +++ b/lombok/src/main/java/com/baeldung/lombok/builder/ClientBuilder.java @@ -0,0 +1,11 @@ +package com.baeldung.lombok.builder; + +import lombok.Builder; + +class ClientBuilder { + + @Builder(builderMethodName = "builder") + public static ImmutableClient newClient(int id, String name) { + return new ImmutableClient(id, name); + } +} diff --git a/lombok/src/main/java/com/baeldung/lombok/builder/ImmutableClient.java b/lombok/src/main/java/com/baeldung/lombok/builder/ImmutableClient.java new file mode 100644 index 0000000000..48782583e2 --- /dev/null +++ b/lombok/src/main/java/com/baeldung/lombok/builder/ImmutableClient.java @@ -0,0 +1,11 @@ +package com.baeldung.lombok.builder; + +import lombok.Value; + +@Value +final class ImmutableClient { + + private int id; + private String name; + +} diff --git a/lombok/src/main/java/com/baeldung/lombok/builder/Widget.java b/lombok/src/main/java/com/baeldung/lombok/builder/Widget.java new file mode 100644 index 0000000000..dbb99f55a4 --- /dev/null +++ b/lombok/src/main/java/com/baeldung/lombok/builder/Widget.java @@ -0,0 +1,13 @@ +package com.baeldung.lombok.builder; + +import lombok.Builder; +import lombok.Getter; + +@Getter +@Builder(toBuilder = true) +public class Widget { + + private final String name; + private final int id; + +} diff --git a/lombok/src/test/java/com/baeldung/lombok/builder/BuilderUnitTest.java b/lombok/src/test/java/com/baeldung/lombok/builder/BuilderUnitTest.java new file mode 100644 index 0000000000..acad7d6c04 --- /dev/null +++ b/lombok/src/test/java/com/baeldung/lombok/builder/BuilderUnitTest.java @@ -0,0 +1,42 @@ +package com.baeldung.lombok.builder; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.*; + +public class BuilderUnitTest +{ + + @Test + public void givenBuilder_WidgetIsBuilt() { + Widget testWidget = Widget.builder().name("foo").id(1).build(); + assertThat(testWidget.getName()) + .isEqualTo("foo"); + assertThat(testWidget.getId()) + .isEqualTo(1); + } + + @Test + public void givenToBuilder_whenToBuilder_BuilderIsCreated() { + + Widget testWidget = Widget.builder().name("foo").id(1).build(); + Widget.WidgetBuilder widgetBuilder = testWidget.toBuilder(); + + Widget newWidget = widgetBuilder.id(2).build(); + assertThat(newWidget.getName()) + .isEqualTo("foo"); + assertThat(newWidget.getId()) + .isEqualTo(2); + } + + + + @Test + public void givenBuilderMethod_ClientIsBuilt() { + ImmutableClient testImmutableClient = ClientBuilder.builder().name("foo").id(1).build(); + assertThat(testImmutableClient.getName()) + .isEqualTo("foo"); + assertThat(testImmutableClient.getId()) + .isEqualTo(1); + } +}