org.testng.Assert.assertThrows()方法的使用及代码示例

x33g5p2x  于2022-01-15 转载在 其他  
字(12.2k)|赞(0)|评价(0)|浏览(938)

本文整理了Java中org.testng.Assert.assertThrows()方法的一些代码示例,展示了Assert.assertThrows()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Assert.assertThrows()方法的具体详情如下:
包路径:org.testng.Assert
类名称:Assert
方法名:assertThrows

Assert.assertThrows介绍

[英]Asserts that runnable throws an exception of type throwableClass when executed. If it does not throw an exception, an AssertionError is thrown. If it throws the wrong type of exception, an AssertionError is thrown describing the mismatch; the exception that was actually thrown can be obtained by calling AssertionError#getCause.
[中]断言runnable在执行时引发throwableClass类型的异常。如果它没有抛出异常,则抛出AssertionError。如果抛出错误类型的异常,将抛出一个AssertionError来描述不匹配;实际引发的异常可以通过调用AssertionError#getCause获得。

代码示例

代码示例来源:origin: org.testng/testng

/**
 * Asserts that {@code runnable} throws an exception when invoked. If it does not, an
 * {@link AssertionError} is thrown.
 *
 * @param runnable A function that is expected to throw an exception when invoked
 * @since 6.9.5
 */
public static void assertThrows(ThrowingRunnable runnable) {
 assertThrows(Throwable.class, runnable);
}

代码示例来源:origin: prestodb/presto

private static void assertDenied(ThrowingRunnable runnable)
  {
    assertThrows(AccessDeniedException.class, runnable);
  }
}

代码示例来源:origin: prestodb/presto

@Test
public void testNaN()
{
  assertThrows(() -> new DoubleStatistics(0.0, NaN));
  assertThrows(() -> new DoubleStatistics(NaN, 0.0));
  assertThrows(() -> new DoubleStatistics(NaN, NaN));
}

代码示例来源:origin: prestodb/presto

@Test
public void testInvalidLike()
{
  assertThrows(PrestoException.class, () -> optimize("unbound_string LIKE 'abc' ESCAPE ''"));
  assertThrows(PrestoException.class, () -> optimize("unbound_string LIKE 'abc' ESCAPE 'bc'"));
  assertThrows(PrestoException.class, () -> optimize("unbound_string LIKE '#' ESCAPE '#'"));
  assertThrows(PrestoException.class, () -> optimize("unbound_string LIKE '#abc' ESCAPE '#'"));
  assertThrows(PrestoException.class, () -> optimize("unbound_string LIKE 'ab#' ESCAPE '#'"));
}

代码示例来源:origin: prestodb/presto

@Test
  public void testCheckValidColumnName()
  {
    checkValidName("abc01_def2");
    assertThrows(() -> checkValidName(null));
    assertThrows(() -> checkValidName(""));
    assertThrows(() -> checkValidName("Abc"));
    assertThrows(() -> checkValidName("0abc"));
    assertThrows(() -> checkValidName("_abc"));
    assertThrows(() -> checkValidName("aBc"));
    assertThrows(() -> checkValidName("ab-c"));
  }
}

代码示例来源:origin: prestodb/presto

@Test
public void testDictionaryBlockWithFailure()
{
  assertThrows(NegativeValueException.class, () -> testFilter(createDictionaryBlockWithFailure(20, 100), LongArrayBlock.class));
}

代码示例来源:origin: prestodb/presto

private static void testProjectFails(Block block, Class<? extends Block> expectedResultType, boolean forceYield)
{
  assertThrows(NegativeValueException.class, () -> testProjectRange(block, expectedResultType, createProjection(), forceYield));
  assertThrows(NegativeValueException.class, () -> testProjectList(block, expectedResultType, createProjection(), forceYield));
  assertThrows(NegativeValueException.class, () -> testProjectRange(lazyWrapper(block), expectedResultType, createProjection(), forceYield));
  assertThrows(NegativeValueException.class, () -> testProjectList(lazyWrapper(block), expectedResultType, createProjection(), forceYield));
}

代码示例来源:origin: prestodb/presto

@Test
public void testInvalidLikePattern()
{
  assertThrows(PrestoException.class, () -> likePattern(utf8Slice("#"), utf8Slice("#")));
  assertThrows(PrestoException.class, () -> likePattern(utf8Slice("abc#abc"), utf8Slice("#")));
  assertThrows(PrestoException.class, () -> likePattern(utf8Slice("abc#"), utf8Slice("#")));
}

代码示例来源:origin: prestodb/presto

@Test
public void testNoHangIfPartitionIsOffline()
    throws Exception
{
  BackgroundHiveSplitLoader backgroundHiveSplitLoader = backgroundHiveSplitLoaderOfflinePartitions();
  HiveSplitSource hiveSplitSource = hiveSplitSource(backgroundHiveSplitLoader, TupleDomain.all());
  backgroundHiveSplitLoader.start(hiveSplitSource);
  assertThrows(RuntimeException.class, () -> drain(hiveSplitSource));
  assertThrows(RuntimeException.class, () -> hiveSplitSource.isFinished());
}

代码示例来源:origin: prestodb/presto

private static void assertShortValueReadFails(BigInteger value)
{
  assertThrows(OrcCorruptionException.class, () -> {
    DecimalInputStream stream = new DecimalInputStream(decimalInputStream(value));
    stream.nextLong();
  });
}

代码示例来源:origin: prestodb/presto

@Test
public void testRleBlockWithFailure()
{
  DictionaryAwarePageFilter filter = createDictionaryAwarePageFilter(true, LongArrayBlock.class);
  RunLengthEncodedBlock fail = new RunLengthEncodedBlock(createLongSequenceBlock(-10, -9), 100);
  assertThrows(NegativeValueException.class, () -> testFilter(filter, fail, true));
}

代码示例来源:origin: apache/incubator-gobblin

@Test
public void testConfigureFromConfig() {
 final Config config = ConfigFactory.empty()
   .withValue(HttpClientConfiguratorLoader.HTTP_CLIENT_CONFIGURATOR_TYPE_KEY,
         ConfigValueFactory.fromAnyRef("blah"));
 Assert.assertThrows(new ThrowingRunnable() {
  @Override public void run() throws Throwable {
   new HttpClientConfiguratorLoader(config);
  }
 });
}

代码示例来源:origin: prestodb/presto

@Test
public void testUnknownCost()
{
  CostComparator costComparator = new CostComparator(1.0, 1.0, 1.0);
  Session session = testSessionBuilder().build();
  assertThrows(IllegalArgumentException.class, () -> costComparator.compare(session, PlanNodeCostEstimate.zero(), PlanNodeCostEstimate.unknown()));
  assertThrows(IllegalArgumentException.class, () -> costComparator.compare(session, PlanNodeCostEstimate.unknown(), PlanNodeCostEstimate.zero()));
  assertThrows(IllegalArgumentException.class, () -> costComparator.compare(session, PlanNodeCostEstimate.unknown(), PlanNodeCostEstimate.unknown()));
}

代码示例来源:origin: prestodb/presto

@Test
  public void testCursor()
  {
    ListBasedRecordSet recordSet = new ListBasedRecordSet(
        ImmutableList.of(
            Arrays.asList("1", null, "3"),
            Arrays.asList("ab", "c", null)),
        ImmutableList.of(BIGINT, VARCHAR));
    assertEquals(recordSet.getColumnTypes(), ImmutableList.of(BIGINT, VARCHAR));
    RecordCursor cursor = recordSet.cursor();
    assertTrue(cursor.advanceNextPosition());
    assertEquals(cursor.getType(0), BIGINT);
    assertEquals(cursor.getType(1), VARCHAR);
    assertThrows(IndexOutOfBoundsException.class, () -> cursor.getLong(2));
    assertEquals(cursor.getLong(0), 1L);
    assertEquals(cursor.getSlice(1), Slices.utf8Slice("ab"));
    assertTrue(cursor.advanceNextPosition());
    assertTrue(cursor.isNull(0));
    assertEquals(cursor.getSlice(1), Slices.utf8Slice("c"));
    assertTrue(cursor.advanceNextPosition());
    assertEquals(cursor.getLong(0), 3L);
    assertTrue(cursor.isNull(1));
    assertFalse(cursor.advanceNextPosition());
    assertThrows(IndexOutOfBoundsException.class, () -> cursor.getLong(0));
  }
}

代码示例来源:origin: prestodb/presto

private static void assertLongValueReadFails(BigInteger value)
{
  Slice decimal = unscaledDecimal();
  assertThrows(OrcCorruptionException.class, () -> {
    DecimalInputStream stream = new DecimalInputStream(decimalInputStream(value));
    stream.nextLongDecimal(decimal);
  });
}

代码示例来源:origin: prestodb/presto

protected void assertMinMax(T min, T max)
{
  assertMinMaxStatistics(min, min);
  assertMinMaxStatistics(max, max);
  assertMinMaxStatistics(min, max);
  assertMinMaxStatistics(min, null);
  assertMinMaxStatistics(null, max);
  if (!min.equals(max)) {
    assertThrows(() -> getCreateStatistics(max, min));
  }
}

代码示例来源:origin: prestodb/presto

@Test
public void testIsLikePattern()
{
  assertFalse(isLikePattern(utf8Slice("abc"), null));
  assertFalse(isLikePattern(utf8Slice("abc#_def"), utf8Slice("#")));
  assertFalse(isLikePattern(utf8Slice("abc##def"), utf8Slice("#")));
  assertFalse(isLikePattern(utf8Slice("abc#%def"), utf8Slice("#")));
  assertTrue(isLikePattern(utf8Slice("abc%def"), null));
  assertTrue(isLikePattern(utf8Slice("abcdef_"), null));
  assertTrue(isLikePattern(utf8Slice("abcdef##_"), utf8Slice("#")));
  assertTrue(isLikePattern(utf8Slice("%abcdef#_"), utf8Slice("#")));
  assertThrows(PrestoException.class, () -> isLikePattern(utf8Slice("#"), utf8Slice("#")));
  assertThrows(PrestoException.class, () -> isLikePattern(utf8Slice("abc#abc"), utf8Slice("#")));
  assertThrows(PrestoException.class, () -> isLikePattern(utf8Slice("abc#"), utf8Slice("#")));
}

代码示例来源:origin: prestodb/presto

@Test
public void testSchemaOperations()
{
  TransactionManager transactionManager = createTestTransactionManager();
  AccessControlManager accessControlManager = newAccessControlManager(transactionManager, "catalog.json");
  transaction(transactionManager, accessControlManager)
      .execute(transactionId -> {
        Set<String> aliceSchemas = ImmutableSet.of("schema");
        assertEquals(accessControlManager.filterSchemas(transactionId, alice, "alice-catalog", aliceSchemas), aliceSchemas);
        assertEquals(accessControlManager.filterSchemas(transactionId, bob, "alice-catalog", aliceSchemas), ImmutableSet.of());
        accessControlManager.checkCanCreateSchema(transactionId, alice, aliceSchema);
        accessControlManager.checkCanDropSchema(transactionId, alice, aliceSchema);
        accessControlManager.checkCanRenameSchema(transactionId, alice, aliceSchema, "new-schema");
        accessControlManager.checkCanShowSchemas(transactionId, alice, "alice-catalog");
      });
  assertThrows(AccessDeniedException.class, () -> transaction(transactionManager, accessControlManager).execute(transactionId -> {
    accessControlManager.checkCanCreateSchema(transactionId, bob, aliceSchema);
  }));
}

代码示例来源:origin: prestodb/presto

@Test
public void testTableOperations()
{
  TransactionManager transactionManager = createTestTransactionManager();
  AccessControlManager accessControlManager = newAccessControlManager(transactionManager, "catalog.json");
  transaction(transactionManager, accessControlManager)
      .execute(transactionId -> {
        Set<SchemaTableName> aliceTables = ImmutableSet.of(new SchemaTableName("schema", "table"));
        assertEquals(accessControlManager.filterTables(transactionId, alice, "alice-catalog", aliceTables), aliceTables);
        assertEquals(accessControlManager.filterTables(transactionId, bob, "alice-catalog", aliceTables), ImmutableSet.of());
        accessControlManager.checkCanCreateTable(transactionId, alice, aliceTable);
        accessControlManager.checkCanDropTable(transactionId, alice, aliceTable);
        accessControlManager.checkCanSelectFromColumns(transactionId, alice, aliceTable, ImmutableSet.of());
        accessControlManager.checkCanInsertIntoTable(transactionId, alice, aliceTable);
        accessControlManager.checkCanDeleteFromTable(transactionId, alice, aliceTable);
        accessControlManager.checkCanAddColumns(transactionId, alice, aliceTable);
        accessControlManager.checkCanRenameColumn(transactionId, alice, aliceTable);
      });
  assertThrows(AccessDeniedException.class, () -> transaction(transactionManager, accessControlManager).execute(transactionId -> {
    accessControlManager.checkCanCreateTable(transactionId, bob, aliceTable);
  }));
}

代码示例来源:origin: prestodb/presto

@Test
public void testViewOperations()
{
  TransactionManager transactionManager = createTestTransactionManager();
  AccessControlManager accessControlManager = newAccessControlManager(transactionManager, "catalog.json");
  transaction(transactionManager, accessControlManager)
      .execute(transactionId -> {
        accessControlManager.checkCanCreateView(transactionId, alice, aliceView);
        accessControlManager.checkCanDropView(transactionId, alice, aliceView);
        accessControlManager.checkCanSelectFromColumns(transactionId, alice, aliceView, ImmutableSet.of());
        accessControlManager.checkCanCreateViewWithSelectFromColumns(transactionId, alice, aliceTable, ImmutableSet.of());
        accessControlManager.checkCanCreateViewWithSelectFromColumns(transactionId, alice, aliceView, ImmutableSet.of());
        accessControlManager.checkCanSetCatalogSessionProperty(transactionId, alice, "alice-catalog", "property");
        accessControlManager.checkCanGrantTablePrivilege(transactionId, alice, SELECT, aliceTable, "grantee", true);
        accessControlManager.checkCanRevokeTablePrivilege(transactionId, alice, SELECT, aliceTable, "revokee", true);
      });
  assertThrows(AccessDeniedException.class, () -> transaction(transactionManager, accessControlManager).execute(transactionId -> {
    accessControlManager.checkCanCreateView(transactionId, bob, aliceView);
  }));
}

相关文章