Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The thread context classloader is not reset between tests #2942

Closed
gregw opened this issue Jun 9, 2022 · 3 comments
Closed

The thread context classloader is not reset between tests #2942

gregw opened this issue Jun 9, 2022 · 3 comments

Comments

@gregw
Copy link

gregw commented Jun 9, 2022

version junit 5.8.2

Steps to reproduce

If a test that passes leaves the thread context classloader set, it may break a subsequent test, which itself passes if run by itself. For example:

public class ContextClassLoaderTest
{
    static final ClassLoader LingeringLoader = new URLClassLoader(new URL[0], Thread.currentThread().getContextClassLoader());

    @Test
    public void testA()
    {
        assertThat(Thread.currentThread().getContextClassLoader(), not(sameInstance(LingeringLoader)));
        Thread.currentThread().setContextClassLoader(LingeringLoader);
    }

    @Test void testB()
    {
        assertThat(Thread.currentThread().getContextClassLoader(), not(sameInstance(LingeringLoader)));
        Thread.currentThread().setContextClassLoader(LingeringLoader);
    }
}

Both testA and testB pass if run by themselves, but if run together, then the second always fails.

I tried a similar test with the thread interrupted status and noted that we reset between test runs, so I think the thread context loader should also be reset.

Context

Failures seen in:
-- maven command line
-- Intellij IDE

@sormuras
Copy link
Member

Thanks for raising this issue, Greg.

I can't find a related issue, but if I remember correctly, we discussed whether or not to (re-)set the TCCL before each test execution at some point in the past. We ended up in not doing so, in order support use-cases that set a TCCL spanning multiple test calls. @marcphilipp and @sbrannen ... can you remember better than me?

What about guarding your TCCL-aware tests? For example with a test-internal try-finally construct or via a resetting life-cycle method annotated with @BeforeEach or @AfterEach?

@sbrannen
Copy link
Member

I tried a similar test with the thread interrupted status and noted that we reset between test runs,

That was addressed in #1688.

I can't find a related issue, but if I remember correctly, we discussed whether or not to (re-)set the TCCL before each test execution at some point in the past. We ended up in not doing so, in order support use-cases that set a TCCL spanning multiple test calls. @marcphilipp and @sbrannen ... can you remember better than me?

I honestly don't recall exactly. We may have discussed that and came to that conclusion, but I cannot find a related issue either.

These are the major open issues regarding class loaders:

@sbrannen
Copy link
Member

Actually, I found the other issue:

I'm therefore closing this issue as a duplicate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants