Update Print2DArraysUnitTest.java

This commit is contained in:
Neetika23 2023-12-28 20:22:23 +05:30 committed by GitHub
parent 03949adeed
commit 458c3493e6
1 changed files with 10 additions and 12 deletions

View File

@ -2,29 +2,27 @@ package com.baeldung.print2darrays;
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 uk.org.webcompere.systemstubs.jupiter.SystemStub;
import uk.org.webcompere.systemstubs.stream.SystemOut;
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;
@ExtendWith(SystemStubsExtension.class)
public class Print2DArraysUnitTest {
public class Print2DArrayTest {
@SystemStub
private SystemOut systemOut;
@Test
void whenPrint2dArrayUsingNestedLoop_thenLinesAreWrittenToSystemOut() {
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] + " ");
for (int j = 0; j < myArray[i].length; j++) {
System.out.print(myArray[i][j] + " ");
}
System.out.println();
System.out.print("\n");
}
String expectedOutput = "1 2 3 \n4 5 6 \n7 8 9 \n";
assertThat(systemOut.getLines()).containsExactly(expectedOutput);
@ -34,7 +32,7 @@ public class Print2DArraysUnitTest {
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 + " "));
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);
}
@ -50,7 +48,7 @@ public class Print2DArraysUnitTest {
public void whenPrint2dArrayUsingArrayToString_thenLinesAreWrittenToSystemOut() {
int[][] myArray = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
for (int[] row : myArray) {
System.out.println(Arrays.toString(row));
System.out.print(Arrays.toString(row) + "\n");
}
String expectedOutput = "[1, 2, 3]\n[4, 5, 6]\n[7, 8, 9]\n";
assertThat(systemOut.getLines()).containsExactly(expectedOutput);