2 * ===============================LICENSE_START======================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============================LICENSE_END===========================================
21 package org.openecomp.dcae.apod.analytics.test.runner;
23 import com.google.inject.Guice;
24 import com.google.inject.Injector;
25 import com.google.inject.Module;
26 import org.junit.runners.BlockJUnit4ClassRunner;
27 import org.junit.runners.model.InitializationError;
28 import org.openecomp.dcae.apod.analytics.test.annotation.GuiceModules;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 import java.util.Arrays;
35 * A custom Junit Runner which can be used to run Guice Test with custom Test Modules
37 * @author Rajiv Singla . Creation Date: 10/20/2016.
39 public class GuiceJUnitRunner extends BlockJUnit4ClassRunner {
41 private static final Logger LOG = LoggerFactory.getLogger(GuiceJUnitRunner.class);
43 private final Injector injector;
45 public GuiceJUnitRunner(Class<?> klass) throws InitializationError {
47 Class<?>[] classes = getModulesFor(klass);
48 injector = createInjectorFor(classes);
53 * Returns a new fixture for running a test. Injects Guice members
56 public Object createTest() throws Exception {
57 Object obj = super.createTest();
58 injector.injectMembers(obj);
64 * Creates new Guice Injector and registers Guice test modules
66 * @param classes test module classes
67 * @return Guice injector with test modules
68 * @throws InitializationError
70 private Injector createInjectorFor(Class<?>[] classes) throws InitializationError {
71 Module[] modules = new Module[classes.length];
72 for (int i = 0; i < classes.length; i++) {
74 modules[i] = (Module) classes[i].newInstance();
75 } catch (InstantiationException | IllegalAccessException e) {
76 throw new InitializationError(e);
79 LOG.debug("Creating Junit Test Runner with Guice Test Modules: {}", Arrays.toString(modules));
80 return Guice.createInjector(modules);
84 * Extract user provide test modules from the {@link GuiceModules} annotation
86 * @param klass Target class which is running the test
87 * @return Guice modules contained passed in annotation of Guice Modules
88 * @throws InitializationError
90 private Class<?>[] getModulesFor(Class<?> klass) throws InitializationError {
91 GuiceModules annotation = klass.getAnnotation(GuiceModules.class);
92 if (annotation == null) {
93 throw new InitializationError(
94 "Missing @GuiceModules annotation for unit test '" + klass.getName()
97 return annotation.value();