类org.springframework.boot.test.util.EnvironmentTestUtils源码实例Demo

下面列出了怎么用org.springframework.boot.test.util.EnvironmentTestUtils的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: qonduit   文件: ConfigurationTest.java
@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();
}
 
源代码2 项目: qonduit   文件: ConfigurationTest.java
@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();
}
 
/**
 * 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());
}
 
/**
 * 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());
}
 
源代码5 项目: Spring   文件: ITCustomerRepo.java
@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");
}
 
源代码6 项目: Spring   文件: ITCustomerRepo.java
@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");
}
 
@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")));
}
 
@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));
}
 
@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")));
}
 
@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"));
}
 
@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));
}
 
@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());
}
 
@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")));
}
 
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;
}
 
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();
}
 
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();
}
 
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();
}
 
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();
}
 
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();
}
 
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();
}
 
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();
}
 
@Test
public void testFooCreatePropertyTrue() {
  EnvironmentTestUtils.addEnvironment(context, "foo.create=true");
  context.register(FooConfiguration.class);
  context.refresh();
  assertNotNull(context.getBean(Foo.class));
}
 
@Test(expectedExceptions = NoSuchBeanDefinitionException.class)
public void testFooCreatePropertyFalse() {
  EnvironmentTestUtils.addEnvironment(context, "foo.create=false");
  context.register(FooConfiguration.class);
  context.refresh();
  assertNotNull(context.getBean(Foo.class));
}
 
@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");
}
 
源代码25 项目: qonduit   文件: ConfigurationTest.java
@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());
}
 
源代码26 项目: service-block-samples   文件: GitHubConfigTest.java
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;
}
 
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;
}
 
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;
}
 
/**
 * 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());
}
 
/**
 * 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());
}
 
 类所在包
 类方法
 同包方法