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 <liamgarvie@Liams-MacBook-Pro.local>
This commit is contained in:
LiamGve 2021-06-09 17:31:16 +01:00 committed by GitHub
parent a8890ebdca
commit 541faa3a97
3 changed files with 70 additions and 0 deletions

View File

@ -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!'
}
}

View File

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

View File

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