Search in sources :

Example 1 with SpringInstantiator

use of com.vaadin.flow.spring.SpringInstantiator in project flow by vaadin.

the class SpringInstantiatorTest method getOrCreateBean_multipleBeansGivenCannotInstantiate_throwsExceptionWithHint.

@Test
public void getOrCreateBean_multipleBeansGivenCannotInstantiate_throwsExceptionWithHint() {
    ApplicationContext context = Mockito.mock(ApplicationContext.class, Mockito.RETURNS_DEEP_STUBS);
    Mockito.when(context.getBeanNamesForType(Number.class)).thenReturn(new String[] { "one", "two" });
    Mockito.when(context.getAutowireCapableBeanFactory().createBean(Number.class)).thenThrow(new BeanInstantiationException(Number.class, "This is an abstract class"));
    SpringInstantiator instantiator = new SpringInstantiator(null, context);
    try {
        instantiator.getOrCreate(Number.class);
    } catch (BeanInstantiationException e) {
        Assert.assertNotNull(e.getMessage());
        Assert.assertTrue(e.getMessage().contains("[HINT]"));
        return;
    }
    Assert.fail();
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) SpringInstantiator(com.vaadin.flow.spring.SpringInstantiator) BeanInstantiationException(org.springframework.beans.BeanInstantiationException) Test(org.junit.Test)

Example 2 with SpringInstantiator

use of com.vaadin.flow.spring.SpringInstantiator in project flow by vaadin.

the class SpringInstantiatorTest method getOrCreateBean_oneBeanGiven_noException.

@Test
public void getOrCreateBean_oneBeanGiven_noException() {
    ApplicationContext context = Mockito.mock(ApplicationContext.class);
    Mockito.when(context.getBeanNamesForType(Number.class)).thenReturn(new String[] { "one" });
    Mockito.when(context.getBean(Number.class)).thenReturn(0);
    SpringInstantiator instantiator = new SpringInstantiator(null, context);
    Number bean = instantiator.getOrCreate(Number.class);
    Assert.assertEquals(0, bean);
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) SpringInstantiator(com.vaadin.flow.spring.SpringInstantiator) Test(org.junit.Test)

Example 3 with SpringInstantiator

use of com.vaadin.flow.spring.SpringInstantiator in project flow by vaadin.

the class SpringInstantiatorTest method getOrCreateBean_multipleBeansGivenButCanInstantiate_noException.

@Test
public void getOrCreateBean_multipleBeansGivenButCanInstantiate_noException() {
    ApplicationContext context = Mockito.mock(ApplicationContext.class, Mockito.RETURNS_DEEP_STUBS);
    Mockito.when(context.getBeanNamesForType(String.class)).thenReturn(new String[] { "one", "two" });
    Mockito.when(context.getAutowireCapableBeanFactory().createBean(String.class)).thenReturn("string");
    SpringInstantiator instantiator = new SpringInstantiator(null, context);
    String bean = instantiator.getOrCreate(String.class);
    Assert.assertEquals("string", bean);
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) SpringInstantiator(com.vaadin.flow.spring.SpringInstantiator) Test(org.junit.Test)

Example 4 with SpringInstantiator

use of com.vaadin.flow.spring.SpringInstantiator in project flow by vaadin.

the class SpringInstantiatorTest method getOrCreateBean_noBeansGivenCannotInstantiate_throwsExceptionWithoutHint.

@Test
public void getOrCreateBean_noBeansGivenCannotInstantiate_throwsExceptionWithoutHint() {
    ApplicationContext context = Mockito.mock(ApplicationContext.class, Mockito.RETURNS_DEEP_STUBS);
    Mockito.when(context.getBeanNamesForType(Number.class)).thenReturn(new String[] {});
    Mockito.when(context.getAutowireCapableBeanFactory().createBean(Number.class)).thenThrow(new BeanInstantiationException(Number.class, "This is an abstract class"));
    SpringInstantiator instantiator = new SpringInstantiator(null, context);
    try {
        instantiator.getOrCreate(Number.class);
    } catch (BeanInstantiationException e) {
        Assert.assertNotNull(e.getMessage());
        Assert.assertFalse(e.getMessage().contains("[HINT]"));
        return;
    }
    Assert.fail();
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) SpringInstantiator(com.vaadin.flow.spring.SpringInstantiator) BeanInstantiationException(org.springframework.beans.BeanInstantiationException) Test(org.junit.Test)

Aggregations

SpringInstantiator (com.vaadin.flow.spring.SpringInstantiator)4 Test (org.junit.Test)4 ApplicationContext (org.springframework.context.ApplicationContext)4 BeanInstantiationException (org.springframework.beans.BeanInstantiationException)2