Closed GoogleCodeExporter closed 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
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
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
Original comment by szcze...@gmail.com
on 19 Apr 2009 at 7:40
Original issue reported on code.google.com by
szcze...@gmail.com
on 25 Jun 2008 at 1:09