From 541faa3a9710cb370e551abb5dbfef6c364aff33 Mon Sep 17 00:00:00 2001 From: LiamGve Date: Wed, 9 Jun 2021 17:31:16 +0100 Subject: [PATCH] BAEL-4991 added in code for Java 9 private method introduction (#10884) * BAEL-4991 added in code for Java 9 private method introduction * BAEL-4991 added unit tests for java 9 private methods in interface article Co-authored-by: Liam Garvie --- .../baeldung/java9/interfaces/CustomFoo.java | 10 +++++ .../com/baeldung/java9/interfaces/Foo.java | 22 +++++++++++ .../java9/interfaces/CustomFooUnitTest.java | 38 +++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 core-java-modules/core-java-9/src/main/java/com/baeldung/java9/interfaces/CustomFoo.java create mode 100644 core-java-modules/core-java-9/src/main/java/com/baeldung/java9/interfaces/Foo.java create mode 100644 core-java-modules/core-java-9/src/test/java/com/baeldung/java9/interfaces/CustomFooUnitTest.java diff --git a/core-java-modules/core-java-9/src/main/java/com/baeldung/java9/interfaces/CustomFoo.java b/core-java-modules/core-java-9/src/main/java/com/baeldung/java9/interfaces/CustomFoo.java new file mode 100644 index 0000000000..388927fbfe --- /dev/null +++ b/core-java-modules/core-java-9/src/main/java/com/baeldung/java9/interfaces/CustomFoo.java @@ -0,0 +1,10 @@ +package com.baeldung.java9.interfaces; + +public class CustomFoo implements Foo { + + public static void main(String... args) { + Foo customFoo = new CustomFoo(); + customFoo.bar(); // 'Hello world!' + Foo.buzz(); // 'Hello static world!' + } +} diff --git a/core-java-modules/core-java-9/src/main/java/com/baeldung/java9/interfaces/Foo.java b/core-java-modules/core-java-9/src/main/java/com/baeldung/java9/interfaces/Foo.java new file mode 100644 index 0000000000..8ccee83b7e --- /dev/null +++ b/core-java-modules/core-java-9/src/main/java/com/baeldung/java9/interfaces/Foo.java @@ -0,0 +1,22 @@ +package com.baeldung.java9.interfaces; + +public interface Foo { + + public default void bar() { + System.out.print("Hello"); + baz(); + } + + public static void buzz() { + System.out.print("Hello"); + staticBaz(); + } + + private void baz() { + System.out.print(" world!"); + } + + private static void staticBaz() { + System.out.print(" static world!"); + } +} diff --git a/core-java-modules/core-java-9/src/test/java/com/baeldung/java9/interfaces/CustomFooUnitTest.java b/core-java-modules/core-java-9/src/test/java/com/baeldung/java9/interfaces/CustomFooUnitTest.java new file mode 100644 index 0000000000..d107c091fb --- /dev/null +++ b/core-java-modules/core-java-9/src/test/java/com/baeldung/java9/interfaces/CustomFooUnitTest.java @@ -0,0 +1,38 @@ +package com.baeldung.java9.interfaces; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +import static org.assertj.core.api.Assertions.assertThat; + +class CustomFooUnitTest { + private ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + private PrintStream originalOut = System.out; + + @BeforeEach + void setup() { + System.setOut(new PrintStream(outContent)); + } + + @AfterEach + void tearDown() { + System.setOut(originalOut); + } + + @Test + void givenACustomFooObject_whenCallingDefaultMethodBar_thenExpectedStringIsWrittenToSystemOut() { + CustomFoo customFoo = new CustomFoo(); + customFoo.bar(); + assertThat(outContent.toString()).isEqualTo("Hello world!"); + } + + @Test + void givenAFooInterface_whenCallingStaticMethodBuzz_thenExpectedStringIsWrittenToSystemOut() { + Foo.buzz(); + assertThat(outContent.toString()).isEqualTo("Hello static world!"); + } +} \ No newline at end of file