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.
This commit is contained in:
Eric Goebelbecker 2018-05-21 22:06:04 -04:00 committed by KevinGilmore
parent 7bba73c6eb
commit eceb217e7a
4 changed files with 77 additions and 0 deletions

View File

@ -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);
}
}

View File

@ -0,0 +1,11 @@
package com.baeldung.lombok.builder;
import lombok.Value;
@Value
final class ImmutableClient {
private int id;
private String name;
}

View File

@ -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;
}

View File

@ -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);
}
}