Hil*_*kus 0 java multithreading unit-testing mockito
我有一个类通常在一个线程中运行,该线程永远处理数据,直到另一个线程调用它的stop().我遇到的问题是单元测试卡在主循环中,因为测试是单线程的,我想保持这种方式.如何在不污染代码的情况下对其进行单元测试?这个类是关键系统的一部分,需要尽可能简单有效,所以我想避免代码中的单元测试黑客
public class MyClass implements Runnable {
boolean running;
public void run() {
//foo is injected from the outside
foo.start();
work();
foo.end();
}
public void work() {
running = true;
while(running) { //main loop
bar.process(); //bar is injected from the outside
}
}
public void stop() {
running = false;
}
}
Run Code Online (Sandbox Code Playgroud)
基本上我在测试中做的是嘲笑foo和bar我run()从单元测试中调用,后来我在bar中验证是否process实际调用了.我还验证了在foo模拟start()和end()被调用.问题是因为我真的想保持测试单线程,测试线程永远陷入while(running)循环中.
我尝试过但不喜欢的一些事情
stop()process().Mockito不喜欢我调用另一个类'方法并抛出异常的事实是否有任何其他建议或常用模式来测试Runnables,或者更好的方法来编写我的代码以便更容易测试?
我建议进行一项更改,这将使您的代码更多地通过书本并允许在单个线程中突破:
while (!Thread.currentThread().isInterrupted() && running) {
bar.process();
}
Run Code Online (Sandbox Code Playgroud)
您可以Thread.currentThread().interrupt()在运行此代码之前调用; 线程的interrupted标志将被设置,方法isInterrupted()将返回true.
这更像是一本书,因为它使你的主循环参与Java的中断机制.
| 归档时间: |
|
| 查看次数: |
1590 次 |
| 最近记录: |