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.

SetSystemProperty / SetEnvironmentVariable broken for nested classes

See original GitHub issue

Looks like 3ecf1338f552d99a5192999ee142d09760413d78 had the side effect of breaking SetSystemProperty and SetEnvironmentVariable when used in nested classes. Example for SetSystemProperty:

@SetSystemProperty(key = "PROP", value = "PROP-VALUE")
class NestedSetSystemPropertyTest {
    @Nested
    @TestMethodOrder(MethodOrderer.OrderAnnotation.class)
    class nested {
        @Test
        @Order(1)
        void worksAsExpected() {
            assertEquals("PROP-VALUE", System.getProperty("PROP"));
        }

        @Test
        @Order(2)
        void breaksUnexpectedly() {
            assertEquals("PROP-VALUE", System.getProperty("PROP"));
        }
    }
}

This is due to SystemPropertyExtension.entriesToSet() now only returning the property “PROP” for the outermost ExtensionContext in AbstractEntryBasedExtension.beforeEach(). While this is fine during the setup phase (beforeEach/beforeAll) during tear-down AbstractEntryBasedExtension.restoreOriginalEntries() uses ExtensionContext.Store.get() to return the matching EntriesBackup: https://github.com/junit-pioneer/junit-pioneer/blob/3ecf1338f552d99a5192999ee142d09760413d78/src/main/java/org/junitpioneer/jupiter/AbstractEntryBasedExtension.java#L149-L151

Note that ExtensionContext.Store.get() has the following documented behaviour:

If no value is stored in the current ExtensionContext for the supplied key, ancestors of the context will be queried for a value with the same key in the Namespace used to create this store.

So although the inner class and inner methods have no EntriesBackup, AbstractEntryBasedExtension.restoreOriginalEntries() grabs the outer context’s instance during tear-down of worksAsExpected(), which accidentally clears the properties for the breaksUnexpectedly() test.

It would be great if ExtensionContext.Store were to provide a way to only query the store of the provided context and not the ancestor stores too. Haven’t come up with an elegant solution for this yet. A workaround could be to store something that identifies the originating context within EntriesBackup on creation and to only restore an EntriesBackup-instance if the current context matches the originating context.

(Our workaround is to pin the junit-pioneer version to 1.4.0 for now.)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
beatngu13commented, May 12, 2021

So, I investigated that issue and can confirm that this indeed is a problem. But thanks to your excellent analysis (kudos!), it wasn’t that hard to come up with a quick fix. As you already pointed out:

A workaround could be to store something that identifies the originating context within EntriesBackup on creation and to only restore an EntriesBackup-instance if the current context matches the originating context.

I did this:

https://github.com/beatngu13/junit-pioneer/blob/0788c6276e7bc647821b080ba4aa92314cfae345/src/main/java/org/junitpioneer/jupiter/AbstractEntryBasedExtension.java#L155-L157

Determining the store key via the display name of the current test or container seems to do the trick.

Would you mind giving this a try? You can obtain my fork’s branch as a dependency e.g. via JitPack:

<repositories>
  <repository>
    <id>jitpack.io</id>
    <url>https://jitpack.io</url>
  </repository>
</repositories>

<!-- ... -->

<dependency>
  <groupId>com.github.beatngu13</groupId>
  <artifactId>junit-pioneer</artifactId>
  <version>issue~480-nested-sysprop-envar-SNAPSHOT</version>
</dependency>
0reactions
nipafxcommented, May 29, 2021

I just released 1.4.2 with the fix. I’m sorry it took 2.5 weeks (particularly after @beatngu13 created the fix in a day). 😔

Read more comments on GitHub >

github_iconTop Results From Across the Web

Java system properties and environment variables
System properties are set on the Java command line using the -Dpropertyname=value syntax. They can also be added at runtime using System.
Read more >
Nested not working in @QuarkusTest · Issue #17975 - GitHub
Describe the bug @Nested classes are not working properly in @QuarkusTest. Expected behavior @Nested tests run without throwing exception ...
Read more >
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 >
Nested Classes - Learning the Java Language
A nested class is a member of its enclosing class. Non-static nested classes (inner classes) have access to other members of the enclosing...
Read more >
Nested and inner classes - Kotlin
Inner classes. A nested class marked as inner can access the members of its outer class. Inner classes carry a reference to an...
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