this SO question
但它并没有为我所要做的提供一个解决方案。
我正在使用EventBus(来自greenrobot)通过我的应用程序发送消息。我想能够单元测试我的应用程序,以确认一条消息已张贴到巴士上。就这样。
下面是我想用一个发布消息的方法测试的类:
public class CxManager {
public void postMessage(JsonObject data) {
EventBus.getDefault().post(new MyCustomEvent(data));
}
}
下面是我尝试过但不起作用的测试:
@RunWith(MockitoJUnitRunner.class)
public class CxManagerTest {
@Mock EventBus eventBus;
private CxManager cxManager;
private JsonObject testJsonObject;
@Before public void setUp() throws Exception {
cxManager = new CxManager();
testJsonObject = new JsonObject();
testJsonObject.addProperty("test", "nada");
}
@Test public void shouldPass() {
cxManager.postMessage(testJsonObject);
verify(eventBus).post(new MyCustomEvent(testJsonObject));
}
}
我写了这个测试,甚至知道它可能会失败,因为EventBus使用单例来发布消息,而我不知道如何测试正在执行的单例方法。
而且,这只是一个大项目的一部分。相关部分。我想根据不同的互动测试信息的正确发布