Skip to content

Commit 541faa3

Browse files
LiamGveLiam Garvie
andauthored
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>
1 parent a8890eb commit 541faa3

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.java9.interfaces;
2+
3+
public class CustomFoo implements Foo {
4+
5+
public static void main(String... args) {
6+
Foo customFoo = new CustomFoo();
7+
customFoo.bar(); // 'Hello world!'
8+
Foo.buzz(); // 'Hello static world!'
9+
}
10+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.java9.interfaces;
2+
3+
public interface Foo {
4+
5+
public default void bar() {
6+
System.out.print("Hello");
7+
baz();
8+
}
9+
10+
public static void buzz() {
11+
System.out.print("Hello");
12+
staticBaz();
13+
}
14+
15+
private void baz() {
16+
System.out.print(" world!");
17+
}
18+
19+
private static void staticBaz() {
20+
System.out.print(" static world!");
21+
}
22+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.java9.interfaces;
2+
3+
import org.junit.jupiter.api.AfterEach;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.io.ByteArrayOutputStream;
8+
import java.io.PrintStream;
9+
10+
import static org.assertj.core.api.Assertions.assertThat;
11+
12+
class CustomFooUnitTest {
13+
private ByteArrayOutputStream outContent = new ByteArrayOutputStream();
14+
private PrintStream originalOut = System.out;
15+
16+
@BeforeEach
17+
void setup() {
18+
System.setOut(new PrintStream(outContent));
19+
}
20+
21+
@AfterEach
22+
void tearDown() {
23+
System.setOut(originalOut);
24+
}
25+
26+
@Test
27+
void givenACustomFooObject_whenCallingDefaultMethodBar_thenExpectedStringIsWrittenToSystemOut() {
28+
CustomFoo customFoo = new CustomFoo();
29+
customFoo.bar();
30+
assertThat(outContent.toString()).isEqualTo("Hello world!");
31+
}
32+
33+
@Test
34+
void givenAFooInterface_whenCallingStaticMethodBuzz_thenExpectedStringIsWrittenToSystemOut() {
35+
Foo.buzz();
36+
assertThat(outContent.toString()).isEqualTo("Hello static world!");
37+
}
38+
}

0 commit comments

Comments
 (0)