Java Code Examples for org.springframework.boot.test.util.EnvironmentTestUtils

The following examples show how to use org.springframework.boot.test.util.EnvironmentTestUtils. 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
Source Project: qonduit   Source File: ConfigurationTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test(expected = BeanCreationException.class)
public void testMissingSSLProperty() throws Exception {
    context.register(SpringBootstrap.class);
    // @formatter:off
    EnvironmentTestUtils.addEnvironment(this.context,
            "qonduit.server.ip:127.0.0.1",
            "qonduit.server.tcp-port:54321",
            "qonduit.server.udp-port:54325",
            "qonduit.http.ip:127.0.0.1",
            "qonduit.http.port:54322",
            "qonduit.websocket.ip:127.0.0.1",
            "qonduit.websocket.port:54323",
            "qonduit.accumulo.zookeepers:localhost:2181",
            "qonduit.accumulo.instance-name:test",
            "qonduit.accumulo.username:root",
            "qonduit.accumulo.password:secret",
            "qonduit.http.host:localhost");
    // @formatter:on
    context.refresh();
}
 
Example 2
Source Project: qonduit   Source File: ConfigurationTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testSSLProperty() throws Exception {
    context.register(SpringBootstrap.class);
    // @formatter:off
    EnvironmentTestUtils.addEnvironment(this.context,
            "qonduit.server.ip:127.0.0.1",
            "qonduit.http.ip:127.0.0.1",
            "qonduit.http.port:54322",
            "qonduit.websocket.ip:127.0.0.1",
            "qonduit.websocket.port:54323",
            "qonduit.accumulo.zookeepers:localhost:2181",
            "qonduit.accumulo.instance-name:test",
            "qonduit.accumulo.username:root",
            "qonduit.accumulo.password:secret",
            "qonduit.http.host:localhost",
            "qonduit.security.ssl.certificate-file:/tmp/foo",
            "qonduit.security.ssl.key-file:/tmp/bar");
    // @formatter:on
    context.refresh();
}
 
Example 3
/**
 * Perform a POST request to check the {@link StringTrimmerAdvice} works.
 *
 * Only the needed autoconfiguration is loaded in order to create
 * the Spring Web MVC artifacts to handle the HTTP request.
 *
 * @see MockServletContext
 * @see MockMvc
 */
@Test
public void registerAdvice() throws Exception {
  EnvironmentTestUtils.addEnvironment(this.context,
      "springlets.mvc.advices.enabled:true",
      "springlets.mvc.advices.trimeditor.chars-to-delete:YOUR-",
      "springlets.mvc.advices.trimeditor.empty-as-null:true");
  this.context.setServletContext(new MockServletContext());
  this.context.register(TestConfiguration.class);
  this.context.refresh();

  MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();
  mockMvc.perform(post("/persons").param("name", "YOUR-NAME").param("surname", "   "))
      .andExpect(status().isOk())
      .andExpect(model().attribute("name", is("NAME")))
      .andExpect(model().attribute("surname", isNull()))
      .andDo(print());
}
 
Example 4
/**
 * Perform a POST request to check the {@link SpringletsImageFileConverter} works.
 *
 * Only the needed autoconfiguration is loaded in order to create
 * the Spring Web MVC artifacts to handle the HTTP request.
 *
 * @see MockServletContext
 * @see MockMvc
 */
@Test
public void checkConverter() throws Exception {
  EnvironmentTestUtils.addEnvironment(this.context, "springlets.image.management:true");
  this.context.setServletContext(new MockServletContext());
  this.context.register(TestConfiguration.class);
  this.context.refresh();

  MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();

  // Mock a multipart file to be sended
  MockMultipartFile imageFile =
      new MockMultipartFile("image", "image1.jpg", "image/jpg", "image1.jpg".getBytes());

  mockMvc
      .perform(MockMvcRequestBuilders.fileUpload("/persons").file(imageFile)
          .param("name", "TESTNAME").param("surname", "TESTSURNAME"))
      .andExpect(status().isOk()).andDo(print());
}
 
Example 5
Source Project: Spring   Source File: ITCustomerRepo.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
	EnvironmentTestUtils.addEnvironment("testcontainers", applicationContext.getEnvironment(),

			"spring.datasource.url=jdbc:tc:postgresql://localhost:" + postgres.getExposedPorts().get(0)
					+ "/test?TC_INITSCRIPT=init_customerdb.sql",
			"spring.datasource.username=" + postgres.getUsername(),
			"spring.datasource.password=" + postgres.getPassword(),
			"spring.datasource.driver-class-name=org.testcontainers.jdbc.ContainerDatabaseDriver");
}
 
Example 6
Source Project: Spring   Source File: ITCustomerRepo.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
	EnvironmentTestUtils.addEnvironment("testcontainers", applicationContext.getEnvironment(),

			"spring.datasource.url=jdbc:tc:postgresql://localhost:" + postgres.getExposedPorts().get(0)
					+ "/test?TC_INITSCRIPT=init_customerdb.sql",
			"spring.datasource.username=" + postgres.getUsername(),
			"spring.datasource.password=" + postgres.getPassword(),
			"spring.datasource.driver-class-name=org.testcontainers.jdbc.ContainerDatabaseDriver");
}
 
Example 7
@Test
public void labelsLocationCanBeCustomized() {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	EnvironmentTestUtils.addEnvironment(context, "inception.labelsLocation:/remote");
	context.register(Conf.class);
	context.refresh();
	LabelImageProcessorProperties properties = context.getBean(LabelImageProcessorProperties.class);
	assertThat(properties.getLabelsLocation(), equalTo(context.getResource("/remote")));
}
 
Example 8
@Test
public void alternativesLengthCanBeCustomized() {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	EnvironmentTestUtils.addEnvironment(context, "inception.labelsLocation:/remote");
	EnvironmentTestUtils.addEnvironment(context, "inception.alternativesLength:5");
	context.register(Conf.class);
	context.refresh();
	LabelImageProcessorProperties properties = context.getBean(LabelImageProcessorProperties.class);
	assertThat(properties.getAlternativesLength(), equalTo(5));
}
 
Example 9
@Test
public void modelLocationCanBeCustomized() {
	EnvironmentTestUtils.addEnvironment(context, "tensorflow.modelLocation:/remote");
	context.register(Conf.class);
	context.refresh();
	TensorflowProcessorProperties properties = context.getBean(TensorflowProcessorProperties.class);
	assertThat(properties.getModelLocation(), equalTo(context.getResource("/remote")));
}
 
Example 10
@Test
public void outputNameCanBeCustomized() {
	EnvironmentTestUtils.addEnvironment(context, "tensorflow.outputName:output1");
	context.register(Conf.class);
	context.refresh();
	TensorflowProcessorProperties properties = context.getBean(TensorflowProcessorProperties.class);
	assertThat(properties.getOutputName(), equalTo("output1"));
}
 
Example 11
@Test
public void outputIndexCanBeCustomized() {
	EnvironmentTestUtils.addEnvironment(context, "tensorflow.outputIndex:666");
	context.register(Conf.class);
	context.refresh();
	TensorflowProcessorProperties properties = context.getBean(TensorflowProcessorProperties.class);
	assertThat(properties.getOutputIndex(), equalTo(666));
}
 
Example 12
@Test
public void saveOutputInHeaderCanBeCustomized() {
	EnvironmentTestUtils.addEnvironment(context, "tensorflow.saveOutputInHeader:false");
	context.register(Conf.class);
	context.refresh();
	TensorflowProcessorProperties properties = context.getBean(TensorflowProcessorProperties.class);
	assertFalse(properties.isSaveOutputInHeader());
}
 
Example 13
@Test
public void vocabularyLocationCanBeCustomized() {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	EnvironmentTestUtils.addEnvironment(context, "inception.vocabularyLocation:/remote");
	context.register(Conf.class);
	context.refresh();
	TwitterSentimentProcessorProperties properties = context.getBean(TwitterSentimentProcessorProperties.class);
	assertThat(properties.getVocabularyLocation(), equalTo(context.getResource("/remote")));
}
 
Example 14
private void load(boolean refresh, String... environment) {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.register(RocketMQAutoConfiguration.class);
    EnvironmentTestUtils.addEnvironment(ctx, environment);
    if (refresh) {
        ctx.refresh();
    }
    this.context = ctx;
}
 
Example 15
private void prepareApplicationContextMissingProducerGroupConfigure() {
    this.context = new AnnotationConfigApplicationContext();
    EnvironmentTestUtils.addEnvironment(this.context, "spring.rocketmq.name-server-address:127.0.0.1:9876");
    this.context.register(TestProducer.class);
    MQProducerAutoConfiguration.setProducer(null);
    this.context.register(MQProducerAutoConfiguration.class);
    this.context.refresh();
}
 
Example 16
private void prepareApplicationContextWithoutParent() {
    this.context = new AnnotationConfigApplicationContext();
    EnvironmentTestUtils.addEnvironment(this.context, "spring.rocketmq.name-server-address:127.0.0.1:9876");
    EnvironmentTestUtils.addEnvironment(this.context, "rocketmq.producer-group:test-producer-group");
    this.context.register(TestProducerNoParent.class);
    this.context.register(MQProducerAutoConfiguration.class);
    this.context.refresh();
}
 
Example 17
private void prepareApplicationContext() {
    this.context = new AnnotationConfigApplicationContext();
    EnvironmentTestUtils.addEnvironment(this.context, "spring.rocketmq.name-server-address:127.0.0.1:9876");
    EnvironmentTestUtils.addEnvironment(this.context, "spring.rocketmq.producer-group:test-producer-group");
    this.context.register(TestProducer.class);
    this.context.register(MQBaseAutoConfiguration.class, MQProducerAutoConfiguration.class);
    this.context.refresh();
}
 
Example 18
private void prepareApplicationContext() {
    this.context = new AnnotationConfigApplicationContext();
    EnvironmentTestUtils.addEnvironment(this.context, "spring.rocketmq.name-server-address:127.0.0.1:9876");
    this.context.register(TestConsumer.class);
    this.context.register(MQConsumerAutoConfiguration.class);
    this.context.refresh();
}
 
Example 19
private void prepareApplicationContextCMOrderly() {
    this.context = new AnnotationConfigApplicationContext();
    EnvironmentTestUtils.addEnvironment(this.context, "spring.rocketmq.name-server-address:127.0.0.1:9876");
    this.context.register(TestConsumerOrderly.class);
    this.context.register(MQConsumerAutoConfiguration.class);
    this.context.refresh();
}
 
Example 20
private void prepareApplicationContextCMError() {
    this.context = new AnnotationConfigApplicationContext();
    EnvironmentTestUtils.addEnvironment(this.context, "spring.rocketmq.name-server-address:127.0.0.1:9876");
    this.context.register(TestConsumerErrorCM.class);
    this.context.register(MQConsumerAutoConfiguration.class);
    this.context.refresh();
}
 
Example 21
private void prepareApplicationContextMissingParent() {
    this.context = new AnnotationConfigApplicationContext();
    EnvironmentTestUtils.addEnvironment(this.context, "spring.rocketmq.name-server-address:127.0.0.1:9876");
    this.context.register(TestConsumerMissingParent.class);
    this.context.register(MQConsumerAutoConfiguration.class);
    this.context.refresh();
}
 
Example 22
Source Project: spring-test-examples   Source File: FooConfigurationTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testFooCreatePropertyTrue() {
  EnvironmentTestUtils.addEnvironment(context, "foo.create=true");
  context.register(FooConfiguration.class);
  context.refresh();
  assertNotNull(context.getBean(Foo.class));
}
 
Example 23
Source Project: spring-test-examples   Source File: FooConfigurationTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = NoSuchBeanDefinitionException.class)
public void testFooCreatePropertyFalse() {
  EnvironmentTestUtils.addEnvironment(context, "foo.create=false");
  context.register(FooConfiguration.class);
  context.refresh();
  assertNotNull(context.getBean(Foo.class));
}
 
Example 24
Source Project: spring-test-examples   Source File: BarConfigurationTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testBarCreation() {
  EnvironmentTestUtils.addEnvironment(context, "bar.name=test");
  context.register(BarConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
  context.refresh();
  assertEquals(context.getBean(Bar.class).getName(), "test");
}
 
Example 25
Source Project: qonduit   Source File: ConfigurationTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testMinimalConfiguration() throws Exception {
    context.register(SpringBootstrap.class);
    // @formatter:off
    EnvironmentTestUtils.addEnvironment(this.context,
            "qonduit.server.ip:127.0.0.1",
            "qonduit.http.ip:127.0.0.1",
            "qonduit.http.port:54322",
            "qonduit.websocket.ip:127.0.0.1",
            "qonduit.websocket.port:54323",
            "qonduit.accumulo.zookeepers:localhost:2181",
            "qonduit.accumulo.instance-name:test",
            "qonduit.accumulo.username:root",
            "qonduit.accumulo.password:secret",
            "qonduit.http.host:localhost",
            "qonduit.security.ssl.use-generated-keypair:true");
    // @formatter:on
    context.refresh();
    Configuration config = this.context.getBean(Configuration.class);
    assertEquals("127.0.0.1", config.getServer().getIp());
    assertEquals(54322, config.getHttp().getPort());
    assertEquals(54323, config.getWebsocket().getPort());
    assertEquals("localhost:2181", config.getAccumulo().getZookeepers());
    assertEquals("test", config.getAccumulo().getInstanceName());
    assertEquals("root", config.getAccumulo().getUsername());
    assertEquals("secret", config.getAccumulo().getPassword());
    assertEquals("localhost", config.getHttp().getHost());
    assertTrue(config.getSecurity().getSsl().isUseGeneratedKeypair());
}
 
Example 26
Source Project: service-block-samples   Source File: GitHubConfigTest.java    License: Apache License 2.0 5 votes vote down vote up
private void load(Class<?> config, String... environment) {
    AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
    EnvironmentTestUtils.addEnvironment(applicationContext, environment);
    applicationContext.register(config);
    applicationContext.register(GitHubAutoConfiguration.class);
    applicationContext.refresh();
    this.context = applicationContext;
}
 
Example 27
private void load(Class<?> config, String... environment) {
    AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
    EnvironmentTestUtils.addEnvironment(applicationContext, environment);
    applicationContext.register(config);
    applicationContext.register(AmazonAutoConfiguration.class);
    applicationContext.refresh();
    this.context = applicationContext;
}
 
Example 28
private void load(boolean refresh, String... environment) {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.register(RocketMqAutoConfiguration.class);
    EnvironmentTestUtils.addEnvironment(ctx, environment);
    if (refresh) {
        ctx.refresh();
    }
    this.context = ctx;
}
 
Example 29
/**
 * Perform a GET request to check the {@link JsonpAdvice} works.
 *
 * Only the needed autoconfiguration is loaded in order to create
 * the Spring Web MVC artifacts to handle the HTTP request.
 *
 * @see MockServletContext
 * @see MockMvc
 */
@Test
public void registerAdvice() throws Exception {
  EnvironmentTestUtils.addEnvironment(this.context,
      "springlets.mvc.advices.enabled:true",
      "springlets.mvc.advices.jsonp.query-param-names:callback1,callback2");
  this.context.setServletContext(new MockServletContext());
  this.context.register(TestConfiguration.class);
  this.context.refresh();

  MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();

  // only returns the invocation related with the first parameter
  mockMvc.perform(get("/persons").param("callback1", "functionJs1").param("callback2", "functionJs2"))
      .andExpect(status().isOk())
      .andExpect(content().string(containsString("functionJs1({\"name\":\"name\",\"surname\":\"surname\"})")))
      .andDo(print());

  mockMvc.perform(get("/persons").param("callback2", "functionJs2"))
      .andExpect(status().isOk())
      .andExpect(content().string(containsString("functionJs2({\"name\":\"name\",\"surname\":\"surname\"})")))
      .andDo(print());

  mockMvc.perform(get("/persons"))
  .andExpect(status().isOk())
  .andExpect(content().string(containsString("{\"name\":\"name\",\"surname\":\"surname\"}")))
  .andDo(print());
}
 
Example 30
/**
 * Perform a GET request to check the {@link JsonpAdvice} works
 * with default values.
 *
 * Only the needed autoconfiguration is loaded in order to create
 * the Spring Web MVC artifacts to handle the HTTP request.
 *
 * @see MockServletContext
 * @see MockMvc
 */
@Test
public void registerAdviceDefaultValues() throws Exception {
  EnvironmentTestUtils.addEnvironment(this.context,
      "springlets.mvc.advices.enabled:true");
  this.context.setServletContext(new MockServletContext());
  this.context.register(TestConfiguration.class);
  this.context.refresh();

  MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();
  mockMvc.perform(get("/persons").param("callback", "functionJs"))
      .andExpect(status().isOk())
      .andExpect(content().string(containsString("functionJs({\"name\":\"name\",\"surname\":\"surname\"})")))
      .andDo(print());
}