将线程局部变量传递给 JUnit 测试

问题描述 投票:0回答:2

我有一个线程,它初始化线程本地类变量并从运行单元测试开始:

public class FooThread extends Thread {
    TestRunner runner;
    Foo foo;

    public void run() {
        initSomeThreadLocalStuff(); // inits foo and runner
        runner.runJUnitTests(); // JUnitCore.runTest(TestClass)
    }

    private void initSomeThreadLocalStuff() {
        foo = new Foo(this);
        // ...
    }
}

public class Foo() {
    public Foo(FooThread t) {
        // ...
    }    
}

现在我想通过访问(或引用)线程本地对象来运行 JUnit 测试

foo
。这可能吗?我试图保持简单,但复杂的事情似乎不太清楚(所以我添加了一些代码):Foo 对象需要当前的
FooThread
来初始化。

java junit thread-local parameterized-unit-test
2个回答
0
投票

看起来 JUnit 参数化单元测试就是您正在寻找的。

编辑:基于 JUnit wiki 上提供的示例的示例代码:

@RunWith(Parameterized.class)
public class Test {

    @Parameters
    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][] {{ new ThreadLocalProvider() }});
    }

    @Parameter(value = 0) // first data value (0) is default
    public /* NOT private */ ThreadLocalProvider tloProvider;

    public ThreadLocal<Object> tlo;

    @Before
    public void setup() {
        // getNew() will be called in the same thread in which the unit test will run.
        tlo = tloProvider.getNew();
    }

    @Test
    public void test() {
        // Test using tlo.
    }
}

class ThreadLocalProvider {
    public ThreadLocal<Object> getNew() {
        // Instantiate a *new* ThreadLocal object and return it.
    }
}

注意:如果您使用提供程序,您也可以在不使用参数化运行程序的情况下运行测试(只需在

@Before
方法中从提供程序获取一个新对象),但因为我不太了解您的代码或要求,我把这个选择留给你。

此外,您不需要实例化自己的 JUnit Runner。您可以使用 JUnit (

reference
) 提供的 Runner 以及
@RunWith
注释。


0
投票

不知道为什么需要 threadLocal。如果您需要使用不同的参数运行相同的测试,那么只需创建这些参数的列表并使用参数化测试(junit 的本机或许多库,如 zohhak 或 junit-dataprovider)。

如果出于任何原因您需要访问测试中的本地线程,那么您还需要在测试中向其中插入数据,因为在运行测试之前您不知道将使用哪个线程来运行测试。但您似乎仍然可以编写一个测试来检查您的代码是否正确使用 threadLocal,然后编写参数化测试来检查您的代码是否正确处理从 threadLocal 获取的值

© www.soinside.com 2019 - 2024. All rights reserved.