Search in sources :

Example 16 with ApplicationContext

use of org.springframework.context.ApplicationContext in project spring-boot by spring-projects.

the class SampleBitronixApplication method main.

public static void main(String[] args) throws Exception {
    ApplicationContext context = SpringApplication.run(SampleBitronixApplication.class, args);
    AccountService service = context.getBean(AccountService.class);
    AccountRepository repository = context.getBean(AccountRepository.class);
    service.createAccountAndNotify("josh");
    System.out.println("Count is " + repository.count());
    try {
        service.createAccountAndNotify("error");
    } catch (Exception ex) {
        System.out.println(ex.getMessage());
    }
    System.out.println("Count is " + repository.count());
    Thread.sleep(100);
    ((Closeable) context).close();
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) Closeable(java.io.Closeable)

Example 17 with ApplicationContext

use of org.springframework.context.ApplicationContext in project spring-boot by spring-projects.

the class SampleNarayanaApplication method main.

public static void main(String[] args) throws Exception {
    ApplicationContext context = SpringApplication.run(SampleNarayanaApplication.class, args);
    AccountService service = context.getBean(AccountService.class);
    AccountRepository repository = context.getBean(AccountRepository.class);
    service.createAccountAndNotify("josh");
    System.out.println("Count is " + repository.count());
    try {
        // Using username "error" will cause service to throw SampleRuntimeException
        service.createAccountAndNotify("error");
    } catch (SampleRuntimeException ex) {
        // Log message to let test case know that exception was thrown
        System.out.println(ex.getMessage());
    }
    System.out.println("Count is " + repository.count());
    ((Closeable) context).close();
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) Closeable(java.io.Closeable)

Example 18 with ApplicationContext

use of org.springframework.context.ApplicationContext in project spring-boot by spring-projects.

the class MockitoTestExecutionListener method injectFields.

private void injectFields(TestContext testContext, DefinitionsParser parser) {
    ApplicationContext applicationContext = testContext.getApplicationContext();
    MockitoPostProcessor postProcessor = applicationContext.getBean(MockitoPostProcessor.class);
    for (Definition definition : parser.getDefinitions()) {
        Field field = parser.getField(definition);
        if (field != null) {
            postProcessor.inject(field, testContext.getTestInstance(), definition);
        }
    }
}
Also used : Field(java.lang.reflect.Field) ApplicationContext(org.springframework.context.ApplicationContext)

Example 19 with ApplicationContext

use of org.springframework.context.ApplicationContext in project spring-boot by spring-projects.

the class SpyBeanOnContextHierarchyIntegrationTests method testSpying.

@Test
public void testSpying() throws Exception {
    ApplicationContext context = this.childConfig.getContext();
    ApplicationContext parentContext = context.getParent();
    assertThat(parentContext.getBeanNamesForType(ExampleService.class)).hasSize(1);
    assertThat(parentContext.getBeanNamesForType(ExampleServiceCaller.class)).hasSize(0);
    assertThat(context.getBeanNamesForType(ExampleService.class)).hasSize(0);
    assertThat(context.getBeanNamesForType(ExampleServiceCaller.class)).hasSize(1);
    assertThat(context.getBean(ExampleService.class)).isNotNull();
    assertThat(context.getBean(ExampleServiceCaller.class)).isNotNull();
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) Test(org.junit.Test)

Example 20 with ApplicationContext

use of org.springframework.context.ApplicationContext in project spring-boot by spring-projects.

the class ApplicationContextTestUtilsTests method closeNonClosableContext.

@Test
public void closeNonClosableContext() {
    ApplicationContext mock = mock(ApplicationContext.class);
    ApplicationContextTestUtils.closeAll(mock);
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) Test(org.junit.Test)

Aggregations

ApplicationContext (org.springframework.context.ApplicationContext)532 Test (org.junit.Test)256 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)161 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)38 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)32 ConfigurableMessenger (org.springframework.scripting.ConfigurableMessenger)28 File (java.io.File)24 Messenger (org.springframework.scripting.Messenger)24 DataSource (javax.sql.DataSource)23 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)21 Refreshable (org.springframework.aop.target.dynamic.Refreshable)20 StubCloudConnectorTest (org.springframework.cloud.StubCloudConnectorTest)17 SchedulerException (org.quartz.SchedulerException)16 HashMap (java.util.HashMap)15 Map (java.util.Map)14 WebApplicationContext (org.springframework.web.context.WebApplicationContext)14 MovieMapper (com.mapper.MovieMapper)13 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)13 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)13 ArrayList (java.util.ArrayList)12