JUnit-假定NotNull抛出异常而不是跳过测试

时间:2019-01-17 15:06:06

标签: java unit-testing junit

在我的测试课中,我有这样的setUp()

import static org.junit.Assume.*;
import org.junit.Test;

@Override
protected void setUp() throws Exception
{
    super.setUp();
    client = new MyClient();
    client.initialize();
}

在MyClient(它是测试类的子类)中,有:

private static class MyClient
{
Socket socket = null;

public void initialize()
    {
        try
        {
            if ( socket == null )
            {
                String targetIP = System.getProperty("targetIP", "some_IP");
                socket = new Socket(targetIP, 1501);
            }
        }

     ... rest of the code ...
   }

public Socket getSocket()
{
    return socket;
}
}

我有一堆使用此套接字的测试。 问题是当我与设备断开连接时,测试仍然尝试使用套接字。 因此,我在每次测试的最开始就放置了

assumeNotNull(client.getSocket());

避免在设备不可用时运行那些测试。

但是当我在Eclipse中运行整个测试项目时,我在JUnit视图中看到了这样的异常:

org.junit.AssumptionViolatedException: got: <[null]>, expected: every item is not null

并且测试被标记为失败(带有红色叉号)。 我同意没有套接字,但是我希望有一些有关跳过测试的消息,而不是有关错误的消息。但这也许是Eclipse的正确行为吗? 我应该另外配置跑步者还是其他东西?

0 个答案:

没有答案
相关问题