funbee / mockito

Automatically exported from code.google.com/p/mockito
0 stars 0 forks source link

NPE on verification when multiple threads used #14

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I am getting a null pointer in my verify step. I get it from something
like verify(mock, times(2).myMethod()

main test thread:
 init mocks
 stub what is needed
 kick of two threads

in the two threads
 execute myMethod()

back in main thread
 wait for the threads to day
 verify myMethod()

Stack trace:

java.lang.NullPointerException
       at
org.mockito.internal.invocation.Invocation.isToString(Invocation.java:
181)
       at org.mockito.internal.verification.RegisteredInvocations
$RemoveToString.isOut(RegisteredInvocations.java:33)
       at org.mockito.internal.verification.RegisteredInvocations
$RemoveToString.isOut(RegisteredInvocations.java:31)
       at org.mockito.internal.util.ListUtil.filter(ListUtil.java:15)
       at
org.mockito.internal.verification.RegisteredInvocations.getVerifiableInvocations
(RegisteredInvocations.java:
28)
       at
org.mockito.internal.verification.VerifyingRecorder.getRegisteredInvocations(Ver
ifyingRecorder.java:
35)
       at
org.mockito.internal.verification.VerifyingRecorder.verify(VerifyingRecorder.jav
a:
47)
       at org.mockito.internal.MockHandler.intercept(MockHandler.java:73)
       at
org.mockito.internal.creation.MethodInterceptorFilter.intercept(MethodIntercepto
rFilter.java:
45)
       at com.sonicsw.esb.service.common.ramps.IDataSink$$EnhancerByCGLIB$
$413fdc72.createSinkConnection(<generated>)
       at RampTest.testUnblock(RampTest.java:326)

Original issue reported on code.google.com by szcze...@gmail.com on 25 Jun 2008 at 1:09

GoogleCodeExporter commented 9 years ago
Tried to reproduce it but everything works correctly. Can you submit more code
describing the problem?

Here is how I tried to reproduce it:

    public interface Foo {
        String myMethod();
    }

    @Test
    public void shouldAllowVerifyingInThreads() throws Exception {
        final Foo foo = mock(Foo.class);

        stub(foo.myMethod()).toReturn("hello!");

        Thread threadOne = new Thread() {
            public void run() {
                foo.myMethod();
            }
        };

        Thread threadTwo = new Thread() {
            public void run() {
                foo.myMethod();
            }
        };

        threadOne.start();
        threadTwo.start();

        threadOne.join();
        threadTwo.join();

        verify(foo, times(2)).myMethod();
    }

Original comment by szcze...@gmail.com on 25 Jun 2008 at 1:12

GoogleCodeExporter commented 9 years ago
Here is a reproducible testcase:

package com.sonicsw.esb.service.common.ramps;

import static org.mockito.Mockito.*;

import org.mockito.MockitoAnnotations;
import org.mockito.MockitoAnnotations.Mock;

import junit.framework.TestCase;

public class MockitoTest extends TestCase {
  public interface Foo {
    String myMethod(Object[] _args);
  }

  @Mock
  private Foo foo;

  @Override
  protected void setUp() throws Exception {
    super.setUp();
    MockitoAnnotations.initMocks(this);
  }

  public void testShouldAllowVerifyingInThreads() throws Exception {
    stub(foo.myMethod(isA(Object[].class))).toReturn("hello!");
    // simulate 2 listener threads
    final Thread[] listeners = new Thread[3];
    for (int i = 0; i < listeners.length; i++) {
      (listeners[i] = new Thread() {
        @Override
        public void run() {
          foo.myMethod(new Object[1]);
          try {
            Thread.sleep(100);
          } catch (InterruptedException e) {
            // ignore
          }
        }
      }).start();
    }
    try {
      Thread.sleep(280);
    } catch (final InterruptedException e) {
      fail();
    }
    for (int i = 0; i < listeners.length; i++) {
      try {
        listeners[i].join();
      } catch (final InterruptedException e) {
        fail();
      }
    }
    verify(foo, times(listeners.length)).myMethod(isA(Object[].class));
  }
}

It fails 9 out of 10 times.

The key issues is the use if an Object[] with null items and the use of more 
than one
thread.

Cheers
Thomas

Original comment by thestone...@googlemail.com on 25 Jun 2008 at 1:52

GoogleCodeExporter commented 9 years ago
Fixed in trunk. Snapshots are downloadable here:

http://hudson.ramfelt.se/job/Mockito/

Original comment by szcze...@gmail.com on 25 Jun 2008 at 6:51

GoogleCodeExporter commented 9 years ago

Original comment by szcze...@gmail.com on 19 Apr 2009 at 7:40