2828import com .google .cloud .storage .BucketInfo ;
2929import com .google .cloud .storage .Storage ;
3030import com .google .cloud .storage .StorageOptions ;
31+ import com .google .cloud .testing .junit4 .StdOutCaptureRule ;
3132import com .google .protobuf .Empty ;
32- import java .io .ByteArrayOutputStream ;
3333import java .io .IOException ;
34- import java .io .PrintStream ;
3534import java .util .UUID ;
3635import java .util .concurrent .ExecutionException ;
3736import org .hamcrest .CoreMatchers ;
3837import org .junit .After ;
3938import org .junit .Before ;
4039import org .junit .BeforeClass ;
40+ import org .junit .Rule ;
4141import org .junit .Test ;
4242import org .junit .runner .RunWith ;
4343import 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