Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

245
Views
JUnit5: access extension field from test class

I need to use extensions to run code before and after all test cases in classes that use it. My test classes need to access a field in my Extension class. Is this possible?

Given:

@ExtendWith(MyExtension.class)
public class MyTestClass {
    
    @Test
    public void test() {
        // get myField from extension and use it in the test
    }
}

and

public class MyExtension implements 
  BeforeAllCallback, AfterAllCallback, BeforeEachCallback, AfterEachCallback {
    
    private int myField;

    public MyExtension() {
        myField = someLogic();
    }

    ...
}

How do I access myField from my test class?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can achieve this via a marker annotation and a BeforeEachCallback extension.

Create a special marker annotation, e.g.

@Documented
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyField {
}

Use the annotation to find and set the values from within the extension:

import org.junit.jupiter.api.extension.BeforeEachCallback;

public class MyExtension implements BeforeEachCallback {

    @Override
    public void beforeEach(final ExtensionContext context) throws Exception {
        // Get the list of test instances (instances of test classes) 
        final List<Object> testInstances = 
            context.getRequiredTestInstances().getAllInstances();
        
        // Find all fields annotated with @MyField
        // in all testInstances objects.
        // You may use a utility library of your choice for this task. 
        // See for example, https://github.com/ronmamo/reflections 
        // I've omitted this boilerplate code here. 

        // Assign the annotated field's value via reflection. 
        // I've omitted this boilerplate code here. 
    }

}

Then, in your tests, you annotate the target field and extend the test with your extension:

@ExtendWith(MyExtension.class)
public class MyTestClass {

    @MyField
    int myField;

    @Test
    public void test() {
        // use myField which has been assigned by the extension before test execution
    }

}

Note: you can alternatively extend BeforeAllCallback which is executed once before all test methods of the class, depending on your actual requirements.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!