All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.dagere.kopeme.runnables.PreparableTestRunnables Maven / Gradle / Ivy

There is a newer version: 1.4.1
Show newest version
package de.dagere.kopeme.runnables;

import java.lang.reflect.Method;
import java.util.List;

import org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor;
import org.junit.jupiter.engine.execution.JupiterEngineExecutionContext;

import de.dagere.kopeme.datastorage.RunConfiguration;
import de.dagere.kopeme.junit.rule.BeforeAfterMethodFinderJUnit5;

public class PreparableTestRunnables implements TestRunnable {
   
   private final List beforeMethods;
   private final List afterMethods;
   private final RunConfiguration config;
   private final Class testClass;
   private final TestMethodTestDescriptor descriptor;
   private final JupiterEngineExecutionContext jupiterContext;
   
   private Object instance;
   private KoPeMeThrowingRunnable innerRunnable;

   public PreparableTestRunnables(RunConfiguration config, Class testClass, TestMethodTestDescriptor descriptor, JupiterEngineExecutionContext jupiterContext) {
      this.config = config;
      this.testClass = testClass;
      beforeMethods = BeforeAfterMethodFinder.getBeforeNoMeasurements(testClass);
      afterMethods = BeforeAfterMethodFinder.getAfterNoMeasurements(testClass);

      this.descriptor = descriptor;
      this.jupiterContext = jupiterContext;
   }

   public void prepare() {
      final JupiterEngineExecutionContext methodContext = descriptor.prepare(jupiterContext);
      instance = methodContext.getExtensionContext().getTestInstance().get();

      innerRunnable = new KoPeMeThrowingRunnable() {

         @Override
         public void run() throws Throwable {
            descriptor.execute(methodContext, null);
            methodContext.close();
            if (!methodContext.getThrowableCollector().isEmpty()) {
               throw methodContext.getThrowableCollector().getThrowable();
            }
         }
      };

   }

   @Override
   public KoPeMeThrowingRunnable getTestRunnable() {
      final KoPeMeThrowingRunnable runnable;
      if (config.isExecuteBeforeClassInMeasurement()) {
         List beforeClassMethod = BeforeAfterMethodFinderJUnit5.getBeforeWithMeasurements(testClass);
         List afterClassMethod = BeforeAfterMethodFinderJUnit5.getAfterWithMeasurements(testClass);

         runnable = new BeforeAfterMethodRunnable(beforeClassMethod, innerRunnable, afterClassMethod, instance);
      } else {
         runnable = innerRunnable;
      }
      return runnable;
   }

   @Override
   public KoPeMeThrowingRunnable getBeforeRunnable() {
      prepare();
      KoPeMeThrowingRunnable beforeRunnable = new ListOfMethodRunnable(beforeMethods, instance);
      return beforeRunnable;
   }

   @Override
   public KoPeMeThrowingRunnable getAfterRunnable() {
      KoPeMeThrowingRunnable afterRunnable = new ListOfMethodRunnable(afterMethods, instance);
      return afterRunnable;
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy