Skip to content

Commit e37b86a

Browse files
authored
chore: update QuickstartTest to use StdOutCaptureRule (#376)
1 parent 807c130 commit e37b86a

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

dataproc/snippets/src/test/java/QuickstartTest.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@
2828
import com.google.cloud.storage.BucketInfo;
2929
import com.google.cloud.storage.Storage;
3030
import com.google.cloud.storage.StorageOptions;
31+
import com.google.cloud.testing.junit4.StdOutCaptureRule;
3132
import com.google.protobuf.Empty;
32-
import java.io.ByteArrayOutputStream;
3333
import java.io.IOException;
34-
import java.io.PrintStream;
3534
import java.util.UUID;
3635
import java.util.concurrent.ExecutionException;
3736
import org.hamcrest.CoreMatchers;
3837
import org.junit.After;
3938
import org.junit.Before;
4039
import org.junit.BeforeClass;
40+
import org.junit.Rule;
4141
import org.junit.Test;
4242
import org.junit.runner.RunWith;
4343
import org.junit.runners.JUnit4;
@@ -60,7 +60,8 @@ public class QuickstartTest {
6060
+ "rdd = sc.parallelize((1,2,3,4,5))\n"
6161
+ "sum = rdd.reduce(lambda x, y: x + y)\n";
6262

63-
private ByteArrayOutputStream bout;
63+
@Rule
64+
public StdOutCaptureRule stdOutCapture = new StdOutCaptureRule();
6465
private Bucket bucket;
6566
private Blob blob;
6667

@@ -78,9 +79,6 @@ public static void checkRequirements() {
7879

7980
@Before
8081
public void setUp() {
81-
bout = new ByteArrayOutputStream();
82-
System.setOut(new PrintStream(bout));
83-
8482
Storage storage = StorageOptions.getDefaultInstance().getService();
8583
bucket = storage.create(BucketInfo.of(BUCKET_NAME));
8684
blob = bucket.create(JOB_FILE_NAME, SORT_CODE.getBytes(UTF_8), "text/plain");
@@ -89,7 +87,7 @@ public void setUp() {
8987
@Test
9088
public void quickstartTest() throws IOException, InterruptedException {
9189
Quickstart.main(PROJECT_ID, REGION, CLUSTER_NAME, JOB_FILE_PATH);
92-
String output = bout.toString();
90+
String output = stdOutCapture.getCapturedOutputAsUtf8String();
9391

9492
assertThat(output, CoreMatchers.containsString("Cluster created successfully"));
9593
assertThat(output, CoreMatchers.containsString("Job finished successfully:"));

0 commit comments

Comments
 (0)