BAEL-7615 - Implement the Builder Pattern in Java 8 (#16399)

* BAEL-7255 - Implementing GraphQL Mutation without Returning Data

* BAEL-7615 - Implement the Builder Pattern in Java 8

* BAEL-7615 - Implement the Builder Pattern in Java 8
This commit is contained in:
Alexandru Borza
2024-04-13 23:50:11 +03:00
committed by GitHub
parent e975895695
commit 977071357e
5 changed files with 177 additions and 1 deletions
@@ -0,0 +1,50 @@
package com.baeldung.builder.implementation;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class BuilderImplementationUnitTest {
@Test
void givenClassicBuilder_whenBuild_thenReturnObject() {
Post post = new Post.Builder()
.title("Java Builder Pattern")
.text("Explaining how to implement the Builder Pattern in Java")
.category("Programming")
.build();
assertEquals("Java Builder Pattern", post.getTitle());
assertEquals("Explaining how to implement the Builder Pattern in Java", post.getText());
assertEquals("Programming", post.getCategory());
}
@Test
void givenGenericBuilder_whenBuild_thenReturnObject() {
Post post = GenericBuilder.of(Post::new)
.with(Post::setTitle, "Java Builder Pattern")
.with(Post::setText, "Explaining how to implement the Builder Pattern in Java")
.with(Post::setCategory, "Programming")
.build();
assertEquals("Java Builder Pattern", post.getTitle());
assertEquals("Explaining how to implement the Builder Pattern in Java", post.getText());
assertEquals("Programming", post.getCategory());
}
@Test
void givenLombokBuilder_whenBuild_thenReturnObject() {
LombokPost post = LombokPost.builder()
.title("Java Builder Pattern")
.text("Explaining how to implement the Builder Pattern in Java")
.category("Programming")
.build();
assertEquals("Java Builder Pattern", post.getTitle());
assertEquals("Explaining how to implement the Builder Pattern in Java", post.getText());
assertEquals("Programming", post.getCategory());
}
}