Class ResourcesExtension

java.lang.Object
io.github.cjstehno.testthings.junit.ResourcesExtension
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, org.junit.jupiter.api.extension.ParameterResolver

public class ResourcesExtension 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, org.junit.jupiter.api.extension.ParameterResolver
A JUnit 5 extension that provides injection of classpath resources based on object type annotated with the Resource annotation - the supported types are as follows:

Path. A Path will be populated with the path representation of the provided classpath value.

File. A File will be populated with the file representation of the provided classpath value.

String. A String will be populated with the contents of the file at the classpath location, as a String.

InputStream. An InputStream will be populated with the content of the file at the classpath location, as an InputStream.

Reader. A Reader will be populated with the content of the file at the classpath location, as a Reader.

byte[]. A byte array will be populated with the content of the file at the classpath location, as a array of bytes.

Everything else. Any other object type will attempt to deserialize the contents of the file at the classpath location using the configured "serdes" value of the annotation (defaulting to JacksonJsonSerdes if none is specified.

The annotated types may be on:

Static Fields. A "static" field annotated with the Resource annotation will be populated during the "BeforeAll" callback.

Non-Static Fields. A non-static field annotated with the Resource annotation will be populated during the "BeforeEach" callback.

Callback or Test Method Parameters. A lifecycle callback or test method parameter annotated with the Resource annotation will be populated when that method is called by the test framework.

Note: All injected fields will be cleared (set to null) during the appropriate "after" callback.

  • 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)
     
    void
    beforeAll(org.junit.jupiter.api.extension.ExtensionContext context)
     
    void
    beforeEach(org.junit.jupiter.api.extension.ExtensionContext context)
     
    resolveParameter(org.junit.jupiter.api.extension.ParameterContext paramContext, org.junit.jupiter.api.extension.ExtensionContext extContext)
     
    boolean
    supportsParameter(org.junit.jupiter.api.extension.ParameterContext paramContext, org.junit.jupiter.api.extension.ExtensionContext extContext)
     

    Methods inherited from class java.lang.Object

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

    • ResourcesExtension

      public ResourcesExtension()
  • 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
      Specified by:
      beforeEach in interface org.junit.jupiter.api.extension.BeforeEachCallback
      Throws:
      Exception
    • afterEach

      public void afterEach(org.junit.jupiter.api.extension.ExtensionContext context) throws Exception
      Specified by:
      afterEach in interface org.junit.jupiter.api.extension.AfterEachCallback
      Throws:
      Exception
    • 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
    • supportsParameter

      public boolean supportsParameter(org.junit.jupiter.api.extension.ParameterContext paramContext, org.junit.jupiter.api.extension.ExtensionContext extContext) throws org.junit.jupiter.api.extension.ParameterResolutionException
      Specified by:
      supportsParameter in interface org.junit.jupiter.api.extension.ParameterResolver
      Throws:
      org.junit.jupiter.api.extension.ParameterResolutionException
    • resolveParameter

      public Object resolveParameter(org.junit.jupiter.api.extension.ParameterContext paramContext, org.junit.jupiter.api.extension.ExtensionContext extContext) throws org.junit.jupiter.api.extension.ParameterResolutionException
      Specified by:
      resolveParameter in interface org.junit.jupiter.api.extension.ParameterResolver
      Throws:
      org.junit.jupiter.api.extension.ParameterResolutionException