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

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

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

Assert.expectThrows介绍

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

代码示例

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

/**
 * Asserts that {@code runnable} throws an exception of type {@code throwableClass} when
 * executed. If it does not throw an exception, an {@link AssertionError} is thrown. If it
 * throws the wrong type of exception, an {@code AssertionError} is thrown describing the
 * mismatch; the exception that was actually thrown can be obtained by calling {@link
 * AssertionError#getCause}.
 *
 * @param throwableClass the expected type of the exception
 * @param runnable       A function that is expected to throw an exception when invoked
 * @since 6.9.5
 */
@SuppressWarnings("ThrowableResultOfMethodCallIgnored")
public static <T extends Throwable> void assertThrows(Class<T> throwableClass, ThrowingRunnable runnable) {
 expectThrows(throwableClass, runnable);
}

代码示例来源:origin: cbeust/testng

/**
 * Asserts that {@code runnable} throws an exception of type {@code throwableClass} when executed.
 * If it does not throw an exception, an {@link AssertionError} is thrown. If it throws the wrong
 * type of exception, an {@code AssertionError} is thrown describing the mismatch; the exception
 * that was actually thrown can be obtained by calling {@link AssertionError#getCause}.
 *
 * @param throwableClass the expected type of the exception
 * @param runnable A function that is expected to throw an exception when invoked
 * @since 6.9.5
 */
@SuppressWarnings("ThrowableResultOfMethodCallIgnored")
public static <T extends Throwable> void assertThrows(
  Class<T> throwableClass, ThrowingRunnable runnable) {
 expectThrows(throwableClass, runnable);
}

代码示例来源:origin: cbeust/testng

@Test(expectedExceptions = AssertionError.class)
public void expectThrowsDetectsTypeMismatchesViaExplicitTypeHint() {
 NullPointerException npe = new NullPointerException();
 expectThrows(IOException.class, throwingRunnable(npe));
}

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

@Test
public void unknownCaptureIsAnError()
{
  Pattern<?> pattern = any();
  Capture<?> unknownCapture = newCapture();
  Match<?> match = DefaultMatcher.DEFAULT_MATCHER.match(pattern, 42);
  Throwable throwable = expectThrows(NoSuchElementException.class, () -> match.capture(unknownCapture));
  assertTrue(throwable.getMessage().contains("unknown Capture"));
}

代码示例来源:origin: cbeust/testng

@Test(expectedExceptions = AssertionError.class)
public void expectThrowsRequiresAnExceptionToBeThrown() {
 expectThrows(Throwable.class, nonThrowingRunnable());
}

代码示例来源:origin: cbeust/testng

@Test
public void expectThrowsReturnsTheSameObjectThrown() {
 NullPointerException npe = new NullPointerException();
 Throwable throwable = expectThrows(Throwable.class, throwingRunnable(npe));
 assertSame(npe, throwable);
}

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

@Test
public void noMatchMeansNoCaptures()
{
  Capture<Void> impossible = newCapture();
  Pattern<Void> pattern = typeOf(Void.class).capturedAs(impossible);
  Match<Void> match = DefaultMatcher.DEFAULT_MATCHER.match(pattern, 42);
  assertTrue(match.isEmpty());
  Throwable throwable = expectThrows(NoSuchElementException.class, () -> match.capture(impossible));
  assertTrue(throwable.getMessage().contains("Captures are undefined for an empty Match"));
}

代码示例来源:origin: cbeust/testng

@Test
public void expectThrowsIncludesAnInformativeDefaultMessage() {
 try {
  expectThrows(Throwable.class, nonThrowingRunnable());
 } catch (AssertionError ex) {
  assertEquals("Expected Throwable to be thrown, but nothing was thrown", ex.getMessage());
  return;
 }
 fail();
}

代码示例来源:origin: cbeust/testng

@Test
public void expectThrowsSuppliesACoherentErrorMessageUponTypeMismatch() {
 NullPointerException npe = new NullPointerException();
 try {
  expectThrows(IOException.class, throwingRunnable(npe));
 } catch (AssertionError error) {
  assertEquals("Expected IOException to be thrown, but NullPointerException was thrown",
      error.getMessage());
  assertSame(npe, error.getCause());
  return;
 }
 fail();
}

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

@Test
public void testRenameTable()
{
  SchemaTableName tableName = new SchemaTableName("test_schema", "test_table_to_be_renamed");
  metadata.createSchema(SESSION, "test_schema", ImmutableMap.of());
  ConnectorOutputTableHandle table = metadata.beginCreateTable(
      SESSION,
      new ConnectorTableMetadata(tableName, ImmutableList.of(), ImmutableMap.of()),
      Optional.empty());
  metadata.finishCreateTable(SESSION, table, ImmutableList.of(), ImmutableList.of());
  // rename table to schema which does not exist
  SchemaTableName invalidSchemaTableName = new SchemaTableName("test_schema_not_exist", "test_table_renamed");
  ConnectorTableHandle tableHandle = metadata.getTableHandle(SESSION, tableName);
  Throwable throwable = expectThrows(SchemaNotFoundException.class, () -> metadata.renameTable(SESSION, tableHandle, invalidSchemaTableName));
  assertTrue(throwable.getMessage().equals("Schema test_schema_not_exist not found"));
  // rename table to same schema
  SchemaTableName sameSchemaTableName = new SchemaTableName("test_schema", "test_renamed");
  metadata.renameTable(SESSION, metadata.getTableHandle(SESSION, tableName), sameSchemaTableName);
  assertEquals(metadata.listTables(SESSION, "test_schema"), ImmutableList.of(sameSchemaTableName));
  // rename table to different schema
  metadata.createSchema(SESSION, "test_different_schema", ImmutableMap.of());
  SchemaTableName differentSchemaTableName = new SchemaTableName("test_different_schema", "test_renamed");
  metadata.renameTable(SESSION, metadata.getTableHandle(SESSION, sameSchemaTableName), differentSchemaTableName);
  assertEquals(metadata.listTables(SESSION, "test_schema"), ImmutableList.of());
  assertEquals(metadata.listTables(SESSION, "test_different_schema"), ImmutableList.of(differentSchemaTableName));
}

代码示例来源:origin: cbeust/testng

@Test
public void expectThrowsWrapsAndPropagatesUnexpectedExceptions() {
 NullPointerException npe = new NullPointerException("inner-message");
 try {
  expectThrows(IOException.class, throwingRunnable(npe));
 } catch (AssertionError ex) {
  assertSame(npe, ex.getCause());
  assertEquals("inner-message", ex.getCause().getMessage());
  return;
 }
 fail();
}

代码示例来源:origin: bozaro/git-as-svn

@Test
void testInvalidPassword() {
 Assert.expectThrows(SVNAuthenticationException.class, () -> checkUser(administrator, "wrongpassword"));
}

代码示例来源:origin: bozaro/git-as-svn

@Test
void invalidUser() {
 Assert.expectThrows(SVNAuthenticationException.class, () -> checkUser("wronguser", rootPassword));
}

代码示例来源:origin: bozaro/git-as-svn

@Test
public void invalidPassword() {
 Assert.expectThrows(SVNAuthenticationException.class, () -> checkUser("ldapadmin", "wrongpassword"));
}

代码示例来源:origin: samtools/htsjdk

@Test
public void testTruncatedStream() throws Exception {
  byte[] compressed = Files.readAllBytes(BLOCK_COMPRESSED.toPath());
  byte[] truncated = Arrays.copyOf(compressed, compressed.length * 2 / 3);
  try (BlockCompressedInputStream stream = new BlockCompressedInputStream(new ByteArrayInputStream(truncated))) {
    Assert.expectThrows(FileTruncatedException.class, () -> InputStreamUtils.readFully(stream));
  }
}

代码示例来源:origin: com.facebook.presto/presto-matching

@Test
public void unknownCaptureIsAnError()
{
  Pattern<?> pattern = any();
  Capture<?> unknownCapture = newCapture();
  Match<?> match = DefaultMatcher.DEFAULT_MATCHER.match(pattern, 42);
  Throwable throwable = expectThrows(NoSuchElementException.class, () -> match.capture(unknownCapture));
  assertTrue(throwable.getMessage().contains("unknown Capture"));
}

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

@Test
public void unknownCaptureIsAnError()
{
  Pattern<?> pattern = any();
  Capture<?> unknownCapture = newCapture();
  Match<?> match = DefaultMatcher.DEFAULT_MATCHER.match(pattern, 42);
  Throwable throwable = expectThrows(NoSuchElementException.class, () -> match.capture(unknownCapture));
  assertTrue(throwable.getMessage().contains("unknown Capture"));
}

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

@Test
public void noMatchMeansNoCaptures()
{
  Capture<Void> impossible = newCapture();
  Pattern<Void> pattern = typeOf(Void.class).capturedAs(impossible);
  Match<Void> match = DefaultMatcher.DEFAULT_MATCHER.match(pattern, 42);
  assertTrue(match.isEmpty());
  Throwable throwable = expectThrows(NoSuchElementException.class, () -> match.capture(impossible));
  assertTrue(throwable.getMessage().contains("Captures are undefined for an empty Match"));
}

代码示例来源:origin: com.facebook.presto/presto-matching

@Test
public void noMatchMeansNoCaptures()
{
  Capture<Void> impossible = newCapture();
  Pattern<Void> pattern = typeOf(Void.class).capturedAs(impossible);
  Match<Void> match = DefaultMatcher.DEFAULT_MATCHER.match(pattern, 42);
  assertTrue(match.isEmpty());
  Throwable throwable = expectThrows(NoSuchElementException.class, () -> match.capture(impossible));
  assertTrue(throwable.getMessage().contains("Captures are undefined for an empty Match"));
}

代码示例来源:origin: Enigmatis/graphql-java-annotations

@Test
  public void testDuplicateField() {
    GraphQLAnnotations instance = new GraphQLAnnotations();
    GraphQLObjectHandler graphQLObjectHandler = instance.getObjectHandler();
    instance.registerTypeExtension(TestObjectExtensionInvalid.class);
    GraphQLAnnotationsException e = expectThrows(GraphQLAnnotationsException.class, () -> graphQLObjectHandler.getObject(TestObject.class,instance.getContainer()));
    assertTrue(e.getMessage().startsWith("Duplicate field"));
  }
}

相关文章