Skip to content

Instantly share code, notes, and snippets.

@alexsnaps
Last active August 29, 2015 14:06
Show Gist options
  • Save alexsnaps/f42557d008e2ed37f2b1 to your computer and use it in GitHub Desktop.
Save alexsnaps/f42557d008e2ed37f2b1 to your computer and use it in GitHub Desktop.
SPI testing
107/
api/
core/src/main <- defines say Store
core/src/test
core/src/TCK <- defines the TCK tests that every Store should pass (StoreTester.java)
impl/src/main <- defines OnHeapStore.java
impl/src/test <- depends on core:TCK, unit tests OnHeapStore and makes it pass TCK
other/src/main <- defines SomeOtherStore.java
other/src/test <- depends on core:TCK, unit tests SomeOtherStore and makes it pass TCK
@Test
public void testStoreSPI() {
SPITester tester = new StoreTester(new Factory<OnHeapStore>())
.runTestSuite()
.throwJUnitException();
}
public class SPITester<T> {
protected final Factory<T> factory;
public SPITester(Factory<T> factory) {
this.factory = factory;
}
public TestReport runTestSuite() {
TestReport report = new TestReport();
for(Method m : findAllTestMethods()) {
try {
m.invoke();
report.addSuccess(m);
} catch(TestFailure f) {
report.addFailure(m, f);
}
}
return report;
}
}
public class StoreTester extends SPITester<Store> {
@TestMethod
public void testSomething(Store s) {
Store store = this.factory.newInstance();
store.put("foo", "bar");
// assert?!
}
}
@hhuynh
Copy link

hhuynh commented Sep 18, 2014

I was looking at this built-in Parameterized method

@RunWith(Parameterized.class)
public abstract class SpiTestBase {
  protected Class<?> provider;

  public SpiTestBase(Class<?> provider) {
    this.provider = provider;
  }

  @Parameters
  public static Collection<Object[]> data() {
    return Arrays.asList(new Object[][] { { Provider1.class }, { Provider2.class } });
  }
}
public class SpiSuite1 extends SpiTestBase {

  private Store store;

  public SpiSuite1(Class<?> provider) {
    super(provider);
  }

  @Before
  private void setup() {
    // load store impl from provider class
  }

  @Test
  public void test1() {
    // exercise test logic which will be run against Provider1, Provider2, etc
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment