Update Print2DArraysUnitTest.java

This commit is contained in:
Neetika23 2023-12-28 20:47:47 +05:30 committed by GitHub
parent 5f0cc26b40
commit 8c9636c5c6
1 changed files with 11 additions and 8 deletions

View File

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