Java Code Examples for org.junit.Assert#assertThrows()

The following examples show how to use org.junit.Assert#assertThrows() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultJobBundleFactoryTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void rejectsStateCachingWithLoadBalancing() throws Exception {
  PortablePipelineOptions portableOptions =
      PipelineOptionsFactory.as(PortablePipelineOptions.class);
  portableOptions.setLoadBalanceBundles(true);
  ExperimentalOptions options = portableOptions.as(ExperimentalOptions.class);
  ExperimentalOptions.addExperiment(options, "state_cache_size=1");
  Struct pipelineOptions = PipelineOptionsTranslation.toProto(options);

  Exception e =
      Assert.assertThrows(
          IllegalArgumentException.class,
          () ->
              new DefaultJobBundleFactory(
                      JobInfo.create("testJob", "testJob", "token", pipelineOptions),
                      envFactoryProviderMap,
                      stageIdGenerator,
                      serverInfo)
                  .close());
  Assert.assertThat(e.getMessage(), containsString("state_cache_size"));
}
 
Example 2
Source File: StacktraceAsserterTest.java    From j2cl with Apache License 2.0 6 votes vote down vote up
@Test
public void testSimpleOptionalFrame_twoFramesNotWorking() {

  Stacktrace expectedTrace =
      Stacktrace.newStacktraceBuilder()
          .message("java.lang.RuntimeException: __the_message__!")
          .addFrame("at A")
          .addOptionalFrame()
          .addFrame("at B")
          .addFrame("at C")
          .build();

  StacktraceAsserter stacktraceAsserter =
      new StacktraceAsserter(
          TestMode.JAVA,
          LogLineBuilder.builder()
              .addLine("java.lang.RuntimeException: __the_message__!")
              .addTrace("A")
              .addTrace("covered_by_optional")
              .addTrace("not_covered_by_optional")
              .addTrace("B")
              .addTrace("C")
              .build());

  Assert.assertThrows(AssertionError.class, () -> stacktraceAsserter.matches(expectedTrace));
}
 
Example 3
Source File: StacktraceAsserterTest.java    From j2cl with Apache License 2.0 6 votes vote down vote up
@Test
public void testSimpleStacktrace_notWorking() {
  Stacktrace expectedTrace =
      Stacktrace.newStacktraceBuilder()
          .message("java.lang.RuntimeException: __the_message__!")
          .addFrame("at A")
          .addFrame("at B")
          .addFrame("at C")
          .build();

  StacktraceAsserter stacktraceAsserter =
      new StacktraceAsserter(
          TestMode.JAVA,
          LogLineBuilder.builder()
              .addLine("java.lang.RuntimeException: __the_message__!")
              .addTrace("A")
              .addTrace("B")
              .addTrace("D")
              .build());

  Assert.assertThrows(AssertionError.class, () -> stacktraceAsserter.matches(expectedTrace));
}
 
Example 4
Source File: AssemblyFileSetUtilsProcessAssemblyFileSetTest.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void assemblyConfigurationHasNoTargetDirShouldThrowException() {
  // Given
  final AssemblyFileSet afs = AssemblyFileSet.builder()
      .directory(sourceDirectory)
      .build();
  final AssemblyConfiguration ac = AssemblyConfiguration.builder().build();
  // When
  final Exception result = Assert.assertThrows(NullPointerException.class, () ->
      AssemblyFileSetUtils.processAssemblyFileSet(baseDirectory, outputDirectory, afs, ac)
  );
  // Then
  assertThat(result.getMessage(), equalTo("Assembly Configuration target dir is required"));
}
 
Example 5
Source File: PubsubMessageToRowTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void testNestedSchemaMessageInvalidElement() {
  Schema payloadSchema = Schema.builder().addInt32Field("id").addStringField("name").build();

  Schema messageSchema =
      Schema.builder()
          .addDateTimeField("event_timestamp")
          .addMapField("attributes", VARCHAR, VARCHAR)
          .addRowField("payload", payloadSchema)
          .build();

  pipeline
      .apply(
          "create",
          Create.timestamped(
              message(1, map("attr", "val"), "{ \"id\" : 3, \"name\" : \"foo\" }"),
              message(2, map("attr1", "val1"), "{ \"invalid1\" : \"sdfsd\" }")))
      .apply(
          "convert",
          PubsubMessageToRow.builder()
              .messageSchema(messageSchema)
              .useDlq(false)
              .useFlatSchema(false)
              .build());

  Exception exception = Assert.assertThrows(RuntimeException.class, () -> pipeline.run());
  Assert.assertTrue(exception.getMessage().contains("Error parsing message"));
}
 
Example 6
Source File: BufferingDoFnRunnerTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void testRejectConcurrentCheckpointingBoundaries() {
  Assert.assertThrows(
      IllegalArgumentException.class,
      () -> {
        createBufferingDoFnRunner(0, Collections.emptyList());
      });
  Assert.assertThrows(
      IllegalArgumentException.class,
      () -> {
        createBufferingDoFnRunner(Short.MAX_VALUE, Collections.emptyList());
      });
}
 
Example 7
Source File: OkapiTokenTest.java    From okapi with Apache License 2.0 5 votes vote down vote up
private String exceptionMessage(String token) {
  OkapiToken okapiToken = new OkapiToken(token);
  Exception e = Assert.assertThrows(
      IllegalArgumentException.class,
      () -> okapiToken.getTenant());
  return e.getMessage();
}
 
Example 8
Source File: StacktraceAsserterTest.java    From j2cl with Apache License 2.0 5 votes vote down vote up
@Test
public void testOptionalAtEnd_threeUsed() {
  Stacktrace expectedTrace =
      Stacktrace.newStacktraceBuilder()
          .message("java.lang.RuntimeException: __the_message__!")
          .addFrame("at A")
          .addFrame("at B")
          .addFrame("at C")
          .addOptionalFrame()
          .addOptionalFrame()
          .build();

  StacktraceAsserter stacktraceAsserter =
      new StacktraceAsserter(
          TestMode.JAVA,
          LogLineBuilder.builder()
              .addLine("java.lang.RuntimeException: __the_message__!")
              .addTrace("A")
              .addTrace("B")
              .addTrace("C")
              .addTrace("covered_by_optional")
              .addTrace("covered_by_optional1")
              .addTrace("not_covered_by_optional")
              .build());

  Assert.assertThrows(AssertionError.class, () -> stacktraceAsserter.matches(expectedTrace));
}
 
Example 9
Source File: StacktraceAsserterTest.java    From j2cl with Apache License 2.0 5 votes vote down vote up
@Test
public void testOptionalAtStart_threeUsed() {
  Stacktrace expectedTrace =
      Stacktrace.newStacktraceBuilder()
          .message("java.lang.RuntimeException: __the_message__!")
          .addOptionalFrame()
          .addOptionalFrame()
          .addFrame("at A")
          .addFrame("at B")
          .addFrame("at C")
          .build();

  StacktraceAsserter stacktraceAsserter =
      new StacktraceAsserter(
          TestMode.JAVA,
          LogLineBuilder.builder()
              .addLine("java.lang.RuntimeException: __the_message__!")
              .addTrace("covered_by_optional")
              .addTrace("covered_by_optional1")
              .addTrace("not_covered_by_optional")
              .addTrace("A")
              .addTrace("B")
              .addTrace("C")
              .build());

  Assert.assertThrows(AssertionError.class, () -> stacktraceAsserter.matches(expectedTrace));
}
 
Example 10
Source File: StacktraceAsserterTest.java    From j2cl with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultipleOptionalFrames_threeFramesFourUsed() {
  Stacktrace expectedTrace =
      Stacktrace.newStacktraceBuilder()
          .message("java.lang.RuntimeException: __the_message__!")
          .addFrame("at A")
          .addOptionalFrame()
          .addOptionalFrame()
          .addOptionalFrame()
          .addFrame("at B")
          .addFrame("at C")
          .build();

  StacktraceAsserter stacktraceAsserter =
      new StacktraceAsserter(
          TestMode.JAVA,
          LogLineBuilder.builder()
              .addLine("java.lang.RuntimeException: __the_message__!")
              .addTrace("A")
              .addTrace("covered_by_optional")
              .addTrace("covered_by_optional1")
              .addTrace("covered_by_optional2")
              .addTrace("not_covered_by_optional")
              .addTrace("B")
              .addTrace("C")
              .build());

  Assert.assertThrows(AssertionError.class, () -> stacktraceAsserter.matches(expectedTrace));
}
 
Example 11
Source File: LogRequestTest.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
@Test
public void testBuildLogEvent_missingFields() {
  Assert.assertThrows(IllegalStateException.class, () -> LogEvent.jsonBuilder("").build());
  Assert.assertThrows(
      IllegalStateException.class,
      () -> LogEvent.protoBuilder(EMPTY_BYTE_ARRAY).setEventTimeMs(4500).build());
  Assert.assertThrows(
      IllegalStateException.class,
      () -> LogEvent.jsonBuilder("").setEventTimeMs(4500).setEventUptimeMs(10000).build());
}
 
Example 12
Source File: SchemaManagerTest.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
@Test
public void upgrade_toANonExistentVersion_fails() {
  int oldVersion = 1;
  int nonExistentVersion = 1000;
  SchemaManager schemaManager =
      new SchemaManager(ApplicationProvider.getApplicationContext(), DB_NAME, oldVersion);

  Assert.assertThrows(
      IllegalArgumentException.class,
      () ->
          schemaManager.onUpgrade(
              schemaManager.getWritableDatabase(), oldVersion, nonExistentVersion));
}
 
Example 13
Source File: AssemblyFileSetUtilsProcessAssemblyFileSetTest.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void assemblyFileSetHasNoDirectoryShouldThrowException() {
  // Given
  final AssemblyFileSet afs = AssemblyFileSet.builder().build();
  final AssemblyConfiguration ac = AssemblyConfiguration.builder()
      .name("deployments")
      .build();
  // When
  final Exception result = Assert.assertThrows(NullPointerException.class, () ->
    AssemblyFileSetUtils.processAssemblyFileSet(baseDirectory, outputDirectory, afs, ac)
  );
  // Then
  assertThat(result.getMessage(), equalTo("Assembly FileSet directory is required"));
}
 
Example 14
Source File: UtilTest.java    From Leanplum-Android-SDK with Apache License 2.0 4 votes vote down vote up
/**
 * Test that {@link Util#handleException(Throwable)} is successfully mocked to rethrow the
 * argument exception.
 */
@Test
public void testHandleExceptionMocked() {
  Assert.assertThrows(Throwable.class, () -> Util.handleException(new Exception()));
}
 
Example 15
Source File: LogRequestTest.java    From firebase-android-sdk with Apache License 2.0 4 votes vote down vote up
@Test
public void testBuildLogRequest_missingFields() {
  Assert.assertThrows(IllegalStateException.class, () -> LogRequest.builder().build());
  Assert.assertThrows(
      IllegalStateException.class, () -> LogRequest.builder().setRequestTimeMs(1000L).build());
}
 
Example 16
Source File: WorkerStatusClientTest.java    From beam with Apache License 2.0 4 votes vote down vote up
@Test
public void testCloseOutstandingRequest() throws IOException {
  CompletableFuture<WorkerStatusResponse> workerStatus = client.getWorkerStatus();
  client.close();
  Assert.assertThrows(ExecutionException.class, workerStatus::get);
}
 
Example 17
Source File: LogResponseTest.java    From firebase-android-sdk with Apache License 2.0 4 votes vote down vote up
@Test
public void testLogRequestParsing_invalidJsonObjectNotOpen() throws IOException {
  Assert.assertThrows(
      IOException.class,
      () -> LogResponse.fromJson(new StringReader("\"nextRequestWaitMillis\":3}")));
}
 
Example 18
Source File: LogResponseTest.java    From firebase-android-sdk with Apache License 2.0 4 votes vote down vote up
@Test
public void testLogRequestParsing_invalidJsonObjectNotClosed() throws IOException {
  Assert.assertThrows(IOException.class, () -> LogResponse.fromJson(new StringReader("{")));
}
 
Example 19
Source File: LogResponseTest.java    From firebase-android-sdk with Apache License 2.0 4 votes vote down vote up
@Test
public void testLogRequestParsing_invalidJsonObjectIncomplete() throws IOException {
  Assert.assertThrows(
      IOException.class,
      () -> LogResponse.fromJson(new StringReader("{\"nextRequestWaitMillis\":")));
}
 
Example 20
Source File: LogResponseTest.java    From firebase-android-sdk with Apache License 2.0 4 votes vote down vote up
@Test
public void testLogRequestParsing_emptyJson() throws IOException {
  Assert.assertThrows(IOException.class, () -> LogResponse.fromJson(new StringReader("")));
}