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

Spy on Thread raise unexpected exception while stack walking #2906

Closed
BegBang opened this issue Feb 8, 2023 · 2 comments
Closed

Spy on Thread raise unexpected exception while stack walking #2906

BegBang opened this issue Feb 8, 2023 · 2 comments

Comments

@BegBang
Copy link

BegBang commented Feb 8, 2023

Hello,

when I switched from Mockito 3.9.0 to 5.1.1, one test scenario started to raise an expcetion (scenario that on 3.1.1 worked fine).

Exception in thread "test-pq" org.mockito.exceptions.base.MockitoException: Caught an unexpected exception while stack walking.
This is unexpected and is likely due to a change in either Java's StackWalker or Reflection APIs.
It's worth trying to upgrade to a newer version of Mockito, or otherwise to file a bug report.
	at cz.aimtec.dci.label.PrinterQueue.run(PrinterQueue.java:375)

Simplified testing code bellow.
mockito: 5.1.1
jdk: java-11-openjdk-11.0.18.10-1.windows.redhat.x86_64
os: Win 10
mvn: 3.6.3

public class ThreadSpyTest {
    @Test
    void cleanup() {
        MyThread t = spy(new MyThread());
        t.start();

        await().atMost(10, TimeUnit.SECONDS)
                .until(() -> t.methodCalled);

        verify(t, times(1)).callMethod();
    }

    private class MyThread extends Thread {
        boolean methodCalled = false;

        @Override
        public void run() {
            if (isTrue()) {
                callMethod();
            }
        }

        boolean isTrue() {
            return true;
        }

        void callMethod() {
            System.out.println("Method called!");
            methodCalled = true;
        }
    }
}

I will be glad about any information about this issue.

@TimvdLippe
Copy link
Contributor

Unfortunately, Thread is one of the classes that Mockito relies on internally for its behavior. Stubbing Thread will therefore lead to undefined behavior. Additionally, it is advised not to mock classes you don't own: https://github.com/mockito/mockito/wiki/How-to-write-good-tests#dont-mock-a-type-you-dont-own We are working on improving the user experience by working on a DoNotMock feature to avoid mocking classes/methods that are known to crash Mockito internals (#1833). Therefore, I am closing this as "Infeasible". Apologies for the uninformative exception that is thrown.

@nodece
Copy link

nodece commented Feb 21, 2024

I have the same issue, the MyThread extends Thread, why cannot spy this object?

I am not an expert in this field, so I am very curious.

A workaround to fix this issue by Wrapper:

    public class MyThread {
        private Thread thread;

        public MyThread() {
            thread = new Thread(this::run);
        }

        public void start() {
            thread.start();
        }

        private void run() {
            // TODO
        }
    }

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

No branches or pull requests

3 participants