Java Code Examples for org.springframework.boot.test.context.FilteredClassLoader

The following examples show how to use org.springframework.boot.test.context.FilteredClassLoader. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@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)
		);
}
 
Example 2
@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");
			});
}
 
Example 3
Source Project: sdn-rx   Source File: Neo4jDataAutoConfigurationTest.java    License: Apache License 2.0 5 votes vote down vote up
@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)
		);
}
 
Example 4
Source Project: sdn-rx   Source File: Neo4jDataAutoConfigurationTest.java    License: Apache License 2.0 5 votes vote down vote up
@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)
		);
}
 
Example 5
@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")
			);

}
 
Example 6
@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"));

}
 
Example 7
@Test
public void testHalJacksonHttpMessageConverterIsNotLoaded() {
	FilteredClassLoader filteredClassLoader = new FilteredClassLoader(
			RepositoryRestMvcConfiguration.class, RepresentationModel.class);
	contextRunner.withClassLoader(filteredClassLoader)
			.run(context -> assertThat(context)
					.doesNotHaveBean("halJacksonHttpMessageConverter"));
}
 
Example 8
@Test
public void testHalJacksonHttpMessageConverterIsLoaded() {
	FilteredClassLoader filteredClassLoader = new FilteredClassLoader(
			RepositoryRestMvcConfiguration.class);
	contextRunner.withClassLoader(filteredClassLoader).run(
			context -> assertThat(context).hasBean("halJacksonHttpMessageConverter"));
}
 
Example 9
@Test
public final void autoConfigurationWithoutJwtOnClasspathInactive() {
	contextRunner.withClassLoader(new FilteredClassLoader(Jwt.class)) // removes Jwt.class from classpath
			.run((context) -> {
				assertThat(context.containsBean("xsuaaServiceConfiguration"), is(false));
			});
}
 
Example 10
@Test
public void autoConfigurationSkipped_without_XsuaaServiceConfiguration() {
	contextRunner.withClassLoader(new FilteredClassLoader(XsuaaServiceConfiguration.class))
			.run((context) -> {
				assertThat(context).doesNotHaveBean("xsuaaTokenFlows");
			});
}
 
Example 11
@Test
public void autoConfigurationInactive_if_noXsuaaTokenFlowsOnClasspath() {
	contextRunner.withClassLoader(new FilteredClassLoader(XsuaaTokenFlows.class))
			.run((context) -> {
				assertThat(context).doesNotHaveBean("xsuaaTokenFlows");
			});
}
 
Example 12
@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");
			});
}
 
Example 13
@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));
			});
}
 
Example 14
@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));
}
 
Example 15
@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);
		});
}
 
Example 16
@Test
void servicesAreNotCreatedWithoutLoggingOnClasspath() {
	contextRunner
		.withClassLoader(new FilteredClassLoader(ApplicationRecentLogsProvider.class))
		.withUserConfiguration(LoggingConfiguration.class)
		.run(context -> assertThat(context)
			.doesNotHaveBean(RecentLogsProvider.class)
			.doesNotHaveBean(RecentLogsController.class));
}
 
Example 17
@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);
			});
}
 
Example 18
@Test
public void oauth2ContextConfiguredWithoutWebClient() {
	this.context.withClassLoader(new FilteredClassLoader(WebClient.class))
			.withPropertyValues(this.oAuth2ClientProperties).run((context) -> {
				assertServletOAuth2ContextConfigured(context);
				assertReactiveOAuth2ContextNotConfigured(context);
			});
}
 
Example 19
@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);
			});
}
 
Example 20
@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));
}
 
Example 21
@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();
			});
}
 
Example 22
@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);
				});
}
 
Example 23
@Test
public void worksWithoutWebflux() {
	contextRunner
			.withClassLoader(
					new FilteredClassLoader("org.springframework.web.reactive"))
			.run(context -> {
				assertThat(context).doesNotHaveBean(ReactiveDiscoveryClient.class);
				assertThat(context).doesNotHaveBean(
						ReactiveDiscoveryClientHealthIndicator.class);
			});
}
 
Example 24
@Test
public void worksWithoutActuator() {
	contextRunner
			.withClassLoader(
					new FilteredClassLoader("org.springframework.boot.actuate"))
			.run(context -> {
				assertThat(context).hasSingleBean(ReactiveDiscoveryClient.class);
				assertThat(context).doesNotHaveBean(
						ReactiveDiscoveryClientHealthIndicator.class);
			});
}
 
Example 25
Source Project: joinfaces   Source File: JpaWebAutoConfigurationTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testDisabledByMissingEntityManagerClass() {
	this.webApplicationContextRunner
			.withClassLoader(new FilteredClassLoader(EntityManager.class))
			.run(context -> {
		assertThat(context).doesNotHaveBean(OpenEntityManagerInViewInterceptor.class);
		assertThat(context).doesNotHaveBean(OpenEntityManagerInViewFilter.class);
	});
}
 
Example 26
@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");
			});
}
 
Example 27
@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);
			});
}
 
Example 28
@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();
			});
}
 
Example 29
@Test
public void withEntityManagerFactoryBeanAndMissingSpringProcessEngineConfigurationClass() {
    contextRunner
        .withConfiguration(AutoConfigurations.of(
            DataSourceAutoConfiguration.class,
            HibernateJpaAutoConfiguration.class
        ))
        .withClassLoader(new FilteredClassLoader(SpringProcessEngineConfiguration.class))
        .run(context -> {
            assertThat(context).doesNotHaveBean(FlowableJpaAutoConfiguration.class);
        });
}
 
Example 30
@Test
public void withMissingAuthenticationManager() {
    contextRunner
        .withConfiguration(IDM_CONFIGURATION)
        .withClassLoader(new FilteredClassLoader(AuthenticationManager.class))
        .run(context -> assertThat(context)
            .hasSingleBean(IdmIdentityService.class)
            .doesNotHaveBean(FlowableSecurityAutoConfiguration.class));
}