简体   繁体   English

Junit / Mockito - 等待方法执行

[英]Junit/Mockito - wait for method execution

In my application I use observer pattern for some operations and I want to test them in unit tests. 在我的应用程序中,我使用观察者模式进行某些操作,我想在单元测试中测试它们。 The problem is that I don't know how can I test observers using junit/mockito/something else. 问题是我不知道如何使用junit / mockito /其他东西测试观察者。 Any help? 有帮助吗?

For example this is my unit test: 例如,这是我的单元测试:

@Before
public void setUp() throws IOException, Exception {
    observer = new GameInstanceObserver(); // observable (GameInstance) will call update() method after every change
    GameInstance.getInstance().addObserver(observer); // this is observable which is updated by serverService
}

@Test(expected = UserDataDoesntExistException.class)
public void getRoomUserData_usingNullKey_shouldThrowUserDataDoesntExist() throws InterruptedException, UserDataDoesntExistException {
    serverService.createRoom("exampleRoom"); // this operation is asynchronous and updates GameInstance data (as I wrote before GameInstance is Observable)
    Thread.sleep(400); // how to do it in better way?

    GameInstance gi = (GameInstance) observer.getObservable();
    assertTrue(gi.getRoom("exampleRoom").getRoomId().equals("exampleRoom"));
}

I would like to not use Thread.sleep() and use it in that way (or similar): 我想不使用Thread.sleep()并以这种方式(或类似)使用它:

@Test(expected = UserDataDoesntExistException.class)
public void getRoomUserData_usingNullKey_shouldThrowUserDataDoesntExist() throws InterruptedException, UserDataDoesntExistException {
    serverService.createRoom("exampleRoom"); // this operation is asynchronous and updates GameInstance data (as I wrote before GameInstance is Observable)
    waitUntilDataChange(GameInstance.getInstance()); // wait until observable will be changed so I know that it notified all observer and I can validate data

    GameInstance gi = (GameInstance) observer.getObservable();
    assertTrue(gi.getRoom("exampleRoom").getRoomId().equals("exampleRoom"));
}

If I understand correctly, the problem is not really to test the observer, but to test the result of an asynchronous method call. 如果我理解正确,问题不是测试观察者,而是测试异步方法调用的结果。 To do that, create an observer which blocks until its update() method has been called. 为此,创建一个阻塞的观察者,直到调用update()方法。 Something like the following: 类似于以下内容:

public class BlockingGameObserver extends GameInstanceObserver {
    private CountDownLatch latch = new CountDownLatch(1);

    @Override
    public void update() {
        latch.countDown();
    }

    public void waitUntilUpdateIsCalled() throws InterruptedException {
        latch.await();
    }
}

And in your test: 在你的测试中:

private BlockingGameObserver observer;

@Before
public void setUp() throws IOException, Exception {
    observer = new BlockingGameObserver();
    GameInstance.getInstance().addObserver(observer); 
}

@Test
public void getRoomUserData_usingNullKey_shouldThrowUserDataDoesntExist() throws InterruptedException, UserDataDoesntExistException {
    serverService.createRoom("exampleRoom");   
    observer.waitUntilUpdateIsCalled();
    assertEquals("exampleRoom",
                 GameInstance.getInstance().getRoom("exampleRoom").getRoomId());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM