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

StackOverFlow when mocking Thread.getName(). #52

Closed
evdjo opened this Issue Aug 27, 2014 · 2 comments

Comments

3 participants
@evdjo

evdjo commented Aug 27, 2014

Hi again,

I want to report an issue I discovered recently.
When one mocks the Thread.getName method, a StackOverFlow occurs. I did debug the code, and it turns out that JMockit internal classes use the Thread.getName method. So when I mock it, an infinite recursion occurs.

Thanks.
JMockit Version 1.10.
JDK 1.7.0_51
OS Win7 64bit

    public class FooTest {
  @Test
     public void blahblah() {

    MockUp<Thread> Mockthread = new MockUp<Thread>() {
        @Mock
        public String getName() {
            return "Fake Thread";
        }
    };
    Thread thread = Mockthread.getMockInstance();


    System.out.println(thread.getName());
}

@rliesenfeld rliesenfeld added the bug label Aug 27, 2014

@rliesenfeld rliesenfeld self-assigned this Aug 27, 2014

@rliesenfeld

This comment has been minimized.

Show comment
Hide comment
@rliesenfeld

rliesenfeld Aug 27, 2014

Member

Ok, this will be fixed for next release. Thanks for reporting.

Member

rliesenfeld commented Aug 27, 2014

Ok, this will be fixed for next release. Thanks for reporting.

@dlipofsky

This comment has been minimized.

Show comment
Hide comment
@dlipofsky

dlipofsky Apr 28, 2015

While it looks like the example above was fixed in 1.12 I still get a stackoverflow for this test in 1.13

@Test
public void test2(@Mocked({ "getName" }) final Thread thread) throws Exception {
    new NonStrictExpectations() {
        {
            thread.getName();
            returns("Thread-1", "Thread-1", "Thread-2");
        }
    };
    System.out.println(thread.getName());
}

and in 1.14 and beyond I get "java.lang.IllegalArgumentException: Attempted to record expectation on unmockable method".

Is there a better way to do this? My actual code isn't going to have a direct reference to the thread instance created in the test, it will be calling Thread.currentThread().getName()

dlipofsky commented Apr 28, 2015

While it looks like the example above was fixed in 1.12 I still get a stackoverflow for this test in 1.13

@Test
public void test2(@Mocked({ "getName" }) final Thread thread) throws Exception {
    new NonStrictExpectations() {
        {
            thread.getName();
            returns("Thread-1", "Thread-1", "Thread-2");
        }
    };
    System.out.println(thread.getName());
}

and in 1.14 and beyond I get "java.lang.IllegalArgumentException: Attempted to record expectation on unmockable method".

Is there a better way to do this? My actual code isn't going to have a direct reference to the thread instance created in the test, it will be calling Thread.currentThread().getName()

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