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