java 如何对实现 Runnable 的类进行单元测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31933769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 19:22:56  来源:igfitidea点击:

How to unit test a class that implements Runnable

javamultithreadingunit-testingjunit

提问by ?т?

I have a class ExampleThread that implements the Runnable interface.

我有一个实现 Runnable 接口的 ExampleThread 类。

public class ExampleThread implements Runnable {

    private int myVar;

    public ExampleThread(int var) {
        this.myVar = var;
    }

    @Override
    public void run() {
        if (this.myVar < 0) {
            throw new IllegalArgumentException("Number less than Zero");
        } else {
            System.out.println("Number is " + this.myVar);
        }
    }
}

How can I write JUnit test for this class. I have tried like below

如何为此类编写 JUnit 测试。我试过如下

public class ExampleThreadTest {

    @Test(expected = IllegalArgumentException.class)
    public void shouldThrowIllegalArgumentExceptionForInvalidNumber() {
        ExampleThread exThread = new ExampleThread(-1);

        ExecutorService service = Executors.newSingleThreadExecutor();
        service.execute(exThread);
    }
}

but this does not work. Is there any way I can test this class to cover all code?

但这不起作用。有什么方法可以测试这个类来覆盖所有代码吗?

回答by René Link

I guess you only want to test if the run()method does the right thing. At the moment you also test the ServiceExecutor.

我猜您只想测试该run()方法是否正确。目前,您还测试了ServiceExecutor.

If you just want to write a unit test you should call the runmethod in your test.

如果您只想编写单元测试,则应run在测试中调用该方法。

public class ExampleThreadTest {

    @Test(expected = IllegalArgumentException.class)
    public void shouldThrowIllegalArgumentExceptionForInvalidNumber() {
        ExampleThread exThread = new ExampleThread(-1);
        exThread.run();
    }
}

回答by Codebender

From the Java Doc,

Java Doc

void execute(Runnable command)

Executes the given command at some time in the future. The command may execute in a new thread, in a pooled thread, or in the calling thread, at the discretion of the Executor implementation.

无效执行(可运行命令)

在将来的某个时间执行给定的命令。根据 Executor 实现的判断,该命令可以在新线程、池线程或调用线程中执行。

Which means, the command wouldn't have finished executing before the the Testcasefinished.

这意味着,在测试用例完成之前,命令不会完成执行。

So, when IllegalArgumentExceptionis not thrown before the testcase finished. Hence it would fail.

所以,whenIllegalArgumentException不会在测试用例完成之前抛出。因此它会失败。

You will need to wait for it to finish before completing the test case.

在完成测试用例之前,您需要等待它完成。

@Test(expected = IllegalArgumentException.class)
public void shouldThrowIllegalArgumentExceptionForInvalidNumber() {
    ExampleThread exThread = new ExampleThread(-1);

    ExecutorService service = Executors.newSingleThreadExecutor();
    service.execute(exThread);

    // Add something like this.
    service.shutdown();
    service.awaitTermination(<sometimeout>);
}