本文整理了Java中org.junit.Assert.assertThrows()
方法的一些代码示例,展示了Assert.assertThrows()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Assert.assertThrows()
方法的具体详情如下:
包路径:org.junit.Assert
类名称:Assert
方法名:assertThrows
[英]Asserts that runnable throws an exception of type expectedThrowable when executed. If it does, the exception object is returned. 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在执行时引发expectedThrowable类型的异常。如果是,则返回异常对象。如果它没有抛出异常,则抛出AssertionError。如果抛出错误类型的异常,将抛出一个AssertionError来描述不匹配;实际引发的异常可以通过调用AssertionError#getCause获得。
代码示例来源:origin: junit-team/junit4
/**
* Asserts that {@code runnable} throws an exception of type {@code expectedThrowable} when
* executed. If it does, the exception object is returned. 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 expectedThrowable the expected type of the exception
* @param runnable a function that is expected to throw an exception when executed
* @return the exception thrown by {@code runnable}
* @since 4.13
*/
public static <T extends Throwable> T assertThrows(Class<T> expectedThrowable,
ThrowingRunnable runnable) {
return assertThrows(null, expectedThrowable, runnable);
}
代码示例来源:origin: junit-team/junit4
/**
* Adds a failure to the table if {@code runnable} does not throw an
* exception of type {@code expectedThrowable} when executed.
* Execution continues, but the test will fail at the end if the runnable
* does not throw an exception, or if it throws a different exception.
*
* @param expectedThrowable the expected type of the exception
* @param runnable a function that is expected to throw an exception when executed
* @since 4.13
*/
public void checkThrows(Class<? extends Throwable> expectedThrowable, ThrowingRunnable runnable) {
try {
assertThrows(expectedThrowable, runnable);
} catch (AssertionError e) {
addError(e);
}
}
代码示例来源:origin: bumptech/glide
@Test
public void write_throwsIfLengthAndOffsetsIsGreaterThanLength() {
assertThrows(IndexOutOfBoundsException.class, new ThrowingRunnable() {
@Override
public void run() throws Throwable {
os.write(new byte[1], /*initialOffset=*/ 1, /*length=*/ 1);
}
});
}
代码示例来源:origin: bumptech/glide
@Test
public void write_throwsIfLengthIsLessThanZero() {
assertThrows(IndexOutOfBoundsException.class, new ThrowingRunnable() {
@Override
public void run() throws Throwable {
os.write(new byte[0], /*initialOffset=*/ 0, /*length=*/ -1);
}
});
}
代码示例来源:origin: bumptech/glide
@Test
public void write_throwsIfOffsetIsGreaterThanLength() {
assertThrows(IndexOutOfBoundsException.class, new ThrowingRunnable() {
@Override
public void run() throws Throwable {
os.write(new byte[0], /*initialOffset=*/ 1, /*length=*/ 0);
}
});
}
代码示例来源:origin: bumptech/glide
@Test
public void write_throwsIfOffsetIsLessThanZero() {
assertThrows(IndexOutOfBoundsException.class, new ThrowingRunnable() {
@Override
public void run() throws Throwable {
os.write(new byte[0], /*initialOffset=*/ -1, /*length=*/ 0);
}
});
}
代码示例来源:origin: bumptech/glide
@Test
public void write_throwsIfLengthsIsGreaterThanLength() {
assertThrows(IndexOutOfBoundsException.class, new ThrowingRunnable() {
@Override
public void run() throws Throwable {
os.write(new byte[0], /*initialOffset=*/ 0, /*length=*/ 1);
}
});
}
代码示例来源:origin: bumptech/glide
@Test
public void load_withShapeDrawableResourceId_asDrawable_withTransformation_sizeOriginal_fails()
throws ExecutionException, InterruptedException {
assertThrows(
ExecutionException.class,
new ThrowingRunnable() {
@Override
public void run() throws Throwable {
Glide.with(context)
.load(ResourceIds.drawable.shape_drawable)
.apply(centerCropTransform())
.submit()
.get();
}
});
}
代码示例来源:origin: bumptech/glide
@Test
public void load_withShapeDrawableResourceId_asBitmap_withSizeOriginal_fails()
throws ExecutionException, InterruptedException {
assertThrows(
ExecutionException.class,
new ThrowingRunnable() {
@Override
public void run() throws Throwable {
Glide.with(context)
.asBitmap()
.load(ResourceIds.drawable.shape_drawable)
.submit()
.get();
}
});
}
代码示例来源:origin: bumptech/glide
@Test
public void load_withColorDrawable_sizeOriginal_requiredTransform_fails()
throws ExecutionException, InterruptedException {
final Drawable colorDrawable = new ColorDrawable(Color.RED);
assertThrows(
ExecutionException.class,
new ThrowingRunnable() {
@Override
public void run() throws Throwable {
Glide.with(context)
.load(colorDrawable)
.apply(new RequestOptions()
.centerCrop())
.submit()
.get();
}
});
}
代码示例来源:origin: bumptech/glide
@Test
public void testBuild_withModelAndDataClasses_excludesModelLoadersForOtherDataClasses() {
multiFactory.append(String.class, String.class, firstFactory);
assertThrows(
NoModelLoaderAvailableException.class,
new ThrowingRunnable() {
@Override
public void run() throws Throwable {
multiFactory.build(String.class, Integer.class);
}
});
}
代码示例来源:origin: bumptech/glide
@Test
public void testBuild_withModelAndDataClass_doesNotMatchSubclassesOfModelClass() {
appendFactoryFor(String.class, Object.class);
assertThrows(
NoModelLoaderAvailableException.class,
new ThrowingRunnable() {
@Override
public void run() throws Throwable {
multiFactory.build(Object.class, Object.class);
}
});
}
代码示例来源:origin: bumptech/glide
@Test
public void testBuild_withModelAndDataClass_doesNotMatchSubclassesOfDataClass() {
appendFactoryFor(Object.class, String.class);
assertThrows(
NoModelLoaderAvailableException.class,
new ThrowingRunnable() {
@Override
public void run() throws Throwable {
multiFactory.build(Object.class, Object.class);
}
});
}
代码示例来源:origin: bumptech/glide
@Test
public void testBuild_withModelAndDataClasses_excludesModelLoadersForOtherModelClasses() {
multiFactory.append(String.class, String.class, firstFactory);
assertThrows(
NoModelLoaderAvailableException.class,
new ThrowingRunnable() {
@Override
public void run() throws Throwable {
multiFactory.build(Integer.class, String.class);
}
});
}
代码示例来源:origin: bumptech/glide
assertThrows(
RuntimeException.class,
new ThrowingRunnable() {
代码示例来源:origin: bumptech/glide
@Test
public void updateDiskCacheKey_throwsException() throws NoSuchAlgorithmException {
// If this test fails, update testEqualsAndHashcode to use KeyTester including regression tests.
final EngineKey key = new EngineKey(
"id",
new ObjectKey("signature"),
100,
100,
Collections.<Class<?>, Transformation<?>>emptyMap(),
Object.class,
Object.class,
new Options());
assertThrows(
UnsupportedOperationException.class,
new ThrowingRunnable() {
@Override
public void run() throws NoSuchAlgorithmException {
key.updateDiskCacheKey(MessageDigest.getInstance("SHA-1"));
}
});
}
代码示例来源:origin: bumptech/glide
@Test
public void fitCenter_withHugeRectangle_throwsOOM()
throws ExecutionException, InterruptedException {
float multiplier = Integer.MAX_VALUE / (canonical.getWidth() * canonical.getHeight() * 2);
final int overrideWidth = (int) multiplier * canonical.getWidth();
final int overrideHeight = (int) multiplier * canonical.getHeight();
assertThrows(
ExecutionException.class,
new ThrowingRunnable() {
@Override
public void run() throws Throwable {
GlideApp
.with(context)
.asBitmap()
.load(canonical.getBitmap())
.fitCenter()
.override(overrideWidth, overrideHeight)
.submit()
.get();
}
});
}
}
代码示例来源:origin: GoogleCloudPlatform/bigdata-interop
@Test
public void testConstructorNullCredential() {
assertThrows(
NullPointerException.class, () -> new RetryHttpInitializer(null, "foo-user-agent"));
}
代码示例来源:origin: GoogleCloudPlatform/bigdata-interop
@Test
public void exceptionIsThrownForNoServiceAccountEmail()
throws IOException, GeneralSecurityException {
// No email set, keyfile doesn't exist, but that's OK.
configuration.setServiceAccountKeyFile("aFile");
assertThrows(IllegalStateException.class, () -> configuration.getCredential(TEST_SCOPES));
}
代码示例来源:origin: GoogleCloudPlatform/bigdata-interop
@Test
public void testValidCallHasNoRetries() throws Exception {
IllegalArgumentException thrown =
assertThrows(
IllegalArgumentException.class, () -> new RetryBoundedBackOff(-7, new BackOffTester()));
assertThat(thrown)
.hasMessageThat()
.contains("Maximum number of retries must not be less than 0.");
}
内容来源于网络,如有侵权,请联系作者删除!