类org.springframework.boot.test.context.FilteredClassLoader源码实例Demo

下面列出了怎么用org.springframework.boot.test.context.FilteredClassLoader的API类实例代码及写法,或者点击链接到github查看源代码。

@Test
void configurationOfRepositoryTypeShouldWork() {
	this.contextRunner
		.withPropertyValues("spring.data.neo4j.repositories.type=none")
		.withUserConfiguration(TestConfiguration.class)
		.withClassLoader(new FilteredClassLoader(Flux.class))
		.run(ctx ->
			assertThat(ctx)
				.doesNotHaveBean(Neo4jTransactionManager.class)
				.doesNotHaveBean(ReactiveNeo4jClient.class)
				.doesNotHaveBean(Neo4jRepository.class)
		);

	this.contextRunner
		.withPropertyValues("spring.data.neo4j.repositories.type=imperative")
		.withUserConfiguration(TestConfiguration.class)
		.run(ctx ->
			assertThat(ctx)
				.hasSingleBean(Neo4jTransactionManager.class)
				.hasSingleBean(Neo4jClient.class)
				.doesNotHaveBean(ReactiveNeo4jRepository.class)
		);
}
 
@Test
public void testPostgre() {
	this.contextRunner.withPropertyValues(
			"spring.cloud.gcp.sql.instanceConnectionName=tubular-bells:singapore:test-instance")
			.withClassLoader(
					new FilteredClassLoader("com.google.cloud.sql.mysql"))
			.run((context) -> {
				CloudSqlJdbcInfoProvider urlProvider =
						context.getBean(CloudSqlJdbcInfoProvider.class);
				assertThat(urlProvider.getJdbcUrl()).isEqualTo(
						"jdbc:postgresql://google/test-database?"
								+ "socketFactory=com.google.cloud.sql.postgres.SocketFactory"
								+ "&cloudSqlInstance=tubular-bells:singapore:test-instance");
				assertThat(urlProvider.getJdbcDriverClass()).matches("org.postgresql.Driver");
			});
}
 
源代码3 项目: sdn-rx   文件: Neo4jDataAutoConfigurationTest.java
@Test
@DisplayName("Should require all needed classes")
void shouldRequireAllNeededClasses() {
	contextRunner
		.withClassLoader(
			new FilteredClassLoader(Neo4jTransactionManager.class, PlatformTransactionManager.class))
		.run(ctx -> assertThat(ctx)
			.doesNotHaveBean(Neo4jClient.class)
			.doesNotHaveBean(Neo4jTemplate.class)
			.doesNotHaveBean(Neo4jTransactionManager.class)
		);
}
 
源代码4 项目: sdn-rx   文件: Neo4jDataAutoConfigurationTest.java
@Test
@DisplayName("Should require all needed classes")
void shouldRequireAllNeededClasses() {
	contextRunner
		.withClassLoader(
			new FilteredClassLoader(ReactiveNeo4jTransactionManager.class, ReactiveTransactionManager.class, Flux.class))
		.run(ctx -> assertThat(ctx)
			.doesNotHaveBean(ReactiveNeo4jClient.class)
			.doesNotHaveBean(ReactiveNeo4jTemplate.class)
			.doesNotHaveBean(ReactiveNeo4jTransactionManager.class)
		);
}
 
@Test
public void configurations_not_loaded_when_mvc_is_not_on_class_path() {
	contextRunner
			.withClassLoader(new FilteredClassLoader("org.springframework.web.context.support.GenericWebApplicationContext"))
			.run(context -> assertThat(context)
					.hasNotFailed()
					.doesNotHaveBean("openApiResource")
					.doesNotHaveBean("actuatorProvider")
					.doesNotHaveBean("multipleOpenApiResource")
			);

}
 
@Test
public void configurations_not_loaded_when_reactor_is_not_on_class_path() {
	contextRunner
			.withClassLoader(new FilteredClassLoader("org.springframework.web.reactive.HandlerResult"))
			.run(context -> assertThat(context)
					.hasNotFailed()
					.doesNotHaveBean("openApiResource"));

}
 
@Test
public void testHalJacksonHttpMessageConverterIsNotLoaded() {
	FilteredClassLoader filteredClassLoader = new FilteredClassLoader(
			RepositoryRestMvcConfiguration.class, RepresentationModel.class);
	contextRunner.withClassLoader(filteredClassLoader)
			.run(context -> assertThat(context)
					.doesNotHaveBean("halJacksonHttpMessageConverter"));
}
 
@Test
public void testHalJacksonHttpMessageConverterIsLoaded() {
	FilteredClassLoader filteredClassLoader = new FilteredClassLoader(
			RepositoryRestMvcConfiguration.class);
	contextRunner.withClassLoader(filteredClassLoader).run(
			context -> assertThat(context).hasBean("halJacksonHttpMessageConverter"));
}
 
@Test
public final void autoConfigurationWithoutJwtOnClasspathInactive() {
	contextRunner.withClassLoader(new FilteredClassLoader(Jwt.class)) // removes Jwt.class from classpath
			.run((context) -> {
				assertThat(context.containsBean("xsuaaServiceConfiguration"), is(false));
			});
}
 
@Test
public void autoConfigurationSkipped_without_XsuaaServiceConfiguration() {
	contextRunner.withClassLoader(new FilteredClassLoader(XsuaaServiceConfiguration.class))
			.run((context) -> {
				assertThat(context).doesNotHaveBean("xsuaaTokenFlows");
			});
}
 
@Test
public void autoConfigurationInactive_if_noXsuaaTokenFlowsOnClasspath() {
	contextRunner.withClassLoader(new FilteredClassLoader(XsuaaTokenFlows.class))
			.run((context) -> {
				assertThat(context).doesNotHaveBean("xsuaaTokenFlows");
			});
}
 
@Test
public void autoConfigurationInactive_if_noJwtOnClasspath() {
	contextRunner.withClassLoader(new FilteredClassLoader(Jwt.class)) // removes Jwt.class from classpath
			.run((context) -> {
				assertThat(context).doesNotHaveBean("xsuaaServiceConfiguration");
				assertThat(context).doesNotHaveBean("xsuaaTokenDecoder");
				assertThat(context).doesNotHaveBean("xsuaaRestOperations");
			});
}
 
@Test
public void autoConfigurationWithoutXsuaaServiceConfigurationOnClasspathInactive() {
	contextRunner.withClassLoader(
			new FilteredClassLoader(Jwt.class)) // make sure Jwt.class is not on the classpath
			.run((context) -> {
				assertThat(context.containsBean("xsuaaJwtDecoder"), is(false));
			});
}
 
@Test
void servicesAreNotCreatedWithoutLoggingOnClasspath() {
	contextRunner
		.withClassLoader(new FilteredClassLoader(ApplicationLogStreamPublisher.class))
		.withUserConfiguration(LoggingConfiguration.class)
		.run(context -> assertThat(context)
			.doesNotHaveBean(StreamingLogWebSocketHandler.class)
			.doesNotHaveBean(WebSocketHandlerAdapter.class)
			.doesNotHaveBean(HandlerMapping.class)
			.doesNotHaveBean(LogStreamPublisher.class)
			.doesNotHaveBean(ApplicationLogStreamPublisher.class));
}
 
@Test
void servicesAreNotCreatedWithoutCredHubOnClasspath() {
	contextRunner
		.withClassLoader(new FilteredClassLoader(ReactiveCredHubOperations.class))
		.run((context) -> {
			assertThat(context)
				.hasSingleBean(CredentialGenerator.class)
				.getBean(CredentialGenerator.class)
				.isExactlyInstanceOf(SimpleCredentialGenerator.class);

			assertThat(context)
				.doesNotHaveBean(CredHubPersistingCreateServiceInstanceAppBindingWorkflow.class)
				.doesNotHaveBean(CredHubPersistingDeleteServiceInstanceBindingWorkflow.class);
		});
}
 
@Test
void servicesAreNotCreatedWithoutLoggingOnClasspath() {
	contextRunner
		.withClassLoader(new FilteredClassLoader(ApplicationRecentLogsProvider.class))
		.withUserConfiguration(LoggingConfiguration.class)
		.run(context -> assertThat(context)
			.doesNotHaveBean(RecentLogsProvider.class)
			.doesNotHaveBean(RecentLogsController.class));
}
 
@Test
public void reactiveCredHubTemplateNotConfiguredWithoutWebClient() {
	this.context.withPropertyValues("spring.credhub.url=https://localhost")
			.withClassLoader(new FilteredClassLoader(WebClient.class)).run((context) -> {
				assertThat(context).hasSingleBean(CredHubTemplate.class);
				CredHubTemplate credHubTemplate = context.getBean(CredHubTemplate.class);
				assertThat(credHubTemplate.isUsingOAuth2()).isFalse();

				assertThat(context).doesNotHaveBean(ReactiveCredHubTemplate.class);
			});
}
 
@Test
public void oauth2ContextConfiguredWithoutWebClient() {
	this.context.withClassLoader(new FilteredClassLoader(WebClient.class))
			.withPropertyValues(this.oAuth2ClientProperties).run((context) -> {
				assertServletOAuth2ContextConfigured(context);
				assertReactiveOAuth2ContextNotConfigured(context);
			});
}
 
@Test
public void oauth2ContextConfiguredWithReactiveWebAppNoServlet() {
	new ReactiveWebApplicationContextRunner().withClassLoader(new FilteredClassLoader("javax.servlet"))
			.withConfiguration(AutoConfigurations.of(this.configurations))
			.withPropertyValues(this.oAuth2ClientProperties).run((context) -> {
				assertServletOAuth2ContextNotConfigured(context);
				assertReactiveOAuth2ContextConfigured(context);
			});
}
 
@Test
public void webClientConnectorNotConfigured() {
	this.context.withClassLoader(new FilteredClassLoader(WebClient.class))
			.withPropertyValues("spring.credhub.url=https://localhost",
					"spring.credhub.oauth2.registration-id=test-client", "spring.credhub.connection-timeout=30",
					"spring.credhub.read-timeout=60")
			.run((context) -> assertThat(context).doesNotHaveBean(ClientHttpConnector.class));
}
 
@Test
public void testNoJdbc() {
	this.contextRunner.withPropertyValues(
			"spring.cloud.gcp.sql.instanceConnectionName=tubular-bells:singapore:test-instance")
			.withClassLoader(
					new FilteredClassLoader(EmbeddedDatabaseType.class, DataSource.class))
			.run((context) -> {
				assertThat(context.getBeanNamesForType(DataSource.class)).isEmpty();
				assertThat(context.getBeanNamesForType(DataSourceProperties.class)).isEmpty();
				assertThat(context.getBeanNamesForType(GcpCloudSqlProperties.class)).isEmpty();
				assertThat(context.getBeanNamesForType(CloudSqlJdbcInfoProvider.class)).isEmpty();
			});
}
 
@Test
public void testIdConverterNotCreated() {
	this.contextRunner
			.withClassLoader(
					new FilteredClassLoader("org.springframework.data.rest.webmvc.spi"))
			.run((context) -> {
				assertThat(
						context.getBeansOfType(BackendIdConverter.class).size()).isEqualTo(0);
				});
}
 
@Test
public void worksWithoutWebflux() {
	contextRunner
			.withClassLoader(
					new FilteredClassLoader("org.springframework.web.reactive"))
			.run(context -> {
				assertThat(context).doesNotHaveBean(ReactiveDiscoveryClient.class);
				assertThat(context).doesNotHaveBean(
						ReactiveDiscoveryClientHealthIndicator.class);
			});
}
 
@Test
public void worksWithoutActuator() {
	contextRunner
			.withClassLoader(
					new FilteredClassLoader("org.springframework.boot.actuate"))
			.run(context -> {
				assertThat(context).hasSingleBean(ReactiveDiscoveryClient.class);
				assertThat(context).doesNotHaveBean(
						ReactiveDiscoveryClientHealthIndicator.class);
			});
}
 
源代码25 项目: joinfaces   文件: JpaWebAutoConfigurationTest.java
@Test
public void testDisabledByMissingEntityManagerClass() {
	this.webApplicationContextRunner
			.withClassLoader(new FilteredClassLoader(EntityManager.class))
			.run(context -> {
		assertThat(context).doesNotHaveBean(OpenEntityManagerInViewInterceptor.class);
		assertThat(context).doesNotHaveBean(OpenEntityManagerInViewFilter.class);
	});
}
 
@Test
public void testKafkaBinderMetricsWhenNoMicrometer() {
	new ApplicationContextRunner().withUserConfiguration(KafkaMetricsTestConfig.class)
			.withClassLoader(new FilteredClassLoader("io.micrometer.core"))
			.run(context -> {
				assertThat(context.getBeanNamesForType(MeterRegistry.class))
						.isEmpty();
				assertThat(context.getBeanNamesForType(MeterBinder.class)).isEmpty();

				DirectFieldAccessor channelBindingServiceAccessor = new DirectFieldAccessor(
						context.getBean(BindingService.class));
				@SuppressWarnings("unchecked")
				Map<String, List<Binding<MessageChannel>>> consumerBindings =
					(Map<String, List<Binding<MessageChannel>>>) channelBindingServiceAccessor
						.getPropertyValue("consumerBindings");
				assertThat(new DirectFieldAccessor(
						consumerBindings.get("input").get(0)).getPropertyValue(
								"lifecycle.messageListenerContainer.beanName"))
										.isEqualTo("setByCustomizer:input");
				assertThat(new DirectFieldAccessor(
						consumerBindings.get("input").get(0)).getPropertyValue(
								"lifecycle.beanName"))
										.isEqualTo("setByCustomizer:input");
				assertThat(new DirectFieldAccessor(
						consumerBindings.get("source").get(0)).getPropertyValue(
								"lifecycle.beanName"))
										.isEqualTo("setByCustomizer:source");

				@SuppressWarnings("unchecked")
				Map<String, Binding<MessageChannel>> producerBindings =
					(Map<String, Binding<MessageChannel>>) channelBindingServiceAccessor
						.getPropertyValue("producerBindings");

				assertThat(new DirectFieldAccessor(
						producerBindings.get("output")).getPropertyValue(
						"lifecycle.beanName"))
						.isEqualTo("setByCustomizer:output");
			});
}
 
@Test
public void shouldConfigureForNoOpWhenMissingImplementation() {
	this.contextRunner
			.withClassLoader(new FilteredClassLoader(org.apache.camel.Message.class,
					org.springframework.messaging.Message.class, JmsTemplate.class,
					KafkaTemplate.class, RabbitTemplate.class, EnableBinding.class))
			.run((context) -> {
				assertThat(context.getBeansOfType(MessageVerifierSender.class))
						.hasSize(1);
				assertThat(context.getBeansOfType(NoOpStubMessages.class)).hasSize(1);
			});
}
 
@Test
public void shouldNotConfigureIfHttpClientIsMissing() {

	this.contextRunner.withUserConfiguration(AuthenticationFactoryConfiguration.class)
			.withClassLoader(
					new FilteredClassLoader("reactor.netty.http.client.HttpClient"))
			.run(context -> {

				assertThat(context.getBeanNamesForType(ReactiveVaultOperations.class))
						.isEmpty();
			});
}
 
@Test
public void withEntityManagerFactoryBeanAndMissingSpringProcessEngineConfigurationClass() {
    contextRunner
        .withConfiguration(AutoConfigurations.of(
            DataSourceAutoConfiguration.class,
            HibernateJpaAutoConfiguration.class
        ))
        .withClassLoader(new FilteredClassLoader(SpringProcessEngineConfiguration.class))
        .run(context -> {
            assertThat(context).doesNotHaveBean(FlowableJpaAutoConfiguration.class);
        });
}
 
@Test
public void withMissingAuthenticationManager() {
    contextRunner
        .withConfiguration(IDM_CONFIGURATION)
        .withClassLoader(new FilteredClassLoader(AuthenticationManager.class))
        .run(context -> assertThat(context)
            .hasSingleBean(IdmIdentityService.class)
            .doesNotHaveBean(FlowableSecurityAutoConfiguration.class));
}
 
 类所在包
 同包方法