@Override public void wiring() { CutDescriptor cutDescriptor = testContext.getCutDescriptor(); String testClassName = testContext.getTestClassName(); String cutClassName = cutDescriptor.getTypeName(); Collection<ParameterDescriptor> fieldDescriptors = testContext.getParamaterDescriptors().values(); fieldDescriptors.parallelStream().forEach(p -> { Optional instance = p.getInstance(); if (!instance.isPresent()) { String paramTypeName = p.getTypeName(); logger.warn("Improper wiring detected. Class under test '{}' defined " + "in '{}' declars constructor argument of type '{}' but '{}' " + "does not define a field of type '{}' annotated with @Fake.", cutClassName, testClassName, paramTypeName, testClassName, paramTypeName); } }); }
@Override public void configuration() { String testClassName = testContext.getTestClassName(); Collection<FieldDescriptor> fieldDescriptors = testContext.getFieldDescriptors().values(); CutDescriptor cutDescriptor = testContext.getCutDescriptor(); checkState(testContext.getConstructorCount() == 1, "Class under test '%s' has '%d' constructors. Please insure that " + "the class under test has one and only one constructor.", cutDescriptor.getTypeName(), testContext.getConstructorCount()); fieldDescriptors.parallelStream().forEach(p -> { Class<?> fieldType = p.getType(); String fieldName = p.getName(); String fieldTypeName = p.getTypeName(); checkState(!isFinal(fieldType.getModifiers()), "Field '%s' in test class '%s' can not be mocked because '%s'" + " is a final class.", fieldName, testClassName, fieldTypeName); checkState(!p.hasAnyAnnotation(Real.class, Inject.class), "Field '%s' in test class '%s' is not annotated with @Fake. " + "Note that @Real and @Inject annotations not supported for " + "unit tests. Please use @Fake instead.", fieldName, fieldTypeName ); }); }
@Override public Class<?> getTestClass() { return testContext.getTestClass(); }
public void create() { Map<DescriptorKey, ParameterDescriptor> parameterDescriptors = testContext.getParamaterDescriptors(); Map<DescriptorKey, FieldDescriptor> fieldDescriptors = testContext.getFieldDescriptors(); Object[] arguments = new Object[parameterDescriptors.size()]; Collection<FieldDescriptor> descriptors = fieldDescriptors.values(); UnitIndexFakeInjector indexInjector = new UnitIndexFakeInjector(testContext, testReifier, arguments); UnitNameFakeInjector nameInjector = new UnitNameFakeInjector(testContext, testReifier, arguments); UnitTypeFakeInjector typeInjector = new UnitTypeFakeInjector(testContext, testReifier, arguments); Set<FieldDescriptor> fakeDescriptors = descriptors.parallelStream() .filter(p -> p.hasAnnotation(Fake.class)) .collect(toSet()); //process fields with a custom index first fakeDescriptors.parallelStream() .forEach(indexInjector::inject); //process fields with custom names second fakeDescriptors.parallelStream() .forEach(nameInjector::inject); //finally process fields based on their type fakeDescriptors.parallelStream() .forEach(typeInjector::inject); testReifier.reifyCut(testContext.getCutDescriptor(), arguments); }
@Override public void configuration() { doPrivileged((PrivilegedAction<Object>) () -> { String testClassName = testContext.getTestClassName(); CutDescriptor cutDescriptor = testContext.getCutDescriptor(); Collection<FieldDescriptor> fieldDescriptors = testContext.getFieldDescriptors().values(); if (testContext.getCutCount() == 1) { checkState(testContext.getConstructorCount() == 1, "Class under test '%s' has '%d' constructors. Please insure that " + "the class under test has one and only one constructor.", cutDescriptor.getTypeName(), testContext.getConstructorCount()); if (testContext.getCutCount() > 1) { checkState(false, "Found more than one class under test in %s. Please insure " if (testContext.getCutCount() == 0 && fieldDescriptors.isEmpty()) { checkState(false, "Test class '%s' does not define a field annotated with @Cut " testContext.getAnnotations(Need.class).parallelStream().map(Need::value).forEach(p -> { try { p.getDeclaredConstructor();
public void cut() { Map<DescriptorKey, ParameterDescriptor> parameterDescriptors = context.getParamaterDescriptors(); Map<DescriptorKey, FieldDescriptor> fieldDescriptors = context.getFieldDescriptors(); Object[] arguments = new Object[parameterDescriptors.size()]; Collection<FieldDescriptor> descriptors = fieldDescriptors.values(); .collect(toSet()); Class<?> testClass = context.getTestClass(); .forEach(realInjector::inject); testReifier.reifyCut(context.getCutDescriptor(), arguments);
testContext.setTestInstance(testInstance); Optional<App> appOptional = testContext.getAnnotation(App.class); checkState(appOptional.isPresent(), "Test class '%s' is not annotated with @App annotation.", Set<Class<?>> modules = testContext.getAnnotations(Module.class) .parallelStream() .map(p -> p.value()) Object context = clientProvider.configuration(clientDescriptor); testContext.getConfigMethod(context.getClass()).map(m -> m.getMethod()).ifPresent(m -> { AccessController.doPrivileged((PrivilegedAction<Object>) () -> { try { SystemTestCreator creator = new SystemTestCreator(testContext, reifier, serviceLocator); if (testContext.getCutDescriptor() != null) { creator.cut(); Set<FieldDescriptor> real = testContext.getFieldDescriptors() .values() .parallelStream()
testContext.setTestInstance(testInstance); String testClassName = testContext.getTestClassName(); = new IntegrationTestCreator(testContext, reifier, serviceLocator); if (testContext.getCutDescriptor() != null) { creator.cut(); Set<FieldDescriptor> real = testContext.getFieldDescriptors() .values() .parallelStream()
Map<DescriptorKey, ParameterDescriptor> parameterDescriptors = context.getParamaterDescriptors(); Parameter parameter = paramDescriptor.getParameter(); String testClassName = context.getTestClassName(); Type fieldType = descriptor.getGenericType(); String fieldTypeName = descriptor.getTypeName();
testContext.setTestInstance(testInstance); = new SystemTestCreator(testContext, reifier, serviceLocator); if (testContext.getCutDescriptor() != null) { creator.cut(); Set<FieldDescriptor> real = testContext.getFieldDescriptors() .values() .parallelStream()
public TestContext getTestContext(Class<?> javaClass) { String name = javaClass.getSimpleName(); TestContext testContext = testClassContexts.computeIfAbsent(javaClass, p -> { try { TestContext context = new TestContext(name, javaClass, LOGGER); ClassReader testReader = new ClassReader(javaClass.getName()); testReader.accept(new TestClassAnalyzer(context), ClassReader.SKIP_DEBUG); CutDescriptor cutDescriptor = context.getCutDescriptor(); if (cutDescriptor != null) { ClassReader cutReader = new ClassReader(context.getCutDescriptor().getField().getType().getName()); cutReader.accept(new CutClassAnalyzer(context), ClassReader.SKIP_DEBUG); } return context; } catch (Exception e) { checkState(false, "Analysis of test class '%s' failed.\n'%s'", name, e.getMessage()); //not reachable throw new IllegalStateException(e); } }); return testContext; }
Map<DescriptorKey, ParameterDescriptor> parameterDescriptors = context.getParamaterDescriptors();
public EmbeddedServletContainerFactory getEmbeddedServletContainerFactory( @SuperCall Callable<EmbeddedServletContainerFactory> zuper, @This Object object) throws Exception { EmbeddedServletContainerFactory containerFactory = zuper.call(); ConfigurableEmbeddedServletContainer configurableServletContainer = (ConfigurableEmbeddedServletContainer) containerFactory; configurableServletContainer.setPort(0); SpringBootDescriptor descriptor = getDescriptor(object); descriptor.setContainerFactory(containerFactory); descriptor.setConfigurableServletContainer(configurableServletContainer); TestContext testContext = descriptor.getTestContext(); testContext.getConfigMethod(ConfigurableEmbeddedServletContainer.class) .map(m -> m.getMethod()) .ifPresent(m -> { AccessController.doPrivileged((PrivilegedAction<Object>) () -> { try { m.setAccessible(true); m.invoke(testContext.getTestInstance(), configurableServletContainer); } catch (Exception e) { checkState(false, "Call to config method '%s' in test class '%s' failed.", m.getName(), testContext.getTestClassName()); throw Throwables.propagate(e); } return null; }); }); return containerFactory; }
@Override public String getTestClassName() { return testContext.getTestClassName(); }
@Override public void inject(FieldDescriptor testFieldDescriptor) { CutDescriptor cutDescriptor = context.getCutDescriptor(); Map<DescriptorKey, FieldDescriptor> cutFieldDescriptors = cutDescriptor.getFieldDescriptors(); Type testFieldType = testFieldDescriptor.getGenericType(); String fieldName = testFieldDescriptor.getName(); DescriptorKey descriptorKey = new DescriptorKey(testFieldType, fieldName); //if there is a cut field descriptor that matches the field then lets use that if (cutFieldDescriptors.containsKey(descriptorKey)) { FieldDescriptor cutFieldDescriptor = cutFieldDescriptors.get(descriptorKey); testReifier.reifyField(cutDescriptor, cutFieldDescriptor, testFieldDescriptor); } else { //otherwise find the right field based on the type of the field Collection<FieldDescriptor> descriptors = cutFieldDescriptors.values(); for (FieldDescriptor cutFieldDEscriptor : descriptors) { Type cutFieldType = cutFieldDEscriptor.getGenericType(); TypeToken token = TypeToken.of(testFieldType); if (token.isSubtypeOf(cutFieldType)) { testReifier.reifyField(cutDescriptor, cutFieldDEscriptor, testFieldDescriptor); break; } } } }
public WebApplicationContext createServletApplicationContext(@SuperCall Callable<WebApplicationContext> zuper) throws Exception { this.context = (AnnotationConfigWebApplicationContext) zuper.call(); context.setId(testContext.getName()); context.setAllowBeanDefinitionOverriding(true); context.setAllowCircularReferences(false); Class<?>[] modules = testContext.getAnnotations(Module.class) .stream() .map(Module::value)
testContext.setTestInstance(testInstance);
@Override public void configuration() { doPrivileged((PrivilegedAction<Object>) () -> { String testClassName = testContext.getTestClassName(); CutDescriptor cutDescriptor = testContext.getCutDescriptor(); Collection<FieldDescriptor> fieldDescriptors = testContext.getFieldDescriptors().values(); if (testContext.getCutCount() == 1) { checkState(testContext.getConstructorCount() == 1, "Class under test '%s' has '%d' constructors. Please insure that " + "the class under test has one and only one constructor.", cutDescriptor.getTypeName(), testContext.getConstructorCount()); if (testContext.getCutCount() > 1) { checkState(false, "Found more than one class under test in %s. Please insure " if (testContext.getCutCount() == 0 && fieldDescriptors.isEmpty()) { checkState(false, "Test class '%s' does not define a field annotated with @Cut " testContext.getAnnotations(Need.class).parallelStream().map(Need::value).forEach(p -> { try { p.getDeclaredConstructor();
public void cut() { Map<DescriptorKey, ParameterDescriptor> parameterDescriptors = context.getParamaterDescriptors(); Map<DescriptorKey, FieldDescriptor> fieldDescriptors = context.getFieldDescriptors(); Object[] arguments = new Object[parameterDescriptors.size()]; Collection<FieldDescriptor> descriptors = fieldDescriptors.values(); .collect(toSet()); Class<?> testClass = context.getTestClass(); of(testClass.getDeclaredAnnotationsByType(Module.class)) .map(Module::value) .forEach(realInjector::inject); testReifier.reifyCut(context.getCutDescriptor(), arguments);
Map<DescriptorKey, ParameterDescriptor> parameterDescriptors = context.getParamaterDescriptors(); Parameter parameter = paramDescriptor.getParameter(); String testClassName = context.getTestClassName(); Type fieldType = descriptor.getGenericType(); String fieldTypeName = descriptor.getTypeName();