Wrap @SetEnvironmentVariable in a JUnit Extension
See original GitHub issueWe 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:
- Created 3 years ago
- Comments:13 (8 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Okay, so, I tested it. It kind of works but not as I originally wrote. Here is what you need to do:
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.
Thanks, that works great! I can use @beatngu13’s suggestion since I don’t have any other logic I need to inject.