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

The following examples show how to use org.springframework.boot.test.util.EnvironmentTestUtils. 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: ConfigurationTest.java    From qonduit with 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 #2
Source File: ConfigurationTest.java    From qonduit with 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 #3
Source File: SpringletsStringTrimmerAdviceAutoConfigurationTest.java    From springlets with Apache License 2.0 6 votes vote down vote up
/**
 * 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
Source File: SpringletsImageFileConverterAutoConfigurationTest.java    From springlets with Apache License 2.0 6 votes vote down vote up
/**
 * 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 File: StaticMethodsAccessTest.java    From ogham with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
	context = new AnnotationConfigApplicationContext();
	EnvironmentTestUtils.addEnvironment(context, 
			"mail.smtp.host="+ServerSetupTest.SMTP.getBindAddress(), 
			"mail.smtp.port="+ServerSetupTest.SMTP.getPort(),
			"ogham.sms.smpp.host=127.0.0.1",
			"ogham.sms.smpp.port="+smppServer.getPort(),
			"spring.freemarker.suffix=");
	context.register( FreeMarkerAutoConfiguration.class, OghamSpringBoot1AutoConfiguration.class);
	context.refresh();
	messagingService = context.getBean(MessagingService.class);
}
 
Example #6
Source File: AmazonConfigurationTest.java    From service-block-samples with 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(AmazonAutoConfiguration.class);
    applicationContext.refresh();
    this.context = applicationContext;
}
 
Example #7
Source File: ITCustomerRepo.java    From Spring with 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 #8
Source File: SpringletsJsonpAdviceAutoConfigurationTest.java    From springlets with Apache License 2.0 5 votes vote down vote up
/**
 * 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 #9
Source File: SpringletsJsonpAdviceAutoConfigurationTest.java    From springlets with Apache License 2.0 5 votes vote down vote up
/**
 * 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());
}
 
Example #10
Source File: OghamSpringBoot1FreeMarkerAutoConfigurationTests.java    From ogham with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
	context = new AnnotationConfigApplicationContext();
	EnvironmentTestUtils.addEnvironment(context, 
			"mail.smtp.host="+ServerSetupTest.SMTP.getBindAddress(), 
			"mail.smtp.port="+ServerSetupTest.SMTP.getPort(),
			"ogham.sms.smpp.host=127.0.0.1",
			"ogham.sms.smpp.port="+smppServer.getPort(),
			"ogham.email.sendgrid.api-key=ogham",
			"spring.sendgrid.api-key=spring",
			"spring.freemarker.charset="+StandardCharsets.UTF_16BE.name());
}
 
Example #11
Source File: OghamSpringBoot1FreeMarkerAutoConfigurationTests.java    From ogham with Apache License 2.0 5 votes vote down vote up
@Test
public void oghamWithFreemarkerAutoConfigWithoutWebContextAndOghamPropertiesShouldUseSpringFreemarkerConfigurationAndOghamProperties() throws Exception {
	EnvironmentTestUtils.addEnvironment(context, "ogham.freemarker.default-encoding="+StandardCharsets.US_ASCII.name());
	context.register(FreeMarkerAutoConfiguration.class, OghamSpringBoot1AutoConfiguration.class);
	context.refresh();
	MessagingService messagingService = context.getBean(MessagingService.class);
	checkEmail(messagingService);
	checkSms(messagingService);
	OghamInternalAssertions.assertThat(messagingService)
		.freemarker()
			.all()
				.configuration()
					.defaultEncoding(equalTo(StandardCharsets.US_ASCII.name()));
}
 
Example #12
Source File: OghamSpringBoot1FreeMarkerAutoConfigurationTests.java    From ogham with Apache License 2.0 5 votes vote down vote up
@Test
public void oghamWithFreemarkerAutoConfigInWebContextAndOghamPropertiesShouldUseSpringFreemarkerConfigurationAndOghamProperties() throws Exception {
	EnvironmentTestUtils.addEnvironment(context, "ogham.freemarker.default-encoding="+StandardCharsets.US_ASCII.name());
	context.register(WebMvcAutoConfiguration.class, FreeMarkerAutoConfiguration.class, OghamSpringBoot1AutoConfiguration.class);
	context.refresh();
	MessagingService messagingService = context.getBean(MessagingService.class);
	checkEmail(messagingService);
	checkSms(messagingService);
	OghamInternalAssertions.assertThat(messagingService)
		.freemarker()
			.all()
				.configuration()
					.defaultEncoding(equalTo(StandardCharsets.US_ASCII.name()));
}
 
Example #13
Source File: OghamSpringBoot1ThymeleafAutoConfigurationTests.java    From ogham with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
	context = new AnnotationConfigApplicationContext();
	EnvironmentTestUtils.addEnvironment(context, 
			"mail.smtp.host="+ServerSetupTest.SMTP.getBindAddress(), 
			"mail.smtp.port="+ServerSetupTest.SMTP.getPort(),
			"ogham.sms.smpp.host=127.0.0.1",
			"ogham.sms.smpp.port="+smppServer.getPort(),
			"ogham.email.sendgrid.api-key=ogham",
			"spring.sendgrid.api-key=spring",
			"ogham.freemarker.default-encoding="+StandardCharsets.US_ASCII.name(),
			"spring.freemarker.charset="+StandardCharsets.UTF_16BE.name());
}
 
Example #14
Source File: RocketMqAutoConfigurationTest.java    From spring-boot-rocketmq-starter with Apache License 2.0 5 votes vote down vote up
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
Source File: OghamSpringBoot1JavaMailAutoConfigurationTests.java    From ogham with Apache License 2.0 5 votes vote down vote up
@Test
public void oghamAloneShouldUseOghamProperties() throws Exception {
	EnvironmentTestUtils.addEnvironment(context, "ogham.email.javamail.host=ogham", "spring.mail.host=spring");
	context.register(OghamSpringBoot1AutoConfiguration.class);
	context.refresh();
	MessagingService messagingService = context.getBean(MessagingService.class);
	OghamInternalAssertions.assertThat(messagingService)
		.javaMail()
			.host(equalTo("ogham"));
}
 
Example #16
Source File: OghamSpringBoot1JavaMailAutoConfigurationTests.java    From ogham with Apache License 2.0 5 votes vote down vote up
@Test
public void oghamWithJavaMailAutoConfigShouldUseSpringProperties() throws Exception {
	EnvironmentTestUtils.addEnvironment(context, "spring.mail.host=spring");
	context.register(MailSenderAutoConfiguration.class, OghamSpringBoot1AutoConfiguration.class);
	context.refresh();
	MessagingService messagingService = context.getBean(MessagingService.class);
	OghamInternalAssertions.assertThat(messagingService)
		.javaMail()
			.host(equalTo("spring"));
}
 
Example #17
Source File: OghamSpringBoot1JavaMailAutoConfigurationTests.java    From ogham with Apache License 2.0 5 votes vote down vote up
@Test
public void oghamWithSpringPropsShouldUseSpringProperties() throws Exception {
	EnvironmentTestUtils.addEnvironment(context, "spring.mail.host=spring");
	context.register(ManuallyEnableSpringPropertiesConfig.class, OghamSpringBoot1AutoConfiguration.class);
	context.refresh();
	MessagingService messagingService = context.getBean(MessagingService.class);
	OghamInternalAssertions.assertThat(messagingService)
		.javaMail()
			.host(equalTo("spring"));
}
 
Example #18
Source File: OghamSpringBoot1JavaMailAutoConfigurationTests.java    From ogham with Apache License 2.0 5 votes vote down vote up
@Test
public void oghamPropertiesWithJavaMailAutoConfigShouldUseOghamPropertiesPrecedence() throws Exception {
	EnvironmentTestUtils.addEnvironment(context, "ogham.email.javamail.host=ogham", "spring.mail.host=spring");
	context.register(MailSenderAutoConfiguration.class, OghamSpringBoot1AutoConfiguration.class);
	context.refresh();
	MessagingService messagingService = context.getBean(MessagingService.class);
	OghamInternalAssertions.assertThat(messagingService)
		.javaMail()
			.host(equalTo("ogham"));
}
 
Example #19
Source File: OghamSpringBoot1JavaMailAutoConfigurationTests.java    From ogham with Apache License 2.0 5 votes vote down vote up
@Test
public void oghamPropertiesWithSpringPropsShouldUseOghamPropertiesPrecedence() throws Exception {
	EnvironmentTestUtils.addEnvironment(context, "ogham.email.javamail.host=ogham", "spring.mail.host=spring");
	context.register(ManuallyEnableSpringPropertiesConfig.class, OghamSpringBoot1AutoConfiguration.class);
	context.refresh();
	MessagingService messagingService = context.getBean(MessagingService.class);
	OghamInternalAssertions.assertThat(messagingService)
		.javaMail()
			.host(equalTo("ogham"));
}
 
Example #20
Source File: SpringBeanResolutionTest.java    From ogham with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
	context = new AnnotationConfigApplicationContext();
	EnvironmentTestUtils.addEnvironment(context, 
			"mail.smtp.host="+ServerSetupTest.SMTP.getBindAddress(), 
			"mail.smtp.port="+ServerSetupTest.SMTP.getPort(),
			"ogham.sms.smpp.host=127.0.0.1",
			"ogham.sms.smpp.port="+smppServer.getPort(),
			"spring.freemarker.suffix=");
	context.register(TestConfig.class, ThymeleafAutoConfiguration.class, FreeMarkerAutoConfiguration.class, OghamSpringBoot1AutoConfiguration.class);
	context.refresh();
	messagingService = context.getBean(MessagingService.class);
}
 
Example #21
Source File: OghamSpringBoot1SendGridAutoConfigurationTests.java    From ogham with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
	context = new AnnotationConfigApplicationContext();
	EnvironmentTestUtils.addEnvironment(context, 
			"mail.smtp.host="+ServerSetupTest.SMTP.getBindAddress(), 
			"mail.smtp.port="+ServerSetupTest.SMTP.getPort(),
			"ogham.sms.smpp.host=127.0.0.1",
			"ogham.sms.smpp.port="+smppServer.getPort(),
			"spring.sendgrid.api-key=spring",
			"ogham.freemarker.default-encoding="+StandardCharsets.US_ASCII.name(),
			"spring.freemarker.charset="+StandardCharsets.UTF_16BE.name());
}
 
Example #22
Source File: OghamSpringBoot1SendGridAutoConfigurationTests.java    From ogham with Apache License 2.0 5 votes vote down vote up
@Test
public void oghamWithSendGridAutoConfigShouldUseSpringSendGridClient() throws Exception {
	EnvironmentTestUtils.addEnvironment(context, "ogham.email.sendgrid.api-key=ogham");
	context.register(SendGridAutoConfiguration.class, OghamSpringBoot1AutoConfiguration.class);
	context.refresh();
	
	MessagingService messagingService = context.getBean(MessagingService.class);
	
	checkEmail(messagingService);
	checkSms(messagingService);
	OghamInternalAssertions.assertThat(messagingService)
		.sendGrid()
			.apiKey(equalTo("spring"))
			.client(isSpringBeanInstance(context, SendGrid.class));
}
 
Example #23
Source File: OghamSpringBoot1SendGridAutoConfigurationTests.java    From ogham with Apache License 2.0 5 votes vote down vote up
@Test
public void oghamWithoutSendGridAutoConfigShouldUseOghamSendGridClientWithOghamProperties() throws Exception {
	EnvironmentTestUtils.addEnvironment(context, "ogham.email.sendgrid.api-key=ogham");
	context.register(ManuallyEnableSendGridPropertiesConfiguration.class, OghamSpringBoot1AutoConfiguration.class);
	context.refresh();
	
	MessagingService messagingService = context.getBean(MessagingService.class);
	
	checkEmail(messagingService);
	checkSms(messagingService);
	OghamInternalAssertions.assertThat(messagingService)
		.sendGrid()
			.apiKey(equalTo("ogham"))
			.client(not(isSpringBeanInstance(context, SendGrid.class)));
}
 
Example #24
Source File: OghamSpringBoot1SendGridAutoConfigurationTests.java    From ogham with Apache License 2.0 5 votes vote down vote up
@Test
public void useCustomSendGridBean() throws Exception {
	EnvironmentTestUtils.addEnvironment(context, "ogham.email.sendgrid.api-key=ogham");
	context.register(CustomSendGridConfig.class, SendGridAutoConfiguration.class, OghamSpringBoot1AutoConfiguration.class);
	context.refresh();
	
	MessagingService messagingService = context.getBean(MessagingService.class);
	
	checkEmail(messagingService);
	checkSms(messagingService);
	OghamInternalAssertions.assertThat(messagingService)
		.sendGrid()
			.apiKey(nullValue(String.class))
			.client(allOf(isA(CustomSendGrid.class), isSpringBeanInstance(context, SendGrid.class)));
}
 
Example #25
Source File: OghamSpringBoot1AutoConfigurationTests.java    From ogham with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
	context = new AnnotationConfigApplicationContext();
	EnvironmentTestUtils.addEnvironment(context, 
			"mail.smtp.host="+ServerSetupTest.SMTP.getBindAddress(), 
			"mail.smtp.port="+ServerSetupTest.SMTP.getPort(),
			"ogham.sms.smpp.host=127.0.0.1",
			"ogham.sms.smpp.port="+smppServer.getPort(),
			"ogham.email.sendgrid.api-key=ogham",
			"spring.sendgrid.api-key=spring",
			"ogham.freemarker.default-encoding="+StandardCharsets.US_ASCII.name(),
			"spring.freemarker.charset="+StandardCharsets.UTF_16BE.name());
}
 
Example #26
Source File: MQConsumerAutoConfigurationTest.java    From rocketmq-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
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 #27
Source File: ITCustomerRepo.java    From Spring with 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 #28
Source File: LabelImageProcessorPropertiesTest.java    From tensorflow-spring-cloud-stream-app-starters with Apache License 2.0 5 votes vote down vote up
@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 #29
Source File: LabelImageProcessorPropertiesTest.java    From tensorflow-spring-cloud-stream-app-starters with Apache License 2.0 5 votes vote down vote up
@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 #30
Source File: TensorflowProcessorPropertiesTest.java    From tensorflow-spring-cloud-stream-app-starters with Apache License 2.0 5 votes vote down vote up
@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")));
}