/** * Return a short useful message for common BeansExceptions. * @param ex BeansException * @return String short descriptive message */ protected String shortMessage(BeansException ex) { if(ex instanceof NoSuchBeanDefinitionException) { NoSuchBeanDefinitionException nsbde = (NoSuchBeanDefinitionException)ex; return "Missing required bean: " + (nsbde.getBeanName()!=null ? "\""+nsbde.getBeanName()+"\" " : "") + (nsbde.getBeanType()!=null ? "\""+nsbde.getBeanType()+"\" " : ""); } if(ex instanceof BeanCreationException) { BeanCreationException bce = (BeanCreationException)ex; return bce.getBeanName()== null ? "" : "Can't create bean '"+bce.getBeanName()+"'"; } return ex.getMessage().replace('\n', ' '); }
/** * Return a short useful message for common BeansExceptions. * @param ex BeansException * @return String short descriptive message */ protected String shortMessage(BeansException ex) { if(ex instanceof NoSuchBeanDefinitionException) { NoSuchBeanDefinitionException nsbde = (NoSuchBeanDefinitionException)ex; return "Missing required bean: " + (nsbde.getBeanName()!=null ? "\""+nsbde.getBeanName()+"\" " : "") + (nsbde.getBeanType()!=null ? "\""+nsbde.getBeanType()+"\" " : ""); } if(ex instanceof BeanCreationException) { BeanCreationException bce = (BeanCreationException)ex; return bce.getBeanName()== null ? "" : "Can't create bean '"+bce.getBeanName()+"'"; } return ex.getMessage().replace('\n', ' '); }
@Override public void prepareTest(Object testInstance) { try { context.getAutowireCapableBeanFactory().autowireBean(testInstance); } catch (BeansException e) { Throwable cause = e.getMostSpecificCause(); if (cause instanceof NoSuchBeanDefinitionException) { NoSuchBeanDefinitionException n = (NoSuchBeanDefinitionException) cause; throw new MissingBeanException(n, testInstance, n.getBeanType()); } if (cause instanceof PermanentException) throw (PermanentException) cause; throw new TestInjectionFailure(e, testInstance.getClass()); } }
@Override public void startup(Class<?> testClass) { try { context.refresh(); } catch (BeansException e) { Throwable cause = e.getMostSpecificCause(); if (cause instanceof NoSuchBeanDefinitionException) { NoSuchBeanDefinitionException n = (NoSuchBeanDefinitionException) cause; throw new MissingBeanException(n, testClass, n.getBeanType()); } if (cause instanceof PermanentException) throw (PermanentException) cause; throw new TestInjectionFailure(e, testClass); } }
assertThat(ex.getBeanType()).isEqualTo(expectedMissingDependency); }); });