Guava has an extensive set of tests for collection implementations written in JUnit3 that look like:
/*
* 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() {
...
}
}
and then different collections are tested by using TestSuiteBuilder
s that pass in a set of features and generators for the collection type, and a heavily reflective framework identifies the set of test methods to run.
I would like to build something similar in JUnit4, but it's not clear to me how to go about it: building my own Runner
? Theories? My best guess so far is to write something like
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
}
The general question is something like: how, in JUnit4, might I build a suite of tests for an interface type, and then apply those tests to individual implementations?