BootstrappingDecorator implements TestClassDecorator { @Override public void beforeAll(final TestInvocation invocation) throws Exception { final DataSource ds = (DataSource) invocation.getContext().getData(Constants.KEY_DATA_SOURCE); final MetadataExtractor extractor = new MetadataExtractor(invocation.getTestClass()); final List<Method> bootstrappingMethods = extractor.bootstrapping().getAnnotatedMethods(); checkArgument(bootstrappingMethods.size() <= 1, "Only single method is allowed to be annotated with @Bootstrapping"); if (!bootstrappingMethods.isEmpty()) { final Method tmp = bootstrappingMethods.get(0); checkArgument(Modifier.isStatic(tmp.getModifiers()), "A bootstrapping method is required to be static"); final Class<?>[] parameterTypes = tmp.getParameterTypes(); checkArgument(parameterTypes.length == 1, "A bootstrapping method is required to have a single parameter of type DataSource"); checkArgument(parameterTypes[0].equals(DataSource.class), "A bootstrapping method is required to have a single parameter of type DataSource"); tmp.invoke(null, ds); } }  @Override int getPriority(); @Override void beforeAll(final TestInvocation invocation); @Override void afterAll(final TestInvocation invocation); @Override boolean isConfigurationSupported(final ExecutionContext ctx);  }
@Test(expected = IllegalArgumentException.class) public void testBootstrappingMethodIsRequiredToHaveSingleParameterOfTypeDataSource() throws Throwable { final Method bootstrappingMethodTwo = getClass().getMethod("bootstrappingMethodTwo"); when(bootstrappingInpector.getAnnotatedMethods()).thenReturn(Arrays.asList(bootstrappingMethodTwo)); final BootstrappingDecorator decorator = new BootstrappingDecorator(); decorator.beforeAll(invocation); }
@Test(expected = IllegalArgumentException.class) public void testMultipleBootstrapingMethodsAreNotAllowed() throws Throwable { final Method bootstrappingMethodOne = getClass().getMethod("bootstrappingMethodOne", DataSource.class); final Method bootstrappingMethodTwo = getClass().getMethod("bootstrappingMethodTwo"); when(bootstrappingInpector.getAnnotatedMethods()).thenReturn(Arrays.asList(bootstrappingMethodOne, bootstrappingMethodTwo)); final BootstrappingDecorator decorator = new BootstrappingDecorator(); decorator.beforeAll(invocation); }
@Test public void testBootstrappingMethodIsInvoked() throws Throwable { final Method bootstrappingMethodOne = getClass().getMethod("bootstrappingMethodOne", DataSource.class); when(bootstrappingInpector.getAnnotatedMethods()).thenReturn(Arrays.asList(bootstrappingMethodOne)); final BootstrappingDecorator decorator = new BootstrappingDecorator(); decorator.beforeAll(invocation); verifyStatic(BootstrappingDecoratorTest.class); BootstrappingDecoratorTest.bootstrappingMethodOne(eq(ds)); }
@Test(expected = IllegalArgumentException.class) public void testBootstrappingMethodIsRequiredToBeStatic() throws Exception { final Method nonstaticMethod = getClass().getMethod("nonstaticMethod"); when(bootstrappingInpector.getAnnotatedMethods()).thenReturn(Arrays.asList(nonstaticMethod)); final BootstrappingDecorator decorator = new BootstrappingDecorator(); decorator.beforeAll(invocation); }
@Test public void testBootstrappingMethodIsInvoked() throws Throwable { final Method bootstrappingMethodOne = getClass().getMethod("bootstrappingMethodOne", DataSource.class); when(bootstrappingInpector.getAnnotatedMethods()).thenReturn(Arrays.asList(bootstrappingMethodOne)); final BootstrappingDecorator decorator = new BootstrappingDecorator(); decorator.beforeAll(invocation); verifyStatic(BootstrappingDecoratorTest.class, times(1)); BootstrappingDecoratorTest.bootstrappingMethodOne(eq(ds)); }