Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

on:
push:
branches:
- main
pull_request:
name: java-pubsub integration-tests-against-emulator
jobs:
filter:
runs-on: ubuntu-latest
outputs:
library: ${{ steps.filter.outputs.library }}
steps:
- uses: actions/checkout@v6
- uses: dorny/paths-filter@v4
id: filter
with:
filters: |
library:
- 'java-pubsub/**'
integration-tests:
needs: filter
if: ${{ needs.filter.outputs.library == 'true' }}
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v6
- uses: actions/setup-java@v5
with:
distribution: temurin
java-version: 17
- name: Start Pub/Sub Emulator
run: |
docker run -d -p 8085:8085 --name pubsub-emulator gcr.io/google.com/cloudsdktool/cloud-sdk:567.0.0-emulators gcloud beta emulators pubsub start --project=abc --host-port=0.0.0.0:8085
echo "Waiting for emulator to start..."
sleep 10

- name: Running tests
run: .kokoro/build.sh
env:
JOB_TYPE: test
BUILD_SUBDIR: java-pubsub
PUBSUB_EMULATOR_HOST: localhost:8085
GOOGLE_CLOUD_PROJECT: abc
SUREFIRE_JVM_OPT: '-Dtest=UsePubSubEmulatorExampleIT -Dsurefire.failIfNoSpecifiedTests=false'
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2026 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package pubsub;

import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class UsePubSubEmulatorExampleIT {
private ByteArrayOutputStream bout;
private PrintStream out;
private PrintStream originalOut;

private static void requireEnvVar(String varName) {
assertWithMessage("Environment variable " + varName + " is required to perform these tests.")
.that(System.getenv(varName))
.isNotNull();
}

@BeforeClass
public static void checkRequirements() {
requireEnvVar("PUBSUB_EMULATOR_HOST");
}

@Before
public void setUp() {
originalOut = System.out;
bout = new ByteArrayOutputStream();
out = new PrintStream(bout);
System.setOut(out);
}

@After
public void tearDown() {
System.setOut(originalOut);
}
Comment on lines +30 to +56
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Setting System.out to null in the tearDown method will cause a NullPointerException for any subsequent code (including other tests in the same JVM) that attempts to use the standard output stream. It is best practice to capture the original PrintStream in the setUp method and restore it in the tearDown method. Additionally, the requireEnvVar method has been updated to use Truth assertions for consistency.

  private ByteArrayOutputStream bout;
  private PrintStream out;
  private PrintStream originalOut;

  private static void requireEnvVar(String varName) {
    assertWithMessage("Environment variable " + varName + " is required to perform these tests.")
        .that(System.getenv(varName))
        .isNotNull();
  }

  @BeforeClass
  public static void checkRequirements() {
    requireEnvVar("PUBSUB_EMULATOR_HOST");
  }

  @Before
  public void setUp() {
    bout = new ByteArrayOutputStream();
    out = new PrintStream(bout);
    originalOut = System.out;
    System.setOut(out);
  }

  @After
  public void tearDown() {
    System.setOut(originalOut);
  }


@Test
public void testUsePubSubEmulatorExample() throws Exception {
UsePubSubEmulatorExample.main();
String output = bout.toString();
assertThat(output).contains("Created topic: ");
assertThat(output).contains("Published message ID: ");
}
}
Loading