org.springframework.mock.env.MockEnvironment Java Examples
The following examples show how to use
org.springframework.mock.env.MockEnvironment.
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
Source File: PropertySourcesPlaceholderConfigurerTests.java From spring-analysis-note with MIT License | 7 votes |
@Test public void replacementFromEnvironmentProperties() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerBeanDefinition("testBean", genericBeanDefinition(TestBean.class) .addPropertyValue("name", "${my.name}") .getBeanDefinition()); MockEnvironment env = new MockEnvironment(); env.setProperty("my.name", "myValue"); PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer(); ppc.setEnvironment(env); ppc.postProcessBeanFactory(bf); assertThat(bf.getBean(TestBean.class).getName(), equalTo("myValue")); assertThat(ppc.getAppliedPropertySources(), not(nullValue())); }
Example #2
Source File: EnableCachingIntegrationTests.java From spring-analysis-note with MIT License | 7 votes |
@Test public void beanConditionOn() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.setEnvironment(new MockEnvironment().withProperty("bar.enabled", "true")); ctx.register(BeanConditionConfig.class); ctx.refresh(); this.context = ctx; FooService service = this.context.getBean(FooService.class); Cache cache = getCache(); Object key = new Object(); Object value = service.getWithCondition(key); assertCacheHit(key, value, cache); value = service.getWithCondition(key); assertCacheHit(key, value, cache); assertEquals(2, this.context.getBean(BeanConditionConfig.Bar.class).count); }
Example #3
Source File: ConfigurationHelperTest.java From herd with Apache License 2.0 | 7 votes |
@Test public void testGetNonNegativeBigDecimalRequiredPropertyValueNegativeValueFail() { ConfigurationValue configurationValue = ConfigurationValue.EMR_CLUSTER_LOWEST_CORE_INSTANCE_PRICE_PERCENTAGE; MockEnvironment environment = new MockEnvironment(); environment.setProperty(configurationValue.getKey(), "-1.00"); try { configurationHelper.getNonNegativeBigDecimalRequiredProperty(configurationValue, environment); fail("Should throw an IllegalStatueException when property value is not BigDecimal."); } catch (IllegalStateException e) { assertEquals(String.format("Configuration \"%s\" has an invalid non-negative BigDecimal value: \"-1.00\".", configurationValue.getKey()), e.getMessage()); } }
Example #4
Source File: PropertySourcesPlaceholderConfigurerTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void replacementFromEnvironmentProperties() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerBeanDefinition("testBean", genericBeanDefinition(TestBean.class) .addPropertyValue("name", "${my.name}") .getBeanDefinition()); MockEnvironment env = new MockEnvironment(); env.setProperty("my.name", "myValue"); PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer(); ppc.setEnvironment(env); ppc.postProcessBeanFactory(bf); assertThat(bf.getBean(TestBean.class).getName(), equalTo("myValue")); assertThat(ppc.getAppliedPropertySources(), not(nullValue())); }
Example #5
Source File: ConfigurationHelperTest.java From herd with Apache License 2.0 | 6 votes |
@Test public void testGetBigDecimalRequiredPropertyValueBlankStringValue() { ConfigurationValue configurationValue = ConfigurationValue.EMR_CLUSTER_LOWEST_CORE_INSTANCE_PRICE_PERCENTAGE; MockEnvironment environment = new MockEnvironment(); environment.setProperty(configurationValue.getKey(), " "); try { configurationHelper.getBigDecimalRequiredProperty(configurationValue, environment); fail("Should throw an IllegalStatueException when property value is not BigDecimal."); } catch (IllegalStateException e) { assertEquals(String.format("Configuration \"%s\" must have a value.", configurationValue.getKey()), e.getMessage()); } }
Example #6
Source File: PropertySourcesPlaceholderConfigurerTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void withNonEnumerablePropertySource() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerBeanDefinition("testBean", genericBeanDefinition(TestBean.class) .addPropertyValue("name", "${foo}") .getBeanDefinition()); PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer(); PropertySource<?> ps = new PropertySource<Object>("simplePropertySource", new Object()) { @Override public Object getProperty(String key) { return "bar"; } }; MockEnvironment env = new MockEnvironment(); env.getPropertySources().addFirst(ps); ppc.setEnvironment(env); ppc.postProcessBeanFactory(bf); assertThat(bf.getBean(TestBean.class).getName(), equalTo("bar")); }
Example #7
Source File: PropertySourcesPlaceholderConfigurerTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void explicitPropertySourcesExcludesEnvironment() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerBeanDefinition("testBean", genericBeanDefinition(TestBean.class) .addPropertyValue("name", "${my.name}") .getBeanDefinition()); MutablePropertySources propertySources = new MutablePropertySources(); propertySources.addLast(new MockPropertySource()); PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer(); ppc.setPropertySources(propertySources); ppc.setEnvironment(new MockEnvironment().withProperty("my.name", "env")); ppc.setIgnoreUnresolvablePlaceholders(true); ppc.postProcessBeanFactory(bf); assertThat(bf.getBean(TestBean.class).getName(), equalTo("${my.name}")); assertEquals(ppc.getAppliedPropertySources().iterator().next(), propertySources.iterator().next()); }
Example #8
Source File: RoutePredicateHandlerMappingTests.java From spring-cloud-gateway with Apache License 2.0 | 6 votes |
@Test public void lookupRouteFromSyncPredicates() { Route routeFalse = Route.async().id("routeFalse").uri("http://localhost") .predicate(swe -> false).build(); Route routeFail = Route.async().id("routeFail").uri("http://localhost") .predicate(swe -> { throw new IllegalStateException("boom"); }).build(); Route routeTrue = Route.async().id("routeTrue").uri("http://localhost") .predicate(swe -> true).build(); RouteLocator routeLocator = () -> Flux.just(routeFalse, routeFail, routeTrue) .hide(); RoutePredicateHandlerMapping mapping = new RoutePredicateHandlerMapping(null, routeLocator, new GlobalCorsProperties(), new MockEnvironment()); final Mono<Route> routeMono = mapping .lookupRoute(Mockito.mock(ServerWebExchange.class)); StepVerifier.create(routeMono.map(Route::getId)).expectNext("routeTrue") .verifyComplete(); outputCapture .expect(containsString("Error applying predicate for route: routeFail")); outputCapture.expect(containsString("java.lang.IllegalStateException: boom")); }
Example #9
Source File: ConfigurationHelperTest.java From herd with Apache License 2.0 | 6 votes |
@Test public void testGetPropertyReturnDefaultWhenValueConversionFails() throws Exception { ConfigurationValue configurationValue = ConfigurationValue.BUSINESS_OBJECT_DATA_GET_ALL_MAX_RESULT_COUNT; Integer expectedValue = (Integer) configurationValue.getDefaultValue(); MockEnvironment environment = new MockEnvironment(); environment.setProperty(configurationValue.getKey(), "NOT_AN_INTEGER"); executeWithoutLogging(ConfigurationHelper.class, () -> { Integer value = ConfigurationHelper.getProperty(configurationValue, Integer.class, environment); assertNotNull("value", value); assertEquals("value", expectedValue, value); }); }
Example #10
Source File: PropertySourcesPlaceholderConfigurerTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void optionalPropertyWithoutValue() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.setConversionService(new DefaultConversionService()); bf.registerBeanDefinition("testBean", genericBeanDefinition(OptionalTestBean.class) .addPropertyValue("name", "${my.name}") .getBeanDefinition()); MockEnvironment env = new MockEnvironment(); env.setProperty("my.name", ""); PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer(); ppc.setEnvironment(env); ppc.setIgnoreUnresolvablePlaceholders(true); ppc.setNullValue(""); ppc.postProcessBeanFactory(bf); assertThat(bf.getBean(OptionalTestBean.class).getName(), equalTo(Optional.empty())); }
Example #11
Source File: TestPropertySourceUtilsTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void addPropertiesFilesToEnvironmentWithSinglePropertyFromVirtualFile() { ConfigurableEnvironment environment = new MockEnvironment(); MutablePropertySources propertySources = environment.getPropertySources(); propertySources.remove(MockPropertySource.MOCK_PROPERTIES_PROPERTY_SOURCE_NAME); assertEquals(0, propertySources.size()); String pair = "key = value"; ByteArrayResource resource = new ByteArrayResource(pair.getBytes(), "from inlined property: " + pair); ResourceLoader resourceLoader = mock(ResourceLoader.class); given(resourceLoader.getResource(anyString())).willReturn(resource); addPropertiesFilesToEnvironment(environment, resourceLoader, FOO_LOCATIONS); assertEquals(1, propertySources.size()); assertEquals("value", environment.getProperty("key")); }
Example #12
Source File: PropertySourcesPlaceholderConfigurerTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void withNonEnumerablePropertySource() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerBeanDefinition("testBean", genericBeanDefinition(TestBean.class) .addPropertyValue("name", "${foo}") .getBeanDefinition()); PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer(); PropertySource<?> ps = new PropertySource<Object>("simplePropertySource", new Object()) { @Override public Object getProperty(String key) { return "bar"; } }; MockEnvironment env = new MockEnvironment(); env.getPropertySources().addFirst(ps); ppc.setEnvironment(env); ppc.postProcessBeanFactory(bf); assertThat(bf.getBean(TestBean.class).getName(), equalTo("bar")); }
Example #13
Source File: ConfigurationHelperTest.java From herd with Apache License 2.0 | 6 votes |
@Test public void testGetRequiredPropertyIllegalStateException() { ConfigurationValue configurationValue = ConfigurationValue.HERD_ENVIRONMENT; MockEnvironment environment = new MockEnvironment(); environment.setProperty(configurationValue.getKey(), BLANK_TEXT); try { configurationHelper.getRequiredProperty(configurationValue, environment); fail(); } catch (IllegalStateException e) { assertEquals(String.format("Configuration \"%s\" must have a value.", ConfigurationValue.HERD_ENVIRONMENT.getKey()), e.getMessage()); } }
Example #14
Source File: PropertySourcesPlaceholderConfigurerTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void optionalPropertyWithValue() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.setConversionService(new DefaultConversionService()); bf.registerBeanDefinition("testBean", genericBeanDefinition(OptionalTestBean.class) .addPropertyValue("name", "${my.name}") .getBeanDefinition()); MockEnvironment env = new MockEnvironment(); env.setProperty("my.name", "myValue"); PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer(); ppc.setEnvironment(env); ppc.setIgnoreUnresolvablePlaceholders(true); ppc.postProcessBeanFactory(bf); assertThat(bf.getBean(OptionalTestBean.class).getName(), equalTo(Optional.of("myValue"))); }
Example #15
Source File: PropertySourcesPlaceholderConfigurerTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void replacementFromEnvironmentProperties() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerBeanDefinition("testBean", genericBeanDefinition(TestBean.class) .addPropertyValue("name", "${my.name}") .getBeanDefinition()); MockEnvironment env = new MockEnvironment(); env.setProperty("my.name", "myValue"); PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer(); ppc.setEnvironment(env); ppc.postProcessBeanFactory(bf); assertThat(bf.getBean(TestBean.class).getName(), equalTo("myValue")); assertThat(ppc.getAppliedPropertySources(), not(nullValue())); }
Example #16
Source File: EnableCachingIntegrationTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void beanConditionOn() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.setEnvironment(new MockEnvironment().withProperty("bar.enabled", "true")); ctx.register(BeanConditionConfig.class); ctx.refresh(); this.context = ctx; FooService service = this.context.getBean(FooService.class); Cache cache = getCache(); Object key = new Object(); Object value = service.getWithCondition(key); assertCacheHit(key, value, cache); value = service.getWithCondition(key); assertCacheHit(key, value, cache); assertEquals(2, this.context.getBean(BeanConditionConfig.Bar.class).count); }
Example #17
Source File: PropertySourcesPlaceholderConfigurerTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void explicitPropertySourcesExcludesEnvironment() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerBeanDefinition("testBean", genericBeanDefinition(TestBean.class) .addPropertyValue("name", "${my.name}") .getBeanDefinition()); MutablePropertySources propertySources = new MutablePropertySources(); propertySources.addLast(new MockPropertySource()); PropertySourcesPlaceholderConfigurer pc = new PropertySourcesPlaceholderConfigurer(); pc.setPropertySources(propertySources); pc.setEnvironment(new MockEnvironment().withProperty("my.name", "env")); pc.setIgnoreUnresolvablePlaceholders(true); pc.postProcessBeanFactory(bf); assertThat(bf.getBean(TestBean.class).getName(), equalTo("${my.name}")); assertEquals(pc.getAppliedPropertySources().iterator().next(), propertySources.iterator().next()); }
Example #18
Source File: ConfigurationHelperTest.java From herd with Apache License 2.0 | 6 votes |
@Test public void testGetBooleanPropertyValueConversionFails() { ConfigurationValue configurationValue = ConfigurationValue.USER_NAMESPACE_AUTHORIZATION_ENABLED; MockEnvironment environment = new MockEnvironment(); environment.setProperty(configurationValue.getKey(), "NOT_A_BOOLEAN"); try { configurationHelper.getBooleanProperty(configurationValue, environment); fail("Should throw an IllegalStatueException when property value is not boolean."); } catch (IllegalStateException e) { assertEquals(String.format("Configuration \"%s\" has an invalid boolean value: \"NOT_A_BOOLEAN\".", configurationValue.getKey()), e.getMessage()); } }
Example #19
Source File: EnvironmentManagerTest.java From spring-cloud-commons with Apache License 2.0 | 6 votes |
@Test public void testCorrectEvents() { MockEnvironment environment = new MockEnvironment(); ApplicationEventPublisher publisher = mock(ApplicationEventPublisher.class); EnvironmentManager environmentManager = new EnvironmentManager(environment); environmentManager.setApplicationEventPublisher(publisher); environmentManager.setProperty("foo", "bar"); then(environment.getProperty("foo")).isEqualTo("bar"); ArgumentCaptor<ApplicationEvent> eventCaptor = ArgumentCaptor .forClass(ApplicationEvent.class); verify(publisher, times(1)).publishEvent(eventCaptor.capture()); then(eventCaptor.getValue()).isInstanceOf(EnvironmentChangeEvent.class); EnvironmentChangeEvent event = (EnvironmentChangeEvent) eventCaptor.getValue(); then(event.getKeys()).containsExactly("foo"); reset(publisher); environmentManager.reset(); then(environment.getProperty("foo")).isNull(); verify(publisher, times(1)).publishEvent(eventCaptor.capture()); then(eventCaptor.getValue()).isInstanceOf(EnvironmentChangeEvent.class); event = (EnvironmentChangeEvent) eventCaptor.getValue(); then(event.getKeys()).containsExactly("foo"); }
Example #20
Source File: PiiReplacerConfigurationTest.java From data-highway with Apache License 2.0 | 5 votes |
@Test public void defaultPiiReplacer() throws Exception { try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) { context.setEnvironment(new MockEnvironment()); context.register(PiiReplacerConfiguration.class); context.refresh(); assertThat(context.getBean(PiiReplacer.class), is(instanceOf(DefaultPiiReplacer.class))); } }
Example #21
Source File: DockerPostgresDatabaseProviderTest.java From embedded-database-spring-test with Apache License 2.0 | 5 votes |
@Test public void providersWithSameCustomizersShouldEquals() { when(containerCustomizers.getIfAvailable()).thenReturn( Collections.singletonList(postgresContainerCustomizer(61)), Collections.singletonList(postgresContainerCustomizer(61))); DockerPostgresDatabaseProvider provider1 = new DockerPostgresDatabaseProvider(new MockEnvironment(), containerCustomizers); DockerPostgresDatabaseProvider provider2 = new DockerPostgresDatabaseProvider(new MockEnvironment(), containerCustomizers); assertThat(provider1).isEqualTo(provider2); }
Example #22
Source File: WebConfigurerTest.java From jhipster-microservices-example with Apache License 2.0 | 5 votes |
@Before public void setup() { servletContext = spy(new MockServletContext()); doReturn(new MockFilterRegistration()) .when(servletContext).addFilter(anyString(), any(Filter.class)); doReturn(new MockServletRegistration()) .when(servletContext).addServlet(anyString(), any(Servlet.class)); env = new MockEnvironment(); props = new JHipsterProperties(); webConfigurer = new WebConfigurer(env, props); metricRegistry = new MetricRegistry(); webConfigurer.setMetricRegistry(metricRegistry); }
Example #23
Source File: WebConfigurerTest.java From okta-jhipster-microservices-oauth-example with Apache License 2.0 | 5 votes |
@Before public void setup() { servletContext = spy(new MockServletContext()); doReturn(mock(FilterRegistration.Dynamic.class)) .when(servletContext).addFilter(anyString(), any(Filter.class)); doReturn(mock(ServletRegistration.Dynamic.class)) .when(servletContext).addServlet(anyString(), any(Servlet.class)); env = new MockEnvironment(); props = new JHipsterProperties(); webConfigurer = new WebConfigurer(env, props); metricRegistry = new MetricRegistry(); webConfigurer.setMetricRegistry(metricRegistry); }
Example #24
Source File: ShardingSpringBootConditionTest.java From shardingsphere with Apache License 2.0 | 5 votes |
@Test public void assertNotMatch() { MockEnvironment mockEnvironment = new MockEnvironment(); mockEnvironment.setProperty("spring.shardingsphere.rules.encrypt.encryptors.aes_encryptor.type", "AES"); ConditionContext context = Mockito.mock(ConditionContext.class); AnnotatedTypeMetadata metadata = Mockito.mock(AnnotatedTypeMetadata.class); when(context.getEnvironment()).thenReturn(mockEnvironment); ShardingSpringBootCondition condition = new ShardingSpringBootCondition(); ConditionOutcome matchOutcome = condition.getMatchOutcome(context, metadata); assertThat(matchOutcome.isMatch(), is(false)); }
Example #25
Source File: TestPropertySourceUtilsTests.java From java-technology-stack with MIT License | 5 votes |
@Test @SuppressWarnings("rawtypes") public void addInlinedPropertiesToEnvironmentWithEmptyProperty() { ConfigurableEnvironment environment = new MockEnvironment(); MutablePropertySources propertySources = environment.getPropertySources(); propertySources.remove(MockPropertySource.MOCK_PROPERTIES_PROPERTY_SOURCE_NAME); assertEquals(0, propertySources.size()); addInlinedPropertiesToEnvironment(environment, asArray(" ")); assertEquals(1, propertySources.size()); assertEquals(0, ((Map) propertySources.iterator().next().getSource()).size()); }
Example #26
Source File: MasterSlaveSpringBootConditionTest.java From shardingsphere with Apache License 2.0 | 5 votes |
@Test public void assertNotMatch() { MockEnvironment mockEnvironment = new MockEnvironment(); mockEnvironment.setProperty("spring.shardingsphere.rules.encrypt.encryptors.aes_encryptor.type", "AES"); mockEnvironment.setProperty("spring.shardingsphere.rules.shadow.column", "user_id"); ConditionContext context = Mockito.mock(ConditionContext.class); AnnotatedTypeMetadata metadata = Mockito.mock(AnnotatedTypeMetadata.class); when(context.getEnvironment()).thenReturn(mockEnvironment); MasterSlaveSpringBootCondition condition = new MasterSlaveSpringBootCondition(); ConditionOutcome matchOutcome = condition.getMatchOutcome(context, metadata); assertThat(matchOutcome.isMatch(), is(false)); }
Example #27
Source File: ShadowSpringBootConditionTest.java From shardingsphere with Apache License 2.0 | 5 votes |
@Test public void assertNotMatch() { MockEnvironment mockEnvironment = new MockEnvironment(); mockEnvironment.setProperty("spring.shardingsphere.rules.encrypt.encryptors.aes_encryptor.type", "AES"); ConditionContext context = Mockito.mock(ConditionContext.class); AnnotatedTypeMetadata metadata = Mockito.mock(AnnotatedTypeMetadata.class); when(context.getEnvironment()).thenReturn(mockEnvironment); ShadowSpringBootCondition condition = new ShadowSpringBootCondition(); ConditionOutcome matchOutcome = condition.getMatchOutcome(context, metadata); assertThat(matchOutcome.isMatch(), is(false)); }
Example #28
Source File: YandexPostgresDatabaseProviderTest.java From embedded-database-spring-test with Apache License 2.0 | 5 votes |
@Test public void providersWithSameConfigurationShouldEquals() { MockEnvironment environment = new MockEnvironment(); environment.setProperty("zonky.test.database.postgres.yandex-provider.postgres-version", "postgres-version"); environment.setProperty("zonky.test.database.postgres.initdb.properties.xxx", "xxx-value"); environment.setProperty("zonky.test.database.postgres.server.properties.yyy", "yyy-value"); environment.setProperty("zonky.test.database.postgres.client.properties.zzz", "zzz-value"); YandexPostgresDatabaseProvider provider1 = new YandexPostgresDatabaseProvider(environment); YandexPostgresDatabaseProvider provider2 = new YandexPostgresDatabaseProvider(environment); assertThat(provider1).isEqualTo(provider2); }
Example #29
Source File: HikariDataSourcePropertiesSetterTest.java From shardingsphere with Apache License 2.0 | 5 votes |
@Before public void setUp() { MockEnvironment mockEnvironment = new MockEnvironment(); mockEnvironment.setProperty("spring.shardingsphere.datasource.ds_master.type", "com.zaxxer.hikari.HikariDataSource"); mockEnvironment.setProperty("spring.shardingsphere.datasource.ds_master.data-source-properties.cachePrepStmts", "true"); mockEnvironment.setProperty("spring.shardingsphere.datasource.ds_master.data-source-properties.prepStmtCacheSize", "250"); environment = mockEnvironment; }
Example #30
Source File: PropertySourcesPlaceholderConfigurerTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void trimValuesIsOffByDefault() { PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer(); DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerBeanDefinition("testBean", rootBeanDefinition(TestBean.class) .addPropertyValue("name", "${my.name}") .getBeanDefinition()); ppc.setEnvironment(new MockEnvironment().withProperty("my.name", " myValue ")); ppc.postProcessBeanFactory(bf); assertThat(bf.getBean(TestBean.class).getName(), equalTo(" myValue ")); }