org.testifyproject.TestContext类的使用及代码示例

x33g5p2x  于2022-01-30 转载在 其他  
字(12.2k)|赞(0)|评价(0)|浏览(96)

本文整理了Java中org.testifyproject.TestContext类的一些代码示例,展示了TestContext类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。TestContext类的具体详情如下:
包路径:org.testifyproject.TestContext
类名称:TestContext

TestContext介绍

[英]A small context class that contains reference to the test testInstance, the test testDescriptor, and helper methods.
[中]一个小的上下文类,包含对test testInstance、testDescriptor和helper方法的引用。

代码示例

代码示例来源:origin: org.testifyproject.junit4/jersey2-system-test

@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;
}

代码示例来源:origin: org.testifyproject.junit4/spring-boot-system-test

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;
}

代码示例来源:origin: org.testifyproject.di/di-hk2

@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);
}

代码示例来源:origin: org.testifyproject.di/di-spring

@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;
}

代码示例来源:origin: org.testifyproject.level/unit

@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);
}

代码示例来源:origin: org.testifyproject.level/system

@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();
  });

代码示例来源:origin: org.testifyproject.di/di-spring

@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);
}

代码示例来源:origin: org.testifyproject.di/di-spring

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,

代码示例来源:origin: org.testifyproject.server/undertow

.getOne(ApplicationProvider.class);
ApplicationInstance applicationInstance = applicationProvider.start(testContext);
testContext.addProperty(APPLICATION_INSTANCE, applicationInstance);
  String name = testContext.getName();
    TestDescriptor testDescriptor = testContext.getTestDescriptor();

代码示例来源:origin: org.testifyproject.di/di-hk2

if (testContext.getName().equals(locatorName)) {
  testContext.computeIfAbsent(SERVICE_INSTANCE, key -> {
    TestDescriptor testDescriptor = testContext.getTestDescriptor();
    ClassLoader classLoader = testDescriptor.getTestClassLoader();

代码示例来源:origin: org.testifyproject.di/di-guice

@Override
public List<Instance> get(TestContext testContext) {
  TestDescriptor testDescriptor = testContext.getTestDescriptor();
  MockProvider mockProvider = testContext.getMockProvider();
  Object testInstance = testContext.getTestInstance();

代码示例来源:origin: org.testifyproject.resources/hsql

@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;
}

代码示例来源:origin: org.testifyproject.junit4/spring-system-test

@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);
  });
}

代码示例来源:origin: org.testifyproject.di/di-guice

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);

代码示例来源:origin: org.testifyproject.junit4/spring-integration-test

@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
  );
}

代码示例来源:origin: org.testifyproject.junit4/spring-boot-system-test

@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");
}

代码示例来源:origin: org.testifyproject.level/system

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;
}

代码示例来源:origin: org.testifyproject.virtual-resources/docker

? 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);

代码示例来源:origin: org.testifyproject.di/di-jersey

@Override
public InjectionManager create(TestContext testContext) {
  TestConfigurer testConfigurer = testContext.getTestConfigurer();
  InjectionManager injectionManager = Injections.createInjectionManager();
  return testConfigurer.configure(testContext, injectionManager);
}

代码示例来源:origin: org.testifyproject.junit4/junit4-core

@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();
        });
      }
    }
  }
}

相关文章