本文整理了Java中org.assertj.core.api.Assertions.assertThatCode()
方法的一些代码示例,展示了Assertions.assertThatCode()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Assertions.assertThatCode()
方法的具体详情如下:
包路径:org.assertj.core.api.Assertions
类名称:Assertions
方法名:assertThatCode
[英]Allows to capture and then assert on a Throwable (easier done with lambdas).
The main difference with assertThatThrownBy(ThrowingCallable) is that this method does not fail if no exception was thrown.
Example :
ThrowingCallable boomCode = () -> {
throw new Exception("boom!");
};
ThrowingCallable doNothing = () -> {};
// assertions succeed
assertThatCode(doNothing).doesNotThrowAnyException();
assertThatCode(boomCode).isInstanceOf(Exception.class)
.hasMessageContaining("boom");
// assertion fails
assertThatCode(boomCode).doesNotThrowAnyException();
Contrary to assertThatThrownBy(ThrowingCallable)
the test description provided with AbstractAssert#as(String,Object...) is always honored as shown below.
ThrowingCallable doNothing = () -> {};
// assertion fails and "display me" appears in the assertion error
assertThatCode(doNothing).as("display me")
.isInstanceOf(Exception.class);
This method was not named assertThat because the java compiler reported it ambiguous when used directly with a lambda :(
[中]允许捕获并在可丢弃文件上断言(使用lambdas更容易实现)。
与assertThatThrownBy(ThrowingCallable)的主要区别在于,如果没有引发异常,则此方法不会失败。
例子:
ThrowingCallable boomCode = () -> {
throw new Exception("boom!");
};
ThrowingCallable doNothing = () -> {};
// assertions succeed
assertThatCode(doNothing).doesNotThrowAnyException();
assertThatCode(boomCode).isInstanceOf(Exception.class)
.hasMessageContaining("boom");
// assertion fails
assertThatCode(boomCode).doesNotThrowAnyException();
与AbstractAssert#as(字符串、对象…)提供的测试描述assertThatThrownBy(ThrowingCallable)
相反始终如下图所示
ThrowingCallable doNothing = () -> {};
// assertion fails and "display me" appears in the assertion error
assertThatCode(doNothing).as("display me")
.isInstanceOf(Exception.class);
此方法未命名为assertThat,因为java编译器在直接与lambda一起使用时报告它不明确:(
代码示例来源:origin: gocd/gocd
@Test
public void shouldNotEncryptText() {
assertThatCode(() -> desEncrypter.encrypt(null))
.isInstanceOf(UnsupportedOperationException.class)
.hasMessage("Encrypting using DES is no longer supported!");
}
代码示例来源:origin: spring-projects/spring-security
@Test
public void decodeWhenUnsignedTokenThenMessageDoesNotMentionClass() {
assertThatCode(() -> this.decoder.decode(this.unsignedToken).block())
.isInstanceOf(JwtException.class)
.hasMessage("Unsupported algorithm of none");
}
代码示例来源:origin: spring-projects/spring-security
@Test
public void decodeWhenExpiredThenFail() {
assertThatCode(() -> this.decoder.decode(this.expired).block())
.isInstanceOf(JwtValidationException.class);
}
代码示例来源:origin: spring-projects/spring-security
@Test
public void loadUserWhenOAuth2UserSubjectNotEqualThenOAuth2AuthenticationException() {
Map<String, Object> attributes = new HashMap<>();
attributes.put(StandardClaimNames.SUB, "not-equal");
attributes.put("user", "rob");
OAuth2User oauth2User = new DefaultOAuth2User(AuthorityUtils.createAuthorityList("ROLE_USER"),
attributes, "user");
when(this.oauth2UserService.loadUser(any())).thenReturn(Mono.just(oauth2User));
assertThatCode(() -> this.userService.loadUser(userRequest()).block())
.isInstanceOf(OAuth2AuthenticationException.class);
}
代码示例来源:origin: spring-projects/spring-security
@Test
public void authenticationEntryPointWhenGivenNullThenThrowsException() {
ApplicationContext context = mock(ApplicationContext.class);
OAuth2ResourceServerConfigurer configurer = new OAuth2ResourceServerConfigurer(context);
assertThatCode(() -> configurer.authenticationEntryPoint(null))
.isInstanceOf(IllegalArgumentException.class);
}
代码示例来源:origin: spring-projects/spring-security
@Test
public void loadUserWhenOAuth2UserSubjectNullThenOAuth2AuthenticationException() {
OAuth2User oauth2User = new DefaultOAuth2User(AuthorityUtils.createAuthorityList("ROLE_USER"), Collections.singletonMap("user", "rob"), "user");
when(this.oauth2UserService.loadUser(any())).thenReturn(Mono.just(oauth2User));
assertThatCode(() -> this.userService.loadUser(userRequest()).block())
.isInstanceOf(OAuth2AuthenticationException.class);
}
代码示例来源:origin: spring-projects/spring-security
@Test
public void authenticateWhenRedirectUriNotEqualThenOAuth2AuthorizationException() {
this.authorizationRequest.redirectUri("https://example.org/notequal");
assertThatCode(() -> authenticate())
.isInstanceOf(OAuth2AuthorizationException.class);
}
代码示例来源:origin: spring-projects/spring-security
@Test
public void setBearerTokenResolverWhenNullThenThrowsException() {
assertThatCode(() -> this.filter.setBearerTokenResolver(null))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("bearerTokenResolver cannot be null");
}
代码示例来源:origin: gocd/gocd
@Test
public void shouldErrorOutWhenCipherTextIsTamperedWith() {
assertThatCode(() -> desEncrypter.decrypt("some bad junk"))
.hasMessageContaining("Illegal base64 character 20")
.hasCauseInstanceOf(IllegalArgumentException.class)
.isInstanceOf(CryptoException.class);
}
代码示例来源:origin: spring-projects/spring-security
@Test
public void configureWhenOpenIDAndFormLoginBothConfigureLoginPagesThenWiringException()
throws Exception {
assertThatCode(() -> this.spring.configLocations(this.xml("WithFormLoginAndOpenIDLoginPages")).autowire())
.isInstanceOf(BeanDefinitionParsingException.class);
}
代码示例来源:origin: spring-projects/spring-security
@Test
public void configureWhenUsingCiRegexMatcherAndServletPathThenThrowsException() {
assertThatCode(() -> this.spring.configLocations(this.xml("CiRegexMatcherServletPath")).autowire())
.isInstanceOf(BeanDefinitionParsingException.class);
}
代码示例来源:origin: spring-projects/spring-security
@Test
public void configureWhenUsingDefaultMatcherAndServletPathThenThrowsException() {
assertThatCode(() -> this.spring.configLocations(this.xml("DefaultMatcherServletPath")).autowire())
.isInstanceOf(BeanDefinitionParsingException.class);
}
代码示例来源:origin: spring-projects/spring-security
@Test
public void configureWhenUsingDataSourceAndANegativeTokenValidityThenThrowsWiringException() {
assertThatCode(() -> this.spring.configLocations(this.xml("NegativeTokenValidityWithDataSource")).autowire())
.isInstanceOf(FatalBeanException.class);
}
代码示例来源:origin: spring-projects/spring-security
@Test
public void setRealmNameWhenNullRealmNameThenNoExceptionThrown() {
assertThatCode(() -> this.accessDeniedHandler.setRealmName(null))
.doesNotThrowAnyException();
}
代码示例来源:origin: spring-projects/spring-security
@Test
public void configureWhenTwoFiltersWithSameOrderThenException() {
assertThatCode(() -> this.spring.configLocations(xml("CollidingFilters")).autowire())
.isInstanceOf(BeanDefinitionParsingException.class);
}
代码示例来源:origin: spring-projects/spring-security
@Test
public void setRealmNameWhenNullRealmNameThenNoExceptionThrown() {
assertThatCode(() -> this.authenticationEntryPoint.setRealmName(null))
.doesNotThrowAnyException();
}
代码示例来源:origin: spring-projects/spring-security
@Test
@WithMockUser
public void methodSecurityWhenUsingCustomPermissionEvaluatorThenPostAuthorizesAccordingly() {
this.spring.register(CustomAccessDecisionManagerConfig.class, MethodSecurityServiceConfig.class).autowire();
assertThatCode(() -> this.service.postHasPermission("granted"))
.doesNotThrowAnyException();
assertThatThrownBy(() -> this.service.postHasPermission("denied"))
.isInstanceOf(AccessDeniedException.class);
}
代码示例来源:origin: spring-projects/spring-security
@Test
public void authenticateWhenJwtDecodeFailsThenRespondsWithInvalidToken() {
BearerTokenAuthenticationToken token = this.authentication();
when(this.jwtDecoder.decode("token")).thenThrow(JwtException.class);
assertThatCode(() -> this.provider.authenticate(token))
.matches(failed -> failed instanceof OAuth2AuthenticationException)
.matches(errorCode(BearerTokenErrorCodes.INVALID_TOKEN));
}
代码示例来源:origin: spring-projects/spring-security
@Test
public void resolveWhenValidHeaderIsPresentTogetherWithQueryParameterThenAuthenticationExceptionIsThrown() {
MockServerHttpRequest.BaseBuilder<?> request = MockServerHttpRequest
.get("/")
.queryParam("access_token", TEST_TOKEN)
.header(HttpHeaders.AUTHORIZATION, "Bearer " + TEST_TOKEN);
assertThatCode(() -> convertToToken(request))
.isInstanceOf(OAuth2AuthenticationException.class)
.hasMessageContaining("Found multiple bearer tokens in the request");
}
代码示例来源:origin: spring-projects/spring-security
@Test
public void buildErrorResponseWhenStateIsNullThenDoesNotThrowAnyException() {
assertThatCode(() -> OAuth2AuthorizationResponse.error(ERROR_CODE)
.redirectUri(REDIRECT_URI)
.state(null)
.build()).doesNotThrowAnyException();
}
内容来源于网络,如有侵权,请联系作者删除!