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

The following examples show how to use org.junit.Assert#assertThrows() . These examples are extracted from open source projects. 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 Project: j2cl   File: StacktraceAsserterTest.java    License: 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 2
Source Project: j2cl   File: StacktraceAsserterTest.java    License: 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
@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 4
@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 5
@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 6
@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 7
@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 8
Source Project: j2cl   File: StacktraceAsserterTest.java    License: 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 9
Source Project: j2cl   File: StacktraceAsserterTest.java    License: 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 Project: j2cl   File: StacktraceAsserterTest.java    License: 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 11
Source Project: okapi   File: OkapiTokenTest.java    License: 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 12
Source Project: beam   File: BufferingDoFnRunnerTest.java    License: 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 13
Source Project: beam   File: PubsubMessageToRowTest.java    License: 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 14
@Test
public void testLogRequestParsing_emptyJson() throws IOException {
  Assert.assertThrows(IOException.class, () -> LogResponse.fromJson(new StringReader("")));
}
 
Example 15
@Test
public void testLogRequestParsing_invalidJsonObjectIncomplete() throws IOException {
  Assert.assertThrows(
      IOException.class,
      () -> LogResponse.fromJson(new StringReader("{\"nextRequestWaitMillis\":")));
}
 
Example 16
@Test
public void testLogRequestParsing_invalidJsonObjectNotClosed() throws IOException {
  Assert.assertThrows(IOException.class, () -> LogResponse.fromJson(new StringReader("{")));
}
 
Example 17
@Test
public void testLogRequestParsing_invalidJsonObjectNotOpen() throws IOException {
  Assert.assertThrows(
      IOException.class,
      () -> LogResponse.fromJson(new StringReader("\"nextRequestWaitMillis\":3}")));
}
 
Example 18
Source Project: beam   File: WorkerStatusClientTest.java    License: 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 19
@Test
public void testBuildLogRequest_missingFields() {
  Assert.assertThrows(IllegalStateException.class, () -> LogRequest.builder().build());
  Assert.assertThrows(
      IllegalStateException.class, () -> LogRequest.builder().setRequestTimeMs(1000L).build());
}
 
Example 20
Source Project: Leanplum-Android-SDK   File: UtilTest.java    License: 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()));
}