org.testng.annotations.Guice Java Examples

The following examples show how to use org.testng.annotations.Guice. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestRunner.java    From qaf with MIT License 6 votes vote down vote up
@Override
public Injector getInjector(IClass iClass) {
  Annotation annotation = AnnotationHelper.findAnnotationSuperClasses(Guice.class, iClass.getRealClass());
  if (annotation == null) return null;
  if (iClass instanceof TestClass) {
    iClass = ((TestClass)iClass).getIClass();
  }
  if (!(iClass instanceof ClassImpl)) return null;
  Injector parentInjector = ((ClassImpl)iClass).getParentInjector();

  Guice guice = (Guice) annotation;
  List<Module> moduleInstances = Lists.newArrayList(getModules(guice, parentInjector, iClass.getRealClass()));

  // Reuse the previous injector, if any
  Injector injector = getInjector(moduleInstances);
  if (injector == null) {
    injector = parentInjector.createChildInjector(moduleInstances);
    addInjector(moduleInstances, injector);
  }
  return injector;
}
 
Example #2
Source File: TestNGRunner.java    From buck with Apache License 2.0 4 votes vote down vote up
/** Guessing whether or not a class is a test class is an imperfect art form. */
private boolean mightBeATestClass(Class<?> klass) {
  int klassModifiers = klass.getModifiers();
  // Test classes must be public, non-abstract, non-interface
  if (!Modifier.isPublic(klassModifiers)
      || Modifier.isInterface(klassModifiers)
      || Modifier.isAbstract(klassModifiers)) {
    return false;
  }
  // Test classes must either have a public, no-arg constructor, or have a constructor that
  // initializes using dependency injection, via the org.testng.annotations.Guice annotation on
  // the class and the com.google.inject.Inject or javax.inject.Inject annotation on the
  // constructor.
  boolean foundPublicNoArgConstructor = false;
  boolean foundInjectedConstructor = false;
  boolean hasGuiceAnnotation = klass.getAnnotationsByType(Guice.class).length > 0;
  for (Constructor<?> c : klass.getConstructors()) {
    if (Modifier.isPublic(c.getModifiers())) {
      if (c.getParameterCount() == 0) {
        foundPublicNoArgConstructor = true;
      }
      if (hasGuiceAnnotation
          && (c.getAnnotationsByType(com.google.inject.Inject.class).length > 0
              || c.getAnnotationsByType(javax.inject.Inject.class).length > 0)) {
        foundInjectedConstructor = true;
      }
    }
  }
  if (!foundPublicNoArgConstructor && !foundInjectedConstructor) {
    return false;
  }
  // Test classes must have at least one public test method (or something that generates tests)
  boolean hasAtLeastOneTestMethod = false;
  for (Method m : klass.getMethods()) {
    if (Modifier.isPublic(m.getModifiers()) && m.getAnnotation(Test.class) != null) {
      hasAtLeastOneTestMethod = true;
    }
    if (Modifier.isPublic(m.getModifiers()) && m.getAnnotation(Factory.class) != null) {
      hasAtLeastOneTestMethod = true; // technically, not *quite* true, but close enough
    }
  }
  return hasAtLeastOneTestMethod;
}