在测试期间阻止线程以检查其状态的最佳方法是什么?
使用 Java 进行测试(使用 Mockito 和 Powermock,但愿意使用任何可以解决此问题的方法)。我有一个具有以下签名的对象:
public class MyClass {
public void doRun() {
//Do work
}
public int numberComplete() {
//Return number of items complete
}
}
“doRun”方法获取项目列表并“完成”它们(每个项目都需要相当长的时间)。为了“获取”和“完成”一个项目,使用了模拟管理器,因此我进行了如下测试:
@Test public void managerIsInformedOfEachFinishedItem() {
task.doRun();
verify(mockManager, times(3)).finish((Item)any());
}
最后,我可以调用“task.numberComplete()”并看到任务确实返回 3但是,我真正想做的是:
@Test public void managerIsInformedOfEachFinishedItem() {
int count1, count2;
when(mockManager.finish(item1).thenSet(count1 = task.numberComplete());
when(mockManager.finish(item2).thenSet(count2 = task.numberComplete());
task.doRun();
assertEquals(1, count1);
assertEquals(2, count2);
}
我意识到这可能不是已经可用的东西。但是,如果您打算尝试实现这样的东西(无论语法如何 - 我不关心测试最初读取的内容有多干净),您会怎么做?任何建议/想法表示赞赏!
Testing in Java (using Mockito and Powermock but willing to use anything that will solve this). I have an object that has the following signature:
public class MyClass {
public void doRun() {
//Do work
}
public int numberComplete() {
//Return number of items complete
}
}
The "doRun" method gets a list of items and "completes" them (each taking a considerable amount of time). In order to "get" and "finish" an item, a mock manager is used, so I have tests like:
@Test public void managerIsInformedOfEachFinishedItem() {
task.doRun();
verify(mockManager, times(3)).finish((Item)any());
}
At the end of this, I can call "task.numberComplete()" and see that indeed, the task returns 3. But, what I'd really like to do is something like:
@Test public void managerIsInformedOfEachFinishedItem() {
int count1, count2;
when(mockManager.finish(item1).thenSet(count1 = task.numberComplete());
when(mockManager.finish(item2).thenSet(count2 = task.numberComplete());
task.doRun();
assertEquals(1, count1);
assertEquals(2, count2);
}
I realize this may not be something that's already available. But if you were going to try to implement something like this (regardless of the syntax - I don't care how clean the test reads initially), how would you do it? Any suggestions/ideas are appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以实现自己的模拟管理器,在其
finish
方法中为您进行验证,而不是为管理器使用第三方模拟框架。请记住,如果finish
将在不同的线程中调用,那么您不应该将assertEquals
放在那里,因为其中会抛出AssertionError
线程很可能会被忽略。相反,您可以将成功/失败结果存储在模拟管理器的成员变量(属性)中,然后从测试中检查该结果。 (不要忘记同步对该共享结果值的访问或使用 原子 来保存它。)Instead of using a third-party mock framework for the manager, you could implement your own mock manager that does the verification for you in its
finish
method. Bear in mind that iffinish
will get called in a different thread, then you shouldn't put theassertEquals
there because anAssertionError
thrown in that thread will most likely be ignored. Instead, you could store the success/failure result in a member variable (property) of your mock manager and check that from your test afterward. (Don't forget to synchronize access to that shared result value or use an atomic to hold it.)