question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Wrap @SetEnvironmentVariable in a JUnit Extension

See original GitHub issue

We have a few JUnit test classes that need to bring up the Spring context, and due to some Kafka libraries, we need to set an environment variable in order for the context to successfully load. Would it be possible to wrap the annotation in a JUnit Extension so we only need to put the particular code for the environment variable setting in one place? That way we can just add our custom extension to @ExtendWith wherever it’s needed.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
Michael1993commented, Nov 15, 2020

Okay, so, I tested it. It kind of works but not as I originally wrote. Here is what you need to do:

  1. Create your extension:
public class MyKafkaExtension implements BeforeEachCallback {
    // extension logic here
}
  1. Create a composed annotation:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@ExtendWith(MyKafkaExtension.class)
@SetEnvironmentVariable(key = "foobar", value = "barfoo") // you reference SetEnvironmentVariable here!!!
public @interface KafkaExtension {
}
  1. Use the composed annotation in your test classes:
@Test
@KafkaExtension
void testUsingKafkaEnvVar() {
  // Invokes both MyKafkaExtension and the EnvironmentVariableExtension
}

This is a bit unfortunate, because you have to use @ExtendWith indirectly, but (in my opinion) it also looks somewhat nicer?

@spartanhooah Let us know if this solution works for you.

1reaction
spartanhooahcommented, Nov 16, 2020

Thanks, that works great! I can use @beatngu13’s suggestion since I don’t have any other logic I need to inject.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Guide to the System Stubs Library - Baeldung
Learn the importance of being able to mock system resources and how System Stubs allows for complex configurations of stubbing with JUnit 4 ......
Read more >
How to Set Environment Varibles for Junit Tests with Spring ...
One solution is to wrap the spring test inside a parent class, and set environment variables in that class.
Read more >
Clearing or Setting Environment Variables - JUnit Pioneer
The JUnit 5 (Jupiter) extensions @ClearEnvironmentVariable/@SetEnvironmentVariable clear/set the values of environment variables for the duration of a test.
Read more >
JUnit Pioneer 1.0 - nipafx.dev
Yesterday we released JUnit Pioneer 1.0 - here's a quick ... Let's try to write as few tests as possible while using each...
Read more >
SetEnvironmentVariable (junit-pioneer 1.5.0 API) - Javadoc.io
@SetEnvironmentVariable is a JUnit Jupiter extension to set the value of a environment variable for a test execution. The key and value of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found