Update Print2DArraysUnitTest.java

This commit is contained in:
Neetika23 2023-12-28 20:47:47 +05:30 committed by GitHub
parent 5f0cc26b40
commit 8c9636c5c6

View File

@ -1,23 +1,25 @@
package com.baeldung.print2darrays; package com.baeldung.print2darrays;
import java.util.Arrays;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import uk.org.webcompere.systemstubs.jupiter.SystemStub; import uk.org.webcompere.systemstubs.jupiter.SystemStub;
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension; import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;
import uk.org.webcompere.systemstubs.stream.SystemOut; import uk.org.webcompere.systemstubs.stream.SystemOut;
import java.util.Arrays;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ExtendWith(SystemStubsExtension.class) @ExtendWith(SystemStubsExtension.class)
public class Print2DArraysUnitTest { public class Print2DArraysUnitTest {
@SystemStub @SystemStub
private SystemOut systemOut; private SystemOut systemOut;
@Test @Test
void whenPrint2dArrayUsingNestedLoop_thenLinesAreWrittenToSystemOut() { void whenPrint2dArrayUsingNestedLoop_thenLinesAreWrittenToSystemOut() {
int[][] myArray = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; int[][] myArray = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
for (int i = 0; i < myArray.length; i++) { for (int i = 0; i < myArray.length; i++) {
for (int j = 0; j < myArray[i].length; j++) { for (int j = 0; j < myArray[i].length; j++) {
System.out.print(myArray[i][j] + " "); System.out.print(myArray[i][j] + " ");
@ -30,15 +32,17 @@ public class Print2DArraysUnitTest {
@Test @Test
public void whenPrint2dArrayUsingStreams_thenLinesAreWrittenToSystemOut() { public void whenPrint2dArrayUsingStreams_thenLinesAreWrittenToSystemOut() {
int[][] myArray = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; int[][] myArray = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
Arrays.stream(myArray) .flatMapToInt(Arrays::stream) .forEach(num -> System.out.print(num + " ")); Arrays.stream(myArray)
.flatMapToInt(Arrays::stream)
.forEach(num -> System.out.print(num + " "));
String expectedOutput = "1 2 3 4 5 6 7 8 9 "; String expectedOutput = "1 2 3 4 5 6 7 8 9 ";
assertThat(systemOut.getLines()).containsExactly(expectedOutput); assertThat(systemOut.getLines()).containsExactly(expectedOutput);
} }
@Test @Test
public void whenPrint2dArrayUsingDeepToString_thenLinesAreWrittenToSystemOut() { public void whenPrint2dArrayUsingDeepToString_thenLinesAreWrittenToSystemOut() {
int[][] myArray = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; int[][] myArray = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
System.out.println(Arrays.deepToString(myArray)); System.out.println(Arrays.deepToString(myArray));
String expectedOutput = "[[1, 2, 3], [4, 5, 6], [7, 8, 9]]"; String expectedOutput = "[[1, 2, 3], [4, 5, 6], [7, 8, 9]]";
assertThat(systemOut.getLines()).containsExactly(expectedOutput); assertThat(systemOut.getLines()).containsExactly(expectedOutput);
@ -46,7 +50,7 @@ public class Print2DArraysUnitTest {
@Test @Test
public void whenPrint2dArrayUsingArrayToString_thenLinesAreWrittenToSystemOut() { public void whenPrint2dArrayUsingArrayToString_thenLinesAreWrittenToSystemOut() {
int[][] myArray = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; int[][] myArray = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
for (int[] row : myArray) { for (int[] row : myArray) {
System.out.print(Arrays.toString(row) + "\n"); System.out.print(Arrays.toString(row) + "\n");
} }
@ -54,4 +58,3 @@ public class Print2DArraysUnitTest {
assertThat(systemOut.getLines()).containsExactly(expectedOutput); assertThat(systemOut.getLines()).containsExactly(expectedOutput);
} }
} }