如何构建可配置的 JUnit4 测试套件?
Guava 为用 JUnit3 编写的收集实现提供了一组广泛的测试,如下所示:
/*
* Copyright (C) 2008 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public class CollectionRemoveTester<E> extends AbstractTester<E> {
@CollectionFeature.Require(SUPPORTS_REMOVE)
@CollectionSize.Require(absent = ZERO)
public void testRemove_present() {
...
}
}
然后,通过使用传递给集合类型的一组功能和生成器的 s 来测试不同的集合,并且一个高度反射的框架标识要运行的测试方法集。TestSuiteBuilder
我想在JUnit4中构建类似的东西,但我不清楚如何去做:构建我自己的?理论?到目前为止,我最好的猜测是写一些类似的东西Runner
abstract class AbstractCollectionTest<E> {
abstract Collection<E> create(E... elements);
abstract Set<Feature> features();
@Test
public void removePresentValue() {
Assume.assumeTrue(features().contains(SUPPORTS_REMOVE));
...
}
}
@RunWith(JUnit4.class)
class MyListImplTest<E> extends AbstractCollectionTest<E> {
// fill in abstract methods
}
一般的问题是这样的:在JUnit4中,我如何为接口类型构建一套测试,然后将这些测试应用于各个实现?