Class LifecycleExtension

java.lang.Object
io.github.cjstehno.testthings.junit.LifecycleExtension
All Implemented Interfaces:
org.junit.jupiter.api.extension.AfterAllCallback, org.junit.jupiter.api.extension.AfterEachCallback, org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.Extension

public class LifecycleExtension extends Object implements org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.AfterEachCallback, org.junit.jupiter.api.extension.AfterAllCallback
JUnit 5 extension used to provide a bit more control of the before/after lifecycle methods. Extensions run before the local test instance before/after methods - this can lead to chicken-and-egg issues from time to time. This extension allow you to define methods that will be run before and after, based on where this extension is defined in the list of extensions. For example, if you want to execute a specific before and after method before all of the extensions and before all the other local lifecycle methods, you can configure this extension as the first one in the list, and then the annotated method will execute before all of the other extensions, and then after all of the others at the end.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    afterAll(org.junit.jupiter.api.extension.ExtensionContext context)
     
    void
    afterEach(org.junit.jupiter.api.extension.ExtensionContext context)
    Finds all the methods annotated with the Lifecycle(AFTER) annotation, and executes them.
    void
    beforeAll(org.junit.jupiter.api.extension.ExtensionContext context)
     
    void
    beforeEach(org.junit.jupiter.api.extension.ExtensionContext context)
    Finds all the methods annotated with the Lifecycle(BEFORE) annotation and executes them.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • LifecycleExtension

      public LifecycleExtension()
  • Method Details

    • beforeAll

      public void beforeAll(org.junit.jupiter.api.extension.ExtensionContext context) throws Exception
      Specified by:
      beforeAll in interface org.junit.jupiter.api.extension.BeforeAllCallback
      Throws:
      Exception
    • beforeEach

      public void beforeEach(org.junit.jupiter.api.extension.ExtensionContext context) throws Exception
      Finds all the methods annotated with the Lifecycle(BEFORE) annotation and executes them.
      Specified by:
      beforeEach in interface org.junit.jupiter.api.extension.BeforeEachCallback
      Parameters:
      context - the current extension context; never null
      Throws:
      Exception - if there is a problem
    • afterEach

      public void afterEach(org.junit.jupiter.api.extension.ExtensionContext context) throws Exception
      Finds all the methods annotated with the Lifecycle(AFTER) annotation, and executes them.
      Specified by:
      afterEach in interface org.junit.jupiter.api.extension.AfterEachCallback
      Parameters:
      context - the current extension context; never null
      Throws:
      Exception - if there is a problem
    • afterAll

      public void afterAll(org.junit.jupiter.api.extension.ExtensionContext context) throws Exception
      Specified by:
      afterAll in interface org.junit.jupiter.api.extension.AfterAllCallback
      Throws:
      Exception