Update Print2DArraysTest.java

This commit is contained in:
Neetika23 2023-12-11 12:44:01 +05:30 committed by GitHub
parent f64c2b1689
commit f2e2c0b38c

View File

@ -1,22 +1,25 @@
package com.baeldung.print2DArrays; package com.baeldung.print2DArrays;
package com.st.tram.common;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Arrays;
import uk.org.webcompere.systemstubs.SystemOut; import uk.org.webcompere.systemstubs.SystemOut;
import uk.org.webcompere.systemstubs.annotations.SystemStub; import uk.org.webcompere.systemstubs.annotations.SystemStub;
public class ArrayPrinterTest { public class Print2DArrayTest {
@SystemStub @SystemStub
private SystemOut systemOut; private SystemOut systemOut;
@Test @Test
void whenPrint2D_thenUseNested() { void whenPrint2D_thenUseNested() {
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 < array.length; i++) { for (int i = 0; i < myArray.length; i++) {
for (int j = 0; j < array[i].length; j++) { for (int j = 0; j < myArray[i].length; j++) {
System.out.print(array[i][j] + " "); System.out.print(myArray[i][j] + " ");
} }
System.out.println(); System.out.println();
} }
@ -27,7 +30,7 @@ public class ArrayPrinterTest {
@Test @Test
public void whenPrint2D_thenUseStream() { public void whenPrint2D_thenUseStream() {
int[][] myArray = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; int[][] myArray = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
Arrays.stream(array) .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);
} }
@ -41,7 +44,7 @@ public class ArrayPrinterTest {
} }
@Test @Test
public void whenPrint2D_thenUseDeepToString() { public void whenPrint2D_thenUseArrayToString() {
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.println(Arrays.toString(row)); System.out.println(Arrays.toString(row));
@ -50,3 +53,4 @@ public class ArrayPrinterTest {
assertThat(systemOut.getLines()).containsExactly(expectedOutput); assertThat(systemOut.getLines()).containsExactly(expectedOutput);
} }
} }