我正在使用EasyMock测试一个简单的多线程代码:

源代码:

public class EasyMockTest {

    ExecutorService executorService;
    TestObject testObject;

    public EasyMockTest(ExecutorService executorService, TestObject testObject)
    {
        this.executorService = executorService;
        this.testObject = testObject;
    }
    public void test()
    {
        try
        {
            executorService.submit(() ->{
                testObject.doSomething();
            });
        }
        catch(RejectedExecutionException ex)
        {
        }
     }
}


public class TestObject {

    public void doSomething()
    {
    }
}


用EasyMock测试代码:

public class EasyMockTest_test {

    private TestObject testObject;
    private ExecutorService executorService;
    private EasyMockTest easyMockTest;

    @Before
    public void setUp()
    {
        executorService = new ThreadPoolExecutor(1, 1, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(1));
        testObject = EasyMock.createMock(TestObject.class);
        easyMockTest = new EasyMockTest(executorService, testObject);
    }

    @Test
    public void test_easyMockTest()
    {
        testObject.doSomething();
        EasyMock.expectLastCall().andAnswer(new IAnswer<Void>(){
            @Override
            public Void answer() throws Throwable {
                Thread.sleep(100);
                return null;
            }}).times(2);

        EasyMock.replay(testObject);
        easyMockTest.test();
        easyMockTest.test();
        easyMockTest.test();

        EasyMock.verify(testObject);
    }
}


我认为在这种情况下,testObject.doSomething()应该只被调用两次。由于线程池有一个线程,队列大小为1,因此我让前两个线程进入睡眠状态。因此,当我提交三个任务时,应该拒绝第三个任务,而应该调用前两个任务。但是当我运行这段代码
有错误:


java.lang.AssertionError:
验证失败:
TestObject.doSomething():预期:2,实际:1
在org.easymock.internal.MocksControl.verify(MocksControl.java:225)
在org.easymock.EasyMock.verify(EasyMock.java:2007)
...


这意味着该方法仅被调用一次,我无法理解。

我也尝试评论Thread.sleep(100);这次实际的调用时间变为2,但是我认为应该为3,因为没有线程正在睡眠。

然后我尝试像这样移动move.times()位置:

EasyMock.expectLastCall().times(2).andAnswer(new IAnswer<Void>(){
        @Override
        public Void answer() throws Throwable {
            Thread.sleep(100);
            return null;
        }});


此时间错误变为:


java.lang.AssertionError:验证失败:
TestObject.doSomething():预期:3,实际:2


当我给它2时,为什么结果期望3?

抱歉,我不是EasyMock方面的专家,如果有人可以提供帮助,我非常感谢。

最佳答案

不能确保在达到verify之前已执行任务。您需要一些东西来加快执行速度。

这有效:

@测试
公共无效的test_easyMockTest()引发InterruptedException {
CountDownLatch锁存器=新的CountDownLatch(3);

testObject.doSomething();
EasyMock.expectLastCall().andAnswer(new IAnswer<Void>(){
  @Override
  public Void answer() throws Throwable {
    latch.countDown();
    return null;
  }}).times(2);

EasyMock.replay(testObject);
easyMockTest.test();
easyMockTest.test();
easyMockTest.test();

latch.await(1, TimeUnit.SECONDS);

EasyMock.verify(testObject);


}

在这里,我假设您确实希望捕获并忽略RejectedExecutionException

关于java - 使用EasyMock测试简单的多线程代码,但结果很奇怪,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41543510/

10-11 00:57