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

Weird mock behavior mix-up on non-multithreaded tests (1.10.19) #946

Open
fabienbk opened this issue Feb 15, 2017 · 2 comments
Open

Weird mock behavior mix-up on non-multithreaded tests (1.10.19) #946

fabienbk opened this issue Feb 15, 2017 · 2 comments

Comments

@fabienbk
Copy link

fabienbk commented Feb 15, 2017

Hi,

So I've got a some annoying randomly failing tests. They happen a lot on our CI server (quite slow, running red hat) and almost never (1 out of 10000 times) on our workstations (windows, much faster cpus).

I read with the utmost attention the error description:

    Bar$$EnhancerByMockitoWithCGLIB$$7e36d32 cannot be returned by getFoo()
getFoo() should return Foo

If you're unsure why you're getting above error read on.
Due to the nature of the syntax above problem might occur because:

  1. This exception might occur in wrongly written multi-threaded tests.
    Please refer to Mockito FAQ on limitations of concurrency testing.
  2. A spy is stubbed using when(spy.foo()).then() syntax. It is safer to stub spies -
    • with doReturn|Throw() family of methods. More in javadocs for Mockito.spy() method.

... But I can't decide which one is applicable here:

  • Our tests are run by maven surefire, without any threads or forks.
  • We don't use spies at all.

Our general test structure is :

public class FooTest {
    @Mock
    private Foo foo;

  // ... 10-15 mocked fields generally

    @Mock
    private Bar bar;

    @Before
    protected void setup(){
        MockitoAnnotations.initMocks(this);
        
        when(foo.getFoo()).thenReturn(foo);
        when(bar.getBar()).thenReturn(bar);
        // ... 30-40 whens
   }
}

Does this ring any bell?

@raphw
Copy link
Member

raphw commented Feb 15, 2017

Have you tired a recent version? We do no longer support this version.

@fabienbk
Copy link
Author

fabienbk commented Feb 17, 2017

Thanks for the answer, will do and report.
(It's going to take a while with all these any* methods that suddenly don't match null :)

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

2 participants