Search in sources :

Example 56 with MockEnvironment

use of org.springframework.mock.env.MockEnvironment in project spring-framework by spring-projects.

the class ContextNamespaceHandlerTests method propertyPlaceholderEnvironmentProperties.

@Test
public void propertyPlaceholderEnvironmentProperties() throws Exception {
    MockEnvironment env = new MockEnvironment().withProperty("foo", "spam");
    GenericXmlApplicationContext applicationContext = new GenericXmlApplicationContext();
    applicationContext.setEnvironment(env);
    applicationContext.load(new ClassPathResource("contextNamespaceHandlerTests-simple.xml", getClass()));
    applicationContext.refresh();
    assertEquals("spam", applicationContext.getBean("string"));
    assertEquals("none", applicationContext.getBean("fallback"));
}
Also used : MockEnvironment(org.springframework.mock.env.MockEnvironment) GenericXmlApplicationContext(org.springframework.context.support.GenericXmlApplicationContext) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 57 with MockEnvironment

use of org.springframework.mock.env.MockEnvironment in project spring-boot by spring-projects.

the class DevToolsHomePropertiesPostProcessorTests method loadsHomeProperties.

@Test
public void loadsHomeProperties() throws Exception {
    Properties properties = new Properties();
    properties.put("abc", "def");
    OutputStream out = new FileOutputStream(new File(this.home, ".spring-boot-devtools.properties"));
    properties.store(out, null);
    out.close();
    ConfigurableEnvironment environment = new MockEnvironment();
    MockDevToolHomePropertiesPostProcessor postProcessor = new MockDevToolHomePropertiesPostProcessor();
    postProcessor.postProcessEnvironment(environment, null);
    assertThat(environment.getProperty("abc")).isEqualTo("def");
}
Also used : ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) MockEnvironment(org.springframework.mock.env.MockEnvironment) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) Properties(java.util.Properties) File(java.io.File) Test(org.junit.Test)

Example 58 with MockEnvironment

use of org.springframework.mock.env.MockEnvironment in project spring-boot by spring-projects.

the class LocalHostWebConnectionHtmlUnitDriverTests method getWhenUrlIsRelativeAndNoPortWillUseLocalhost8080.

@Test
public void getWhenUrlIsRelativeAndNoPortWillUseLocalhost8080() throws Exception {
    MockEnvironment environment = new MockEnvironment();
    LocalHostWebConnectionHtmlUnitDriver driver = new TestLocalHostWebConnectionHtmlUnitDriver(environment);
    driver.get("/test");
    verify(this.webClient).getPage(any(WebWindow.class), requestToUrl(new URL("http://localhost:8080/test")));
}
Also used : MockEnvironment(org.springframework.mock.env.MockEnvironment) URL(java.net.URL) WebWindow(com.gargoylesoftware.htmlunit.WebWindow) Test(org.junit.Test)

Example 59 with MockEnvironment

use of org.springframework.mock.env.MockEnvironment in project spring-boot by spring-projects.

the class TestRestTemplateTests method verifyRelativeUriHandling.

private void verifyRelativeUriHandling(TestRestTemplateCallback callback) throws IOException {
    ClientHttpRequestFactory requestFactory = mock(ClientHttpRequestFactory.class);
    MockClientHttpRequest request = new MockClientHttpRequest();
    request.setResponse(new MockClientHttpResponse(new byte[0], HttpStatus.OK));
    URI absoluteUri = URI.create("http://localhost:8080/a/b/c.txt?param=%7Bsomething%7D");
    given(requestFactory.createRequest(eq(absoluteUri), (HttpMethod) any())).willReturn(request);
    RestTemplate delegate = new RestTemplate();
    TestRestTemplate template = new TestRestTemplate(delegate);
    delegate.setRequestFactory(requestFactory);
    LocalHostUriTemplateHandler uriTemplateHandler = new LocalHostUriTemplateHandler(new MockEnvironment());
    template.setUriTemplateHandler(uriTemplateHandler);
    callback.doWithTestRestTemplate(template, URI.create("/a/b/c.txt?param=%7Bsomething%7D"));
    verify(requestFactory).createRequest(eq(absoluteUri), (HttpMethod) any());
}
Also used : ClientHttpRequestFactory(org.springframework.http.client.ClientHttpRequestFactory) InterceptingClientHttpRequestFactory(org.springframework.http.client.InterceptingClientHttpRequestFactory) HttpComponentsClientHttpRequestFactory(org.springframework.http.client.HttpComponentsClientHttpRequestFactory) CustomHttpComponentsClientHttpRequestFactory(org.springframework.boot.test.web.client.TestRestTemplate.CustomHttpComponentsClientHttpRequestFactory) MockEnvironment(org.springframework.mock.env.MockEnvironment) RestTemplate(org.springframework.web.client.RestTemplate) MockClientHttpRequest(org.springframework.mock.http.client.MockClientHttpRequest) URI(java.net.URI) MockClientHttpResponse(org.springframework.mock.http.client.MockClientHttpResponse)

Example 60 with MockEnvironment

use of org.springframework.mock.env.MockEnvironment in project spring-boot by spring-projects.

the class LocalHostWebClientTests method getPageWhenUrlIsRelativeAndHasPortWillUseLocalhostPort.

@Test
public void getPageWhenUrlIsRelativeAndHasPortWillUseLocalhostPort() throws Exception {
    MockEnvironment environment = new MockEnvironment();
    environment.setProperty("local.server.port", "8181");
    WebClient client = new LocalHostWebClient(environment);
    WebConnection connection = mockConnection();
    client.setWebConnection(connection);
    client.getPage("/test");
    verify(connection).getResponse(this.requestCaptor.capture());
    assertThat(this.requestCaptor.getValue().getUrl()).isEqualTo(new URL("http://localhost:8181/test"));
}
Also used : WebConnection(com.gargoylesoftware.htmlunit.WebConnection) MockEnvironment(org.springframework.mock.env.MockEnvironment) WebClient(com.gargoylesoftware.htmlunit.WebClient) URL(java.net.URL) Test(org.junit.Test)

Aggregations

MockEnvironment (org.springframework.mock.env.MockEnvironment)65 Test (org.junit.Test)54 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)10 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)9 TestBean (org.springframework.tests.sample.beans.TestBean)7 ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)6 Before (org.junit.Before)5 LoggingInitializationContext (org.springframework.boot.logging.LoggingInitializationContext)5 CoffeeNetConfigurationProperties (coffee.synyx.autoconfigure.CoffeeNetConfigurationProperties)4 URL (java.net.URL)4 ServerProperties (org.springframework.boot.autoconfigure.web.ServerProperties)4 InetUtils (org.springframework.cloud.commons.util.InetUtils)4 InetUtilsProperties (org.springframework.cloud.commons.util.InetUtilsProperties)4 Environment (org.springframework.core.env.Environment)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 PrintStream (java.io.PrintStream)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 MutablePropertySources (org.springframework.core.env.MutablePropertySources)3 WebClient (com.gargoylesoftware.htmlunit.WebClient)2 WebConnection (com.gargoylesoftware.htmlunit.WebConnection)2