@Override @SuppressWarnings("UseSpecificCatch") public ResourceConfig configure(TestContext testContext) { TestContextHolder.INSTANCE.set(testContext); TestDescriptor testDescriptor = testContext.getTestDescriptor(); Optional<Application> foundApplication = testDescriptor.getApplication(); ResourceConfig resourceConfig = null; if (foundApplication.isPresent()) { Application application = foundApplication.get(); resourceConfig = ReflectionUtil.INSTANCE.newInstance(application.value()); resourceConfig.setApplicationName(testContext.getName()); } return resourceConfig; }
public EmbeddedServletContainer startEmbeddedServletContainer( @SuperCall Callable<EmbeddedServletContainer> zuper) throws Exception { EmbeddedServletContainer servletContainer = zuper.call(); TestContextHolder.INSTANCE.command((Consumer<TestContext>) testContext -> testContext.addProperty(SERVER, servletContainer) ); return servletContainer; }
@Override public ServiceLocator create(TestContext testContext) { TestConfigurer testConfigurer = testContext.getTestConfigurer(); ServiceLocatorFactory locatorFactory = ServiceLocatorFactory.getInstance(); ServiceLocator serviceLocator = locatorFactory.create(testContext.getName()); return testConfigurer.configure(testContext, serviceLocator); }
@Override public Object postProcessAfterInitialization(Object bean, String beanName) { Class<? extends Object> beanClass = bean.getClass(); //XXX: DO NOT remove this method and code as it is required to extract //collaborators before the sut class is proxied down stream. Once the //sut class is proxied we will not be able to access the sut class //fields with ease. testContext.getSutDescriptor().ifPresent(sutDescriptor -> { if (sutDescriptor.isSutClass(beanClass)) { sutDescriptor.setValue(testContext.getTestInstance(), bean); } }); return bean; }
@Override public void stop(TestContext testContext) { Object testInstance = testContext.getTestInstance(); TestDescriptor testDescriptor = testContext.getTestDescriptor(); Collection<Class<? extends Annotation>> guidelines = testDescriptor.getGuidelines(); serviceLocatorUtil .findAllWithFilter(PostVerifier.class, guidelines, UnitCategory.class) .forEach(p -> p.verify(testContext)); testContext.verify(); //invoke destroy method on fields annotated with Fixture testDescriptor.getFieldDescriptors() .forEach(p -> p.destroy(testInstance)); //invoke destroy method on sut field annotated with Fixture testContext.getSutDescriptor() .ifPresent(p -> p.destroy(testInstance)); testContext.<ServiceInstance>findProperty(SERVICE_INSTANCE) .ifPresent(ServiceInstance::destroy); resourceController.stop(testContext); }
@Override public void start(TestContext testContext) { TestConfigurer testConfigurer = testContext.getTestConfigurer(); TestDescriptor testDescriptor = testContext.getTestDescriptor(); Optional<SutDescriptor> foundSutDescriptor = testContext.getSutDescriptor(); Object testInstance = testContext.getTestInstance(); Collection<Class<? extends Annotation>> guidelines = testDescriptor.getGuidelines(); Optional<Application> foundApplication = testDescriptor.getApplication(); .forEach(p -> p.verify(testContext)); testContext.verify(); .findAllWithFilter(Verifier.class, guidelines, SystemCategory.class) .forEach(p -> p.verify(testContext)); testContext.verify(); });
@Override public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) { return testContext.getSutDescriptor().map(sutDescriptor -> { MockProvider mockProvider = testContext.getMockProvider(); for (FieldDescriptor fieldDescriptor : testContext.getTestDescriptor() .getFieldDescriptors()) { if (!fieldDescriptor.getFake().isPresent()) { continue; } Type fieldType = fieldDescriptor.getGenericType(); TypeToken fieldTypeToken = TypeToken.of(fieldType); TypeToken rawTypeToken = getRawTypeToken(fieldType); if (fieldTypeToken.isSupertypeOf(beanClass)) { Optional<Object> foundValue = fieldDescriptor.getValue(testContext .getTestInstance()); if (foundValue.isPresent()) { return foundValue.get(); } } else if (rawTypeToken.isSupertypeOf(beanClass)) { return mockProvider.createFake(beanClass); } } return null; }).orElse(null); }
AnnotationConfigApplicationContext configContext = (AnnotationConfigApplicationContext) applicationContext; configContext.setId(testContext.getName()); configContext.setDisplayName(testContext.getName()); configContext.setAllowCircularReferences(false); configContext.setAllowBeanDefinitionOverriding(true); TestConfigurer testConfigurer = testContext.getTestConfigurer(); ConfigurableApplicationContext configuredApplicationContext = testConfigurer.configure(testContext, applicationContext); TestDescriptor testDescriptor = testContext.getTestDescriptor(); DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) applicationContext.getBeanFactory(); testContext.computeIfAbsent(SERVICE_INSTANCE, key -> { ServiceProvider<ConfigurableApplicationContext> serviceProvider = ServiceLocatorUtil.INSTANCE.getOne(ServiceProvider.class,
.getOne(ApplicationProvider.class); ApplicationInstance applicationInstance = applicationProvider.start(testContext); testContext.addProperty(APPLICATION_INSTANCE, applicationInstance); String name = testContext.getName(); TestDescriptor testDescriptor = testContext.getTestDescriptor();
if (testContext.getName().equals(locatorName)) { testContext.computeIfAbsent(SERVICE_INSTANCE, key -> { TestDescriptor testDescriptor = testContext.getTestDescriptor(); ClassLoader classLoader = testDescriptor.getTestClassLoader();
@Override public List<Instance> get(TestContext testContext) { TestDescriptor testDescriptor = testContext.getTestDescriptor(); MockProvider mockProvider = testContext.getMockProvider(); Object testInstance = testContext.getTestInstance();
@Override public JDBCDataSource configure(TestContext testContext, LocalResource localResource, PropertiesReader configReader) { JDBCDataSource dataSource = new JDBCDataSource(); dataSource.setUrl(format("jdbc:hsqldb:mem:%s?default_schema=public", testContext .getName())); dataSource.setUser("sa"); dataSource.setPassword(""); return dataSource; }
@RuntimeType public Class<?>[] getServletConfigClasses(@SuperCall Callable<Class<?>[]> zuper, @This Object object) throws Exception { Class<?>[] result = zuper.call(); return TestContextHolder.INSTANCE.query(testContext -> { TestDescriptor testDescriptor = testContext.getTestDescriptor(); Collection<Module> modules = testDescriptor.getModules(); Stream<Class<?>> testModules = modules.stream().map(Module::value); Stream<Class<?>> productionModules = Stream.empty(); if (result != null) { productionModules = Stream.of(result); } return Stream.concat(testModules, productionModules).toArray(Class[]::new); }); }
throws Exception { Injector result = TestContextHolder.INSTANCE.query(testContext -> { TestDescriptor testDescriptor = testContext.getTestDescriptor(); Queue<Module> prodModules = new ConcurrentLinkedQueue<>(); Queue<Module> testModules = new ConcurrentLinkedQueue<>(); testContext.addProperty(TestContextProperties.SERVICE_INSTANCE, serviceInstance);
@Override public void verify(TestContext testContext) { String className = "org.springframework.context.ApplicationContext"; Optional<Class> foundClass = ReflectionUtil.INSTANCE.load(className); testContext.addError( !foundClass.isPresent(), "Please insure Spring Application Context class '{}' is in the classpath", className ); }
@Override public ServerInstance<EmbeddedServletContainer> start(TestContext testContext, Application application, SpringApplicationBuilder configuration) { LoggingUtil.INSTANCE.debug("Starting Spring Boot application '{}'", testContext .getName()); SpringApplication springApplication = configuration.build(); springApplication.run(); Optional<ServletContext> servletContext = testContext.findProperty(SERVLET_CONTEXT); Optional<EmbeddedServletContainer> servletContainer = testContext.findProperty(SERVER); if (servletContext.isPresent() && servletContainer.isPresent()) { EmbeddedServletContainer server = servletContainer.get(); ServletContext context = servletContext.get(); String uri = format(DEFAULT_URI_FORMAT, server.getPort(), context .getContextPath()); URI baseURI = URI.create(uri); return ServerInstanceBuilder.builder() .baseURI(baseURI) .server(server) .property(APP, springApplication) .property(APP_NAME, testContext.getName()) .property(SERVER, server) .build("springboot", application); } throw ExceptionUtil.INSTANCE.propagate( "Could not start springboot application due to missing servlet container"); }
ServiceInstance createService(TestContext testContext, TestDescriptor testDescriptor, TestConfigurer testConfigurer) { ServiceInstance serviceInstance; Optional<ServiceInstance> foundServiceInstance = testContext.<ServiceInstance>findProperty(SERVICE_INSTANCE); if (foundServiceInstance.isPresent()) { serviceInstance = foundServiceInstance.get(); } else { ServiceProvider serviceProvider = serviceLocatorUtil.getFromHintOrDefault( testContext, ServiceProvider.class, DefaultServiceProvider.class, Hint::serviceProvider); Object serviceContext = serviceProvider.create(testContext); testConfigurer.configure(testContext, serviceContext); serviceInstance = serviceProvider.configure(testContext, serviceContext); testContext.addProperty(SERVICE_INSTANCE, serviceInstance); serviceProvider.postConfigure(testContext, serviceInstance); } return serviceInstance; }
? String.format("%s-%s", testContext.getName(), UUID .randomUUID().toString()) : String.format("%s-%s", virtualResource.name(), UUID } else { containerName = virtualResource.name().isEmpty() ? String.format("%s-%d-%s", testContext.getName(), i, UUID .randomUUID().toString()) : String.format("%s-%d-%s", virtualResource.name(), i, testContext.<ContainerInfo>findCollection( DOCKER_CONTAINERS) .stream() try { Map<String, Object> templateContext = testContext.<ContainerInfo>findCollection( DOCKER_CONTAINERS) .stream() .networkSettings().ports(); testContext.addCollectionElement(DOCKER_CONTAINERS, containerInfo);
@Override public InjectionManager create(TestContext testContext) { TestConfigurer testConfigurer = testContext.getTestConfigurer(); InjectionManager injectionManager = Injections.createInjectionManager(); return testConfigurer.configure(testContext, injectionManager); }
@Override protected void runChild(final FrameworkMethod method, RunNotifier runNotifier) { Description methodDescription = describeChild(method); TestClass testClass = getTestClass(); Class<?> javaClass = testClass.getJavaClass(); TestifyJUnit4RunNotifier notifier = (TestifyJUnit4RunNotifier) runNotifier; MDC.put(TEST_CLASS_KEY, javaClass.getSimpleName()); MDC.put(TEST_METHOD_KEY, method.getName()); if (isIgnored(method)) { notifier.fireTestIgnored(methodDescription); } else { try { runLeaf(methodBlock(method), methodDescription, notifier); } catch (Exception e) { notifier.addFailure(e); notifier.pleaseStop(); } finally { if (!isIgnored(method)) { TestContextHolder.INSTANCE.command(testContext -> { LoggingUtil.INSTANCE.debug("performing cleanup of '{}'", testContext.getName()); TestRunner testRunner = testContext.getTestRunner(); testRunner.stop(testContext); TestContextHolder.INSTANCE.remove(); }); } } } }